如何在 Magento 中使用 getQuoteItem() 将可配置产品添加到购物车中

发布于 2025-01-04 17:22:10 字数 586 浏览 0 评论 0原文

我想为所有类型的产品设置自定义价格。我正在听观察者 checkout_cart_product_add_after 的声音。在它的函数中,我使用以下代码来设置产品的自定义价格。

$newPrice  = $_POST["txtprice"];
$event = $observer->getEvent();
$minPrice = $observer->getProduct()->getMinPrice();
$quoteItem = $event->getQuoteItem();

if($newPrice > $minPrice){          
$quoteItem->setCustomPrice($newPrice);              
$quoteItem->setOriginalCustomPrice($newPrice);
    $quoteItem->getProduct()->setIsSuperMode(true);
}

该代码适用于简单的产品。对于可配置产品,它不起作用。购物车的可配置项目不会设置在 $quoteItem 对象中。因此我无法使用 $quoteItem 设置自定义价格。

I want to set custom price for all types of products. I am listening to the observer checkout_cart_product_add_after. In it's function I am using the following code to set custom price for products.

$newPrice  = $_POST["txtprice"];
$event = $observer->getEvent();
$minPrice = $observer->getProduct()->getMinPrice();
$quoteItem = $event->getQuoteItem();

if($newPrice > $minPrice){          
$quoteItem->setCustomPrice($newPrice);              
$quoteItem->setOriginalCustomPrice($newPrice);
    $quoteItem->getProduct()->setIsSuperMode(true);
}

This code works fine for simple products. For configurable products it is not working. A configurable item of the cart doesn't get set int the $quoteItem object. And so I can't get the custom price to set using $quoteItem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日久见人心 2025-01-11 17:22:10

请参阅我在此处编辑的答案

这里是一些示例代码,您可以在侦听的观察者中使用
对于 checkout_cart_product_add_after
checkout_cart_update_items_after 事件。代码逻辑上是
相同,除了 checkout_cart_product_add_after 仅调用一个
item 和 checkout_cart_update_items_after 为所有项目调用
购物车。此代码被分离/复制为 2 个方法,仅作为
示例。

对于可配置产品,您需要检查 $item->getParentItem(),如该答案中的示例代码所示:

事件:checkout_cart_product_add_after

/**
 * @param Varien_Event_Observer $observer
 */
public function applyDiscount(Varien_Event_Observer $observer)
{
    /* @var $item Mage_Sales_Model_Quote_Item */
    $item = $observer->getQuoteItem();
    if ($item->getParentItem()) {
        $item = $item->getParentItem();
    }

    // Discounted 25% off
    $percentDiscount = 0.25; 

    // This makes sure the discount isn't applied over and over when refreshing
    $specialPrice = $item->getOriginalPrice() - ($item->getOriginalPrice() * $percentDiscount);

    // Make sure we don't have a negative
    if ($specialPrice > 0) {
        $item->setCustomPrice($specialPrice);
        $item->setOriginalCustomPrice($specialPrice);
        $item->getProduct()->setIsSuperMode(true);
    }
}

事件:checkout_cart_update_items_after

/**
 * @param Varien_Event_Observer $observer
 */
public function applyDiscounts(Varien_Event_Observer $observer)
{
    foreach ($observer->getCart()->getQuote()->getAllVisibleItems() as $item /* @var $item Mage_Sales_Model_Quote_Item */) {
         if ($item->getParentItem()) {
             $item = $item->getParentItem();
         }

         // Discounted 25% off
         $percentDiscount = 0.25; 

         // This makes sure the discount isn't applied over and over when refreshing
         $specialPrice = $item->getOriginalPrice() - ($item->getOriginalPrice() * $percentDiscount);

         // Make sure we don't have a negative
         if ($specialPrice > 0) {
             $item->setCustomPrice($specialPrice);
             $item->setOriginalCustomPrice($specialPrice);
             $item->getProduct()->setIsSuperMode(true);
         }
    }
}

See the answer I've edited here:

Here is some sample code you can use within an Observer that listens
for the checkout_cart_product_add_after or
checkout_cart_update_items_after events. The code is logically the
same except checkout_cart_product_add_after is called for only one
item and checkout_cart_update_items_after is called for all items in
the cart. This code is separated/duplicated into 2 methods only as an
example.

For configurable products, you need to check for $item->getParentItem() as in the sample code from that answer:

Event: checkout_cart_product_add_after

/**
 * @param Varien_Event_Observer $observer
 */
public function applyDiscount(Varien_Event_Observer $observer)
{
    /* @var $item Mage_Sales_Model_Quote_Item */
    $item = $observer->getQuoteItem();
    if ($item->getParentItem()) {
        $item = $item->getParentItem();
    }

    // Discounted 25% off
    $percentDiscount = 0.25; 

    // This makes sure the discount isn't applied over and over when refreshing
    $specialPrice = $item->getOriginalPrice() - ($item->getOriginalPrice() * $percentDiscount);

    // Make sure we don't have a negative
    if ($specialPrice > 0) {
        $item->setCustomPrice($specialPrice);
        $item->setOriginalCustomPrice($specialPrice);
        $item->getProduct()->setIsSuperMode(true);
    }
}

Event: checkout_cart_update_items_after

/**
 * @param Varien_Event_Observer $observer
 */
public function applyDiscounts(Varien_Event_Observer $observer)
{
    foreach ($observer->getCart()->getQuote()->getAllVisibleItems() as $item /* @var $item Mage_Sales_Model_Quote_Item */) {
         if ($item->getParentItem()) {
             $item = $item->getParentItem();
         }

         // Discounted 25% off
         $percentDiscount = 0.25; 

         // This makes sure the discount isn't applied over and over when refreshing
         $specialPrice = $item->getOriginalPrice() - ($item->getOriginalPrice() * $percentDiscount);

         // Make sure we don't have a negative
         if ($specialPrice > 0) {
             $item->setCustomPrice($specialPrice);
             $item->setOriginalCustomPrice($specialPrice);
             $item->getProduct()->setIsSuperMode(true);
         }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文