簡體   English   中英

更新報價項目的權重

[英]Update the weight of a quote item

當我在magento的購物車中添加產品時,我想更新報價項目的重量。

我嘗試了這段代碼:

public function checkoutCartProductAddAfter(Varien_Event_Observer $observer)
{
   $event = $observer->getEvent();
   $quote_item = $event->getQuoteItem();
   $myWeight = ($quote_item->getWeight()/100)*$number;
   $quote_item->setWeight($myWeight);
   $quote_item->save();
}

但這不起作用。 我在相同的功能中更新了報價項目的價格,並且可以正常工作。

為什么我的新體重不適用?

使用sales_quote_item_set_product事件。 報價項目設置其產品后,將在每次請求時調用此事件。 這樣可以將價格和權重更改為自定義值。

/*
 * Called on every request, updates item after loaded from session
 */
public function updateItem(Varien_Event_Observer $observer) 
{
    $item = $observer->getQuoteItem();
    if ($item->getParentItem()) {
        $item = $item->getParentItem();
    }

    // set price
    $item->setCustomPrice($customPrice);
    $item->setOriginalCustomPrice($customPrice);
    $item->getProduct()->setIsSuperMode(true);

    // set weight
    $item->setWeight($customWeight);
}

希望能幫助到你 :)

您需要保存報價單或購物車而不是報價單項目。 嘗試:

$quote_item->getQuote()->save();

要么

Mage::getSingleton('checkout/cart')->save();

您正在觀察正確的事件

checkout_cart_product_add_after

無需這樣做,您可以從函數中刪除它

$quote_item->save();

但添加這個

$quote_item->getProduct()->setIsSuperMode(true);

現在,您的函數應顯示為

public function checkoutCartProductAddAfter(Varien_Event_Observer $observer)
{  
   $event = $observer->getEvent();
   $quote_item = $event->getQuoteItem();
   $myWeight = ($quote_item->getWeight()/100)*$number;

   $quote_item->getProduct()->setIsSuperMode(true);

   $quote_item->setWeight($myWeight);
}

為了不將原始產品重量寫入表sales_flat_quote_item ,需要做的主要事情是將item.php文件從app/code/core/Mage/Sales/Model/Quote復制到app/code/local/Mage/Sales/Model/Quote並刪除以下行(v1.9 CE)行#390

->setWeight($this->getProduct()->getWeight())

現在它應該像魅力一樣工作。 嘗試和測試

您可以在這里嘗試我的代碼。 在sidebar.phtml中

<?php
$items = Mage::getSingleton('checkout/cart')->getQuote()
    ->getItemsCollection()->getItems();
$product = Mage::getModel('catalog/product');
$total_weight = 0;
foreach ($items as $item) {
    $product = $item->getProduct();
    $qty = $item->getQty();
    $weight = $item->getWeight();
    if ($product->isConfigurable()) {
        $total_weight += ($weight * ($qty - 1));
    } else {
        $total_weight += ($weight * $qty);
    };
}
?>


<div><strong>Total Weight</strong></div>
</div><strong><?php echo $total_weight;?> KG</strong></div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM