【问题标题】:Using include method in PHP function in Magento在 Magento 的 PHP 函数中使用 include 方法
【发布时间】:2015-03-26 07:04:12
【问题描述】:

让我们保持简单,所以我在 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 对我有影响吗?

UPDATE 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>";
          });

你可以忽略这些代码,我只想知道我应该在哪里传递我的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;
            }

        }

        }   ?>

【问题讨论】:

  • 你把这个文件保存在哪里?
  • View.phtml 存储在:\magento\app\design\frontend\neighborhood\default\template\catalog\product\view.phtml
  • DATA.php 存储在:\magento\app\code\core\Mage\Checkout\Helper\DATA.php
  • 应该是 require_once(Mage::getBaseDir() . ''/app\design\frontend\neighborhood\default\template\catalog\product\view.‌​phtml');跨度>

标签: javascript php jquery json magento


【解决方案1】:

您可以通过以下代码做到这一点

require_once(Mage::getBaseDir() . 'path to your file');

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

【讨论】:

  • 即使复制和粘贴您编辑的代码也有同样的错误,我认为您的代码是正确的,但似乎无法解决我的问题。 P.S:我已经投票给你了
  • 你能告诉我你将整个产品视图文件包含在另一个文件中吗?
  • 我更新了一些额外的信息,请看一下,你可以忽略一些不必要的信息。谢谢!
猜你喜欢
  • 2013-03-11
  • 2011-12-16
  • 2014-12-20
  • 1970-01-01
  • 2014-04-29
  • 2017-05-05
  • 2018-01-06
  • 2013-02-14
  • 1970-01-01
相关资源
最近更新 更多