javascript - 在 Magento 中的 PHP 函数中使用 include 方法

标签 javascript php jquery json magento

让我们保持简单,这样我就可以在 View.phtml 中使用我的代码来显示 javascript 元素

    $Abowl = '<p id="finalprice"></p>';
    echo $Abowl; 

现在我想将其传递给另一个 PHP 名称的 DATA.php,我尝试过,但它不起作用

 public function formatPrice($price)
{
    include'view.phtml';
    return $this->getQuote()->getStore()->formatPrice($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不接受这里的任何外包变量:How can I get access to modify this price function in Magento 对我有影响吗?

更新3:我修改了\magento\js\varien\product.js下的函数以满足客户的要求。效果很好,折扣将根据我们数据库中的用户信息而适用。 enter image description here

但是当客户将商品添加到购物车时,它只显示原价,在本例中为 549.47 美元。

enter image description here

因此,我也想将 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>";
          });

您可以忽略这些代码,我只是想知道我应该将我的 document.getElementById("finalprice"); 结果传递到哪里才能显示折扣价格。

以下是我的view.html供引用:

  <?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');

Mage::getBaseDir() 这将为您提供 magento 基本目录 url

关于javascript - 在 Magento 中的 PHP 函数中使用 include 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29272748/

相关文章:

PHP 将文件读入数组

php - 在没有找到记录时插入数据,否则使用 PHP Mysql 使用最后插入的 ID 更新前一条记录

javascript - 使用 jquery、css 创建自定义确认框

javascript - jQuery UI 日期选择器无法正常工作

javascript - 如何删除部分用户输入的值并将新值附加到输入框

JavaScript 随机数一/零实现

javascript - 从 Chrome 扩展程序将图像上传到 Picasa

javascript - Istanbul 尔:使用 mocha 生成代码 cobertura 报告

php - Laravel 5.1 auth 刷新后注销

javascript - 获取没有提交按钮的选定单选按钮