这是关于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);
}
});
答案 0 :(得分:1)
首先添加一个输入隐藏字段,并在初始化$ $factor
变量后为其分配factor
变量。然后通过javascript访问此变量
说 在你的phtml文件中添加
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);
}
?>
<input type="hidden" id="factorDiscount" value="<?php echo $factor;?>"/>
//Hidden field added here
<?php
//If you have other codes place it here
?>
在js文件中使用
访问上述变量var factor = $("#factorDiscount").val();
我希望这就是你的意思。 :)
答案 1 :(得分:0)
如果您的外部js文件具有.js
扩展名,它可能不会被处理为php并且php源代码将保留在那里,打破了javascript。
有几种方法可以解决这个问题,例如:
.php
; .js
个文件作为php文件处理;