Reputation: 1145
I know the way to pass a JavaScript variable/function is similar to standard JavaScript in Magento but since this is the first time i am develop a e-commerce system i want to make everything clear to prevent ask duplicate question in the future.
So I have a function in the Product.js which wrote by myself:
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 ;
});
So basically, it's a function for me recalculate the price then return it by this line of code document.getElementById("finalprice").innerHTML = '$' + fomattedprice ;
I am now wondering how to put this result in the another php file names 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)
}
I try to insert my product.js file in the PHP like <script type="text/javascript" src="product.js"></script>
but it doesn't seem working.
Upvotes: 1
Views: 1470
Reputation: 624
Here formatPrice() which is in Data.php is helper function. in magento helper function you can call from anywhere. but if you include any file it will be not called. you can create methods and call it from any module.
if you want to include product.js file then go to /app/design/frontend/[packagename]/[theme]/layout/page.xml and add this line in head block.
<action method="addJs"><script>product.js</script></action>
upload your js file in this path [magento]/js/product.js and refresh cache from backend and check it.
Upvotes: 1