問題描述
我想以編程方式將產品添加到購物車.另外,我想在添加到購物車時更改產品價格.
I want to add a product to cart programmatically. Also, I want to change the product price when added to cart.
假設我的產品價格是 100 美元.我想在添加到購物車時將其更改為 90 美元.
Suppose, my product's price is $100. I wanted to change it to $90 when added to cart.
我將產品添加到購物車.但是,我無法更改產品價格.
I added product to cart. However, I am unable to change the product price.
有可能嗎?
這是將產品添加到購物車的代碼:-
Here is the code to add product to cart:-
$cart = Mage::getSingleton('checkout/cart');
try {
$cart->addProduct($product, array('qty' => 1));
$cart->save();
}
catch (Exception $ex) {
echo $ex->getMessage();
}
推薦答案
深入 Magento 的核心代碼后,我發現你需要使用 $item->getProduct()->setIsSuperMode(true)
以使 $item->setCustomPrice()
和 $item->setOriginalPrice()
工作.
After digging a bit into Magento's core code, I found that you need to use $item->getProduct()->setIsSuperMode(true)
in order to make $item->setCustomPrice()
and $item->setOriginalPrice()
work.
這里有一些示例代碼,您可以在監聽 checkout_cart_product_add_after
或 checkout_cart_update_items_after
事件的觀察者中使用.代碼在邏輯上是相同的,除了 checkout_cart_product_add_after
只為一件商品調用,checkout_cart_update_items_after
為購物車中的所有商品調用.此代碼僅作為示例被分離/復制為 2 個方法.
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.
/**
* @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);
}
}
}
這篇關于以編程方式將產品添加到購物車并更改價格的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!