0

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

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

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

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

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

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() 函数时,我发现那里的层价格不是我使用第一个事件更新的价格。所以我想我需要在保存报价之前更新价格。请任何人帮助我并显示正确的方向。

请帮我解决这个问题,我错过了一些重要的步骤。任何帮助是极大的赞赏。

提前致谢。

4

3 回答 3

0

我实现了像你这样的项目。我没有 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() );
     }
}

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

也许你应该尝试这种方式。

问候博蒂

于 2012-12-21T07:51:24.533 回答
0

终于弄清楚了问题并得到了解决方案。

问题是在购物车页面或结帐页面getTierPrice() function中调用 ,它存在于/app/code/core/Mage/Catalog/Product.php. 它采用一个名为 $qty 的参数,默认为空。该函数依次调用文件中的function getTierPricewhich/app/code/core/Mage/Type/Price.php有两个参数$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.

?>
于 2012-12-24T06:38:48.807 回答
0

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

尝试以下方式:

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

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

于 2012-12-20T22:14:01.980 回答