簡體   English   中英

Magento 1.9.x:如何在以編程方式將商品添加到購物車時添加額外費用?

[英]Magento 1.9.x: How to add an extra fee to order while adding item to cart programmatically?

我也在這里問過https://magento.stackexchange.com/questions/167982/magento-1-9-x-how-to-add-an-extra-fee-to-order-while-adding-item-to- cart-progra但是沒有得到任何答案,所以我在這里問。

我將以編程方式將項目添加到購物車(下面的代碼)中,並希望在訂購時添加額外費用。 我在這里發現了很多結果,但由於我是magento的新手,因此無法遵循。 任何人都可以分享創建文件的位置(觀察者,帶有路徑的config.xml等)以及該文件的名稱應該是什么? 下面的代碼是將商品成功添加到購物車,但我無法為完成訂單添加費用。

任何幫助都會非常明顯。

提前致謝。 我的代碼是:

$productId = 13114;
$price_extra = 50.00;
$product_model = Mage::getModel('catalog/product');
$code = Mage::app()->getStore()->getCode();
$_product = Mage::getModel('catalog/product')->load($productId);
$_product->setMinimalPrice($price_extra);

$cart = Mage::getSingleton('checkout/cart');
$cart->init();
$params = array(
    'product' => $productId,
    'related_product' => null,
    'qty' => 1,
    'options' => array(
        2 => array('7', '8', '9', '10', '11', '12'),
        1 => array('1', '2', '3', '4', '5', '6'),
    )
);
$customer = Mage::getSingleton('customer/session')->getCustomer();
$storeId = $customer->getStoreId();

$cart->addProduct($_product, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

加載產品后,您可以設置該產品的價格,替換此行

$_product->setMinimalPrice($price_extra);

有了這個

$_product->setPrice($_product->getPrice()+$price_extra);

您需要使用事件觀察器設置自定義價格

使用此活動。 sales_quote_add_item 1. config.xml

<前端>
<活動>
<sales_quote_add_item>
<觀察員>
<mymodule_observer>
<類型>單</類型>
<類> MyCompany_MyModule_Model_Observer </類>
<方法> updateCartPrice </方法>
</ mymodule_observer>
</觀察員>
</ sales_quote_add_item>
</事件>
</前端>

  1. Observer.php

class MyCompany_Mymodule_Model_Observer {

public function updateCartPrice(Varien_Event_Observer $observer) {
    $event = $observer->getEvent();
    $quoteItem = $event->getQuoteItem();
    $product = $item->getProduct();

    $extraPrice = 100;
    $customPrice = $product->getFinalPrice() + $extraPrice;
    $quoteItem->setOriginalCustomPrice($customPrice);
    $quoteItem->setCustomPrice($customPrice);
    $quoteItem->save();
}

}

嘗試這個。 它會對你有用。

暫無
暫無

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

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