【发布时间】:2015-03-17 09:05:32
【问题描述】:
这是关于 Magento 电子商务网站的。
我现在需要将一个变量从 view.phtml 传递给 product.js。
首先,我在 php 文件(view.phtml)中创建了一个 if 语句,以便从数据库中获取折扣率:
if(Mage::getSingleton('customer/session')->isLoggedIn()) {
$customerData = Mage::getSingleton('customer/session')->getCustomer();
//echo $customerData->getemail();
$conn = Mage::getModel('core/resource')->getConnection('core_read');
$sql = 'select max(sl.discount_amount) from salesrule sl
join salesrule_customer_group scg on scg.rule_id = sl.rule_id
join customer_entity ce on ce.group_id = scg.customer_group_id
where ce.email = "'.$customerData->getemail().'";';
$results = $conn->fetchAll($sql);
//echo $results;
}
else
{ $factor = 100;} //only apply to NOTLOGGED IN's custom option price label
foreach ($results as $result){
{ $factor = $result['max(sl.discount_amount)'];
}
//echo '<small> Discounted price: </small> $',$prices*($factor/100);
}
如您所见,该方法正在重新调整 $factor 作为结果,我需要在 product.js(javascript) 文件中传递此变量 $factor。
var subPrice = 0;
var subPriceincludeTax = 0;
//var a = test;
Object.values(this.customPrices).each(function(el){
if (el.excludeTax && el.includeTax) {
subPrice += parseFloat(el.excludeTax); // this will affect the price when changing option *important
//so need to change somewhere else in the same time to full fill the requirementirement
// but it doesn;t change to backend value
subPriceincludeTax += parseFloat(el.includeTax);
} else {
subPrice += parseFloat(el.price);
subPriceincludeTax += parseFloat(el.price);
}
});
excl += subPrice;//also affect the final price *frontEnd
incl += subPriceincludeTax;
我试图通过 JSON 传递它,但它根本不起作用,我无法弄清楚原因。我有类似echo json_encode($factor); 的东西来验证输出,它看起来很好,但仍然无法传递到外部 js 文件。
有人可以帮忙吗?谢谢
更新 1: 在 php 文件中,我添加了这些:
var optionsPrice = new Product.OptionsPrice(<?php echo $this->getJsonConfig() ?>);
var tester = "<?php echo json_encode($factor);?>"
</script>
并将这些代码传递给js,例如:
var subPrice = 0;
var subPriceincludeTax = 0;
var tester = tester;
//var a = test;
Object.values(this.customPrices).each(function(el){
if (el.excludeTax && el.includeTax) {
subPrice += parseFloat(el.excludeTax)*tester; // this will affect the price when changing option *important
//so need to change somewhere else in the same time to full fill the requirementirement
// but it doesn;t change to backend value
subPriceincludeTax += parseFloat(el.includeTax);
} else {
subPrice += parseFloat(el.price);
subPriceincludeTax += parseFloat(el.price);
}
});
excl += subPrice;//also affect the final price *frontEnd
incl += subPriceincludeTax;
但这似乎不起作用:(
更新 2 根据outlooker,我尝试在phtml文件中添加输入类型,如下所示:
<script type="text/javascript">
var optionsPrice = new Product.OptionsPrice(<?php echo $this->getJsonConfig() ?>); //the var of the option price, disable to disable price options
**<input type="hidden" id="factorDiscount" value="<?php echo $factor;?>"/>**
</script>
之后我也将var实现到js文件中,调用并使用:
var subPrice = 0;
var subPriceincludeTax = 0;
var factor = $("#factorDiscount").val();//is not global
Object.values(this.customPrices).each(function(el){
if (el.excludeTax && el.includeTax) {
subPrice += parseFloat(el.excludeTax)*factor; // this will affect the price when changing option *important
subPriceincludeTax += parseFloat(el.includeTax);
} else {
subPrice += parseFloat(el.price);
subPriceincludeTax += parseFloat(el.price);
}
});
它现在不起作用,但我明白你的意思,但我可能没有以正确的方式插入代码。任何意见?先生。谢谢你
更新 3 谢谢你的帮助,先生。我将代码完美地插入了phtml文件中,但是当我尝试使用它时,它似乎没有从php中获取数据
var subPrice = 0;
var subPriceincludeTax = 0;
var factor = $("#factorDiscount").val();//I declare the var here
Object.values(this.customPrices).each(function(el){
if (el.excludeTax && el.includeTax) {
subPrice += parseFloat(el.excludeTax)*factor; // trying to times the var factor with the price but don't know if i am doing the right things.
subPriceincludeTax += parseFloat(el.includeTax);
} else {
subPrice += parseFloat(el.price);
subPriceincludeTax += parseFloat(el.price);
}
});
【问题讨论】:
-
将值保存在隐藏字段中并通过javascript访问它
-
介意给我更多细节吗?
标签: javascript php jquery json magento