2015-03-25 83 views
1

我知道傳遞JavaScript變量/函數的方式與Magento中的標準JavaScript類似,但由於這是我第一次開發電子商務系統,我希望將所有內容都清除以防止出現重複未來的問題。Magento中的調用函數

所以我在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>,但它似乎不起作用。

回答

1

這裏在Data.php中的formatPrice()是helper函數。在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和後臺刷新緩存和檢查。

+0

感謝您的信息,但我還有一個問題;我怎麼能進入'$ this-> getQuote() - > getStore() - > formatPrice($ price);',我想看看我是否也能夠修改$ price。 – anson920520 2015-03-26 01:41:26

+0

您可以通過覆蓋幫助器方法formatprice更改幫助函數。 – 2015-03-26 21:05:35