使用observer Magento更改所有页面上的产品价格

使用observer Magento更改所有页面上的产品价格,magento,events,observers,Magento,Events,Observers,我正在学习Magento,我已经开始学习事件和观察者。通过在“管理产品”区域中设置,将金额的百分比添加到产品中。 它工作正常,但折扣价格仅显示在产品页面上。有人能建议我怎样通过Magento改变价格吗。我的意思是改变价格应该去购物车,订单等 下面是观察员的代码 <?php class Xyz_Catalog_Model_Price_Observer { public function __construct() { } /** * Applies

我正在学习Magento,我已经开始学习事件和观察者。通过在“管理产品”区域中设置,将金额的百分比添加到产品中。 它工作正常,但折扣价格仅显示在产品页面上。有人能建议我怎样通过Magento改变价格吗。我的意思是改变价格应该去购物车,订单等

下面是观察员的代码

<?php
class Xyz_Catalog_Model_Price_Observer
{
    public function __construct()
    {
    }
    /**
     * Applies the special price percentage discount
     * @param   Varien_Event_Observer $observer
     * @return  Xyz_Catalog_Model_Price_Observer
     */
    public function apply_discount_percent($observer)
    {
      $event = $observer->getEvent();
      $product = $event->getProduct();   
      // process percentage discounts only for simple products     
      if ($product->getSuperProduct() && $product->getSuperProduct()->isConfigurable()) {
      } else {
        $percentDiscount = $product->getPercentDiscount();

        if (is_numeric($percentDiscount)) {
          $today = floor(time()/86400)*86400;
          $from = floor(strtotime($product->getSpecialFromDate())/86400)*86400;
          $to = floor(strtotime($product->getSpecialToDate())/86400)*86400;

          if ($product->getSpecialFromDate() && $today < $from) {
          } elseif ($product->getSpecialToDate() && $today > $to) {
          } else {
            $price = $product->getPrice();
            $finalPriceNow = $product->getData('final_price');

            $specialPrice = $price - $price * $percentDiscount / 100;

            // if special price is negative - negate the discount - this may be a mistake in data
            if ($specialPrice < 0)
              $specialPrice = $finalPriceNow;

            if ($specialPrice < $finalPriceNow)
              $product->setFinalPrice($specialPrice); // set the product final price
          }
        }   
      }         
      return $this;
    }
}

  • 为什么不使用产品的“目录价格规则”或“特殊价格”功能?这些是做这类事情的内置函数

  • 对于“添加到购物车”价格更改,您需要观察另一个事件。速成班:

  • 您必须构建一个观察者来捕获添加到购物车事件
    sales\u quote\u add\u item
    ,然后您可以像在产品页面上那样在观察者中执行php操作,以更改添加到卡中的产品的价格:

    $observer->getEvent()->getQuoteItem()->setOriginalCustomPrice([your price])
    

    对于此任务,最好使用
    促销
    菜单中的
    目录价格规则

    但据我所知,你这样做是为了学习。因此,当您不确定某个事件时,可以只记录已调度的事件。在
    app/Mage.php
    中有一个名为
    dispatchEvent
    的方法。然后,您可以只记录每个已调度事件:

    public static function dispatchEvent($name, array $data = array())
    {
        if(strpos($name, 'product') || strpos($name, 'price')) { // optional
            Mage::log($name, null, 'events.log', true);
        }
        Varien_Profiler::start('DISPATCH EVENT:'.$name);
        $result = self::app()->dispatchEvent($name, $data);
        Varien_Profiler::stop('DISPATCH EVENT:'.$name);
        return $result;
    }
    
    您的日志文件将在
    var/log
    文件夹中创建。当然,完成后不要忘记将
    Mage.php
    还原为原始版本。更改核心文件不是一个好主意

    这是解决方案

    config.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <Seta_DiscountPrice>
                <version>0.1.0</version>    <!-- Version number of your module -->
            </Seta_DiscountPrice>
        </modules>
        <global>
            <models>
                <setadiscountprice>
                    <class>Seta_DiscountPrice_Model</class>
                </setadiscountprice>
            </models>
            <events>
            <catalog_product_get_final_price>
                <observers>
                    <seta_discountprice_price_observer>
                        <type>singleton</type>
                        <class>Seta_DiscountPrice_Model_Price_Observer</class>
                        <method>apply_10</method>
                    </seta_discountprice_price_observer>
                </observers>
            </catalog_product_get_final_price>
                <catalog_product_collection_load_after>
                    <observers>
                        <seta_discountprice_price_observer>
                            <type>singleton</type>
                            <class>Seta_DiscountPrice_Model_Price_Observer</class>
                            <method>apply_view</method>
                        </seta_discountprice_price_observer>
                    </observers>
                </catalog_product_collection_load_after>
            </events>
        </global>
    </config>
    
    
    0.1.0    
    Seta\u折扣价格\u模型
    独生子女
    Seta\u折扣价格\u模型\u价格\u观察者
    申请10
    独生子女
    Seta\u折扣价格\u模型\u价格\u观察者
    应用视图
    
    Observer.php

    <?php
    
    class Seta_DiscountPrice_Model_Price_Observer
    {
        public function __construct()
        {
        }
        /**
         * Applies the special price percentage discount
         * @param   Varien_Event_Observer $observer
         * @return  Seta_DiscountPrice_Model_Price_Observer
         */
    public function apply_10($observer)
    {
        $event = $observer->getEvent();
        $product = $event->getProduct();
        // process percentage discounts only for simple products
        if ($product->getSuperProduct() && $product->getSuperProduct()->isConfigurable()) {
        } else {
    
            $product->setFinalPrice(10);
        }
        return $this;
    }
        public function apply_view($observer)
        {
            $event = $observer->getEvent();
            $myCustomPrice = 10;
            $products = $observer->getCollection();
    
            foreach( $products as $product )
            {
                $product->setPrice( $myCustomPrice );
                $product->setFinalPrice( $myCustomPrice );
            }
            return $this;
        }
    }
    

    我的回答有助于解决您的问题吗?你修好了吗?@dushyant如果我觉得这个问题解决得太快了,我无法接受。re:选项1,如果价格修改涉及十进制精度>4,这是不可能的,因为MySQL价格字段类型设置为
    decimal(12,4)
    这个答案确实为我节省了很多时间!这适用于所有产品我们可以为特别选择的产品做什么?是的。此解决方案适用于简单产品。您可以在代码“仅处理简单产品的百分比折扣”上方看到我的注释,我仅使用简单产品设置。
    <?php
    
    class Seta_DiscountPrice_Model_Price_Observer
    {
        public function __construct()
        {
        }
        /**
         * Applies the special price percentage discount
         * @param   Varien_Event_Observer $observer
         * @return  Seta_DiscountPrice_Model_Price_Observer
         */
    public function apply_10($observer)
    {
        $event = $observer->getEvent();
        $product = $event->getProduct();
        // process percentage discounts only for simple products
        if ($product->getSuperProduct() && $product->getSuperProduct()->isConfigurable()) {
        } else {
    
            $product->setFinalPrice(10);
        }
        return $this;
    }
        public function apply_view($observer)
        {
            $event = $observer->getEvent();
            $myCustomPrice = 10;
            $products = $observer->getCollection();
    
            foreach( $products as $product )
            {
                $product->setPrice( $myCustomPrice );
                $product->setFinalPrice( $myCustomPrice );
            }
            return $this;
        }
    }