我知道传递 JavaScript 变量/函数的方式与 Magento 中的标准 JavaScript 类似,但由于这是我第一次开发电子商务系统,我想把一切都说清楚,以防止将来提出重复的问题.
所以我在Product.js中有一个我自己编写的函数:
var subPrice = 0; //is the price inside the option
var subPriceincludeTax = 0;
var discountRate = discountRateUrl; //discount base on database
var price = priceUrl;//get the product price
var discountedPrice = price*discountRate; // price * ourdiscount
//var discountedSubPrice = subPrice*((100-test)/100); // custom option addition price * ourdiscounted prices
//console.log(discountedPrice); //display the prices as int
//console.log(discountedSubPrice);
//console.log(test);
Object.values(this.customPrices).each(function(el){
if (el.excludeTax && el.includeTax) {
subPrice += parseFloat(el.excludeTax*discountRate); // use the var factor && this will affect the price when changing option *important
subPriceincludeTax += parseFloat(el.includeTax*discountRate);
} else {
subPrice += parseFloat(el.price);
subPriceincludeTax += parseFloat(el.price);
}
var finalprice = (subPrice*discountRate+discountedPrice);//checking if getting the php
var fomattedprice = finalprice.toFixed(2); //Convert a number into a string, keeping only two decimals
console.log(finalprice); //tester of the final prices
console.log(discountRate);//tester discount rate in string
document.getElementById("finalprice").innerHTML = '$' + fomattedprice ;
});
所以基本上,这是一个让我重新计算价格然后通过这行代码返回它的函数 document.getElementById("finalprice").innerHTML = '$' + fomattedprice ;
我现在想知道如何将此结果放入另一个 php 文件名 Data.php
public function formatPrice($price)
{
return $this->getQuote()->getStore()->formatPrice($price); //this is where i want to put my javascript result in (the recalculated price)
}
我尝试在 PHP 中插入我的 product.js 文件,如 <script type="text/javascript" src="product.js"></script>
但似乎不起作用。
最佳答案
这里 Data.php 中的 formatPrice() 是辅助函数。在 magento 辅助函数中,您可以从任何地方调用。但如果你包含任何文件,它将不会被调用。您可以创建方法并从任何模块调用它。
如果您想包含product.js文件,请转到/app/design/frontend/[packagename]/[theme]/layout/page.xml并在head block 中添加此行。
<action method="addJs"><script>product.js</script></action>
将你的js文件上传到这个路径[magento]/js/product.js并从后端刷新缓存并检查它。
关于javascript - Magento 中的调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29250942/