【问题标题】:Custom Tier Price not working in checkout page magento自定义等级价格在结帐页面 magento 中不起作用
【发布时间】:2012-12-20 17:57:17
【问题描述】:

我使用 Alan Storms 教程在 magento 中创建模块,开发了一个自定义模块来满足我的项目要求。

我需要根据实时源在前端动态更改价格属性。 Feed 每秒都会更新一次,因此每次页面刷新时,网站上的每种产品都必须显示一个新价格。

为此,我已经覆盖了产品模块和价格模块。问题在于分级定价。当分级定价到位时,我需要根据实时价格计算分级价格。

为此,我还设法使用 price_type 类覆盖进行了更改。

现在,每当将商品添加到购物车时,分层定价不起作用,我编写了 event_trigger 即一个观察者,它更新事件“checkout_cart_save_before”上的 tier_pricing,这是我的代码

class My_Custom_Model_Observer extends Varien_Event_Observer
{
   public function __construct()
   {
   }
   public function updateCartBasedOnLiveFeed($observer)
   {

          foreach ($observer->getCart()->getQuote()->getAllVisibleItems() as $item /* @var $item Mage_Sales_Model_Quote_Item */) 
          {
                        $tierPrices =   array();
                        $tierPrices =   $item->getProduct()->getTierPrice();
                        $itemPrice  =   $item->getProduct()->getPrice();
                        $i=0;
                        foreach($tierPrices as $key => $tierPrice)
                        {

                            if(!is_numeric($key))
                            {

                                    $updatedTierPrice                   =   $itemPrice - ($itemPrice * ($tierPrice['price']/100));
                                    $tierPrices[$key]['price']          =   $updatedTierPrice;
                                    $tierPrices[$key]['website_price']  =   $updatedTierPrice;  
                            }   
                            else 
                            {
                                if($tierPrice['price'] > 0)
                                {
                                    $updatedTierPrice                   =   $itemPrice - ($itemPrice * ($tierPrice['price']/100));
                                    $tierPrice['price']                 =   $updatedTierPrice;
                                    $tierPrice['website_price']         =   $updatedTierPrice;
                                    $tierPrices[$i]                     =   $tierPrice;
                                    $i++;   
                                }

                            }

                        }

                        $item->getProduct()->setData('tier_price',$tierPrices);

          }

   }
}

上面的代码在购物车页面中效果很好。但是当谈到结帐页面时。它适用于单个商品,并且当分级定价发挥作用时,它会应用购物车价格。

请帮帮我。

我还尝试使用其他事件以及上述事件。

事件:sales_quote_save_before

public function updateQuoteLive($observer)
   {
        $tierPrices =   array();

        $quote_item = $observer->getEvent()->getQuote;

    $itemPrice  =   $quote_item->getProduct()->getPrice();
        $tierPrices =   $quote_item->getProduct()->getTierPrice();

        $tierPricesSize =   sizeof($tierPrices);
        for($i=0;$i<$tierPricesSize;$i++)
        {
            $updatedTierPrice                   =   $itemPrice - ($itemPrice * ($tierPrices[$i]['price']/100));
            $tierPrices[$i]['price']            =   $updatedTierPrice;
            $tierPrices[$i]['website_price']    =   $updatedTierPrice;
        }

        $quote_item->getProduct()->setData('tier_price',$tierPrices);



   }

当我尝试打印 Quote.php 中可用的 getQuote() 函数时,我发现那里的层价格不是我使用第一个事件更新的价格。所以我想我需要在保存报价之前更新价格。请任何人帮助我并指出正确的方向。

请帮我解决这个问题,我错过了一些重要的步骤。非常感谢任何帮助。

提前致谢。

