Magento 中的调用函数

Calling function in Magento

我知道传递 JavaScript variable/function 的方法与 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 ;
          });   

所以基本上,它是我重新计算价格的函数,然后 return 通过这行代码 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)
}

我尝试将我的 product.js 文件插入到 PHP 中,就像 <script type="text/javascript" src="product.js"></script> 但它似乎不起作用。

这里 Data.php 中的 formatPrice() 是辅助函数。在 magento 辅助功能中,您可以从任何地方调用。但如果您包含任何文件,它将不会被调用。您可以创建方法并从任何模块调用它。

如果你想包含 product.js 文件然后转到 /app/design/frontend/[packagename]/[theme]/layout/page.xml 并在 head 块中添加这一行.

<action method="addJs"><script>product.js</script></action>

上传你的js文件到这个路径[magento]/js/product.js并从后端刷新缓存并检查它。