Javascript 在Magento的PHP函数中使用include方法
让我们保持简单,这样我的代码就可以在View.phtml中显示javascript元素Javascript 在Magento的PHP函数中使用include方法,javascript,php,jquery,json,magento,Javascript,Php,Jquery,Json,Magento,让我们保持简单,这样我的代码就可以在View.phtml中显示javascript元素 $Abowl = '<p id="finalprice"></p>'; echo $Abowl; 更新1: 我试过这样的方法: public function formatPrice($price) { require_once(Mage::getBaseDir() . '\app\design\frontend\neighborhood\default\t
$Abowl = '<p id="finalprice"></p>';
echo $Abowl;
更新1:
我试过这样的方法:
public function formatPrice($price)
{
require_once(Mage::getBaseDir() . '\app\design\frontend\neighborhood\default\template\catalog\product\view.phtml');
return $this->getQuote()->getStore()->formatPrice($Abowl); //this is the source
}
更新2:我问了一个问题,上面说DATA.php在这里不接受任何外包变量:
这对我有影响吗
更新3:为了满足客户的要求,我修改了\magento\js\varien\product.js下的函数。它工作良好,折扣将适用于用户信息从我们的数据库。
但当客户将商品添加到购物车时,它只显示原始价格,在本例中为$549.47
因此,我还想将javascript结果传递给DATA.php,因为它包含应用于购物车部分的formatPrice函数
下面是我的部分javascript代码(\magento\js\varien\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); // use the var factor && this will affect the price when changing option *important
subPriceincludeTax += parseFloat(el.includeTax);
} 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 = "<small>Your price is </small>"+ "$" + fomattedprice + "*" +"<br/><small><em>*Discount will be applied during check out</em></small>";
});
您可以通过以下代码执行此操作
require_once(Mage::getBaseDir() . 'path to your file');
Mage::getBaseDir()这将为您提供保存此文件的magento基本目录url?View.phtml存储在:\magento\app\design\frontend\Neighbour\default\template\catalog\product\View.phtmlDATA.php存储在:\magento\app\code\core\Mage\Checkout\Helper\DATA.phpit应该像require\u一样(Mage::getBaseDir()“.”/app\design\frontend\neighborary\default\template\catalog\product\view。phtml');有同样的错误,甚至复制和修改你编辑的代码,我认为你的代码是正确的,但似乎它不能解决我的问题。附言:我已经投票给你了,你能告诉我你在另一个文件中包含了整个产品视图文件吗?我更新了一些附加信息,请看一下,你可以忽略一些不必要的信息。非常感谢。
<?php
//SQL to identify ones email and discount
$prices = Mage::helper('core')->currency($_product->getPrice(), $formatPrice, $html);
echo $this->getChildHtml('product_type_data');
if(Mage::getSingleton('customer/session')->isLoggedIn()) {
$customerData = Mage::getSingleton('customer/session')->getCustomer();
$conn = Mage::getModel('core/resource')->getConnection('core_read');
//need to change this sql statment to other table, took up and sl.is_active = 1 for test only
//it won't work by now because method is changed and not connecting to the right DB table
$sql = 'select sl.discount_amount, scg.customer_group_id, sl.stop_rules_processing 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().'"
order by sl.discount_amount desc';
//and sl.is_active = 1
//'select 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);
$Abowl = '<p id="finalprice"></p>';
echo $Abowl; //only applied when not logged in
$discountRate = 1;
foreach ($results as $result)
{
$currentRate = $result['discount_amount'];
//print_r($currentRate);
$discountRate = $discountRate*(100 - $currentRate)/100;
if($result['stop_rules_processing']==1){
break;
}
}
} ?>
require_once(Mage::getBaseDir() . 'path to your file');