【问题讨论】:

    标签: magento


    【解决方案1】:

    在更新时将新价格“保存”到数据库中可能会更好。

    尝试以下方式:

    $product = $observer->getProduct();
    $procuct->setPrice($updatedPrice);
    

    这样,在结账时,它将从数据库中提取正确的价格(并避免在“飞行途中”更正它的麻烦

    【讨论】:

    • 感谢您的回复 pzirkind.. 但是让我明确一点,问题不在于价格。但它与“tier_price”有关。如果我添加数量为 1 的项目,它工作正常。
    • 但是当我们将产品的数量增加到 10 件、20 件等时,我们将不得不再次根据使用的实时提要和管理员输入的值来计算等级价格值。我们需要对这两个值进行倍增并更新层级定价。
    【解决方案2】:

    我意识到像你这样的项目。我没有 sales_quote_save_before Observer。我只使用 checkout_cart_save_before。将根据会话设置价格。

    我是这样意识到的:

    public function updatePrice( $observer )
    {   
    try {
           $cart = $observer->getCart();
           $items = $cart->getItems();
           foreach($items as $item)
           {
           $item->setCustomPrice($price);
           $item->setOriginalCustomPrice($price);
           }
         } catch ( Exception $e )
         {
           Mage::log( "checkout_cart_save_before: " . $e->getMessage() );
         }
    }
    

    我使用这个观察者即时计算等级价格。所有价格都将在 qoute 中正确设置。

    也许你应该试试这个方法。

    问候博蒂

    【讨论】:

      【解决方案3】:

      终于找到了问题所在并得到了解决方案。

      问题是在购物车页面或结帐页面中调用getTierPrice() function 时,它存在于/app/code/core/Mage/Catalog/Product.php 中。它采用一个名为 $qty 的参数,默认为空。此函数依次调用/app/code/core/Mage/Type/Price.php 文件中的function getTierPrice,该文件接受两个参数$qty and $productObject. 默认情况下$qty 为null,当它为null 时,函数返回tier_prices 数组。但是,当传递 $qty 值时,该函数会为该特定数量返回单个值。

      所以,我编写了自己的自定义函数,它根据我的要求计算等级价格,而不是像

      我按照 Alan Storm 的教程使用自定义模块覆盖了两个核心文件。

      我用 My_CustomModule_Model_Product 类扩展了 Mage_Catalog_Model_Product 和

      然后

      Mage_Catalog_Model_Product_Type_Price 和 My_CustomModule_Model_Price

      然后在 /app/code/local/My/Custommodule/Model/Product.php 中

      我添加了我的自定义代码,例如

      public function getTierPrice($qty=null)
          {
              if($qty)
              {
                  return  $this->getPriceModel()->getCustomTierPrice($qty, $this); 
              }
              return $this->getPriceModel()->getTierPrice($qty, $this);
          }
      

      然后在 /app/code/local/My/Custommodule/Model/Price.php 中

      <?php 
      
      public function getCustomTierPrice($qty = null, $product)
          {
              $allGroups = Mage_Customer_Model_Group::CUST_GROUP_ALL;
              $prices = $product->getData('tier_price');
      
      
              if (is_null($prices)) {
                  $attribute = $product->getResource()->getAttribute('tier_price');
                  if ($attribute) {
                      $attribute->getBackend()->afterLoad($product);
                      $prices = $product->getData('tier_price');
                  }
              }
      
      
       foreach($prices as $key => $customPrices)
              {
                  if($prices[$key]['price'] < 1)
                  {
                          $prices[$key]['price']          =   abs($product->getPrice() - ($productPrice * ($customPrices['price']/100)));
                          $prices[$key]['website_price']  =   $prices[$key]['price'];
                  }
      
              }
      }
      
      which retured a customized value when $qty is passed and voila it worked.
      
      I just posed this answer so that any one else who has similar requirement may get benefited with this.
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 2013-03-09
        • 1970-01-01
        • 1970-01-01
        • 2014-02-05
        • 1970-01-01
        • 2015-08-26
        • 1970-01-01
        • 1970-01-01
        • 2016-01-15
        相关资源
        最近更新 更多