问题描述
我想以编程方式将产品添加到购物车.另外,我想在添加到购物车时更改产品价格.
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);
}
}
}
这篇关于以编程方式将产品添加到购物车并更改价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!