我需要获取在相关产品中列出指定产品的所有产品。因此,在移除主产品之后,我将尝试从购物车中移除不必要的物品。
有没有一种更简单的方法来循环所有购物车项目?
谢谢你的建议。
发布于 2012-10-28 21:08:20
我能想到的唯一两种方法是:
在app/local/RWS/AutoDeleteRelatedCartProducts/etc/config.xml中创建
<config>
<global>
<models>
<autodeleterelatedcartproducts>
<class>RWS_AutoDeleteRelatedCartProducts_Model</class>
</autodeleterelatedcartproducts>
</models>
</global>
<frontend>
<events>
<sales_quote_remove_item>
<observers>
<autodeleterelatedcartproducts>
<type>singleton</type>
<class>autodeleterelatedcartproducts/observer</class>
<method>removeQuoteItem</method>
</autodeleterelatedcartproducts>
</observers>
</sales_quote_remove_item>
</events>
</frontend>
</config>在app/local/RWS/AutoDeleteRelatedCartProducts/Model/Observer.php中创建
<?php
class RWS_AutoDeleteRelatedCartProducts_Model_Observer
{
public function removeQuoteItem(Varien_Event_Observer $observer)
{
//get deleted product
$delete_product = $observer->getQuoteItem()->getProduct();
// Get all related products
$related_products = $delete_product->getRelatedProductCollection();
// get all related product id and save in array
$related_product_ids = array();
foreach($related_products as $product){
$related_product_ids[] = $product->getId(); // double check to make sure this product_id
}
foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item )
{
// if getId is a related product remove it
if(in_array($item->getId(), $related_product_ids))
Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save();
}
}
}
?>阅读更多@
Observer for removed items in the cart
Help with Magento and related products
Magento - How to check if a product has already been removed from the cart
http://www.magentocommerce.com/boards/viewthread/30113/
发布于 2012-10-30 06:58:47
如果我是您,我可能一开始就不会添加产品,而是使用checkout_cart_product_add_after事件的观察者。根据您的预期产品检查产品。使用R.S.的方法在购物车中获得报价和检查产品数量的句柄。向产品添加一个自定义选项,让客户知道他是否有免费的好东西。这可能是合适的Magento - Quote/order product item attribute based on user input
然后将观察者添加到此事件sales_convert_quote_to_order。这个观察者可以检查数量,并对类似的东西进行调整,将产品提供给客户Skip Checkout in Magento for a downloadable product你唯一得到的单例,并使用观察者,因此这种方法比购物车过程中的大量添加和删除要便宜得多。而且它看起来也会好很多。
如果你愿意,我会尝试实现它,但是使用你的站点和数据库的副本,因为我太懒了,不能设置产品。
ps。也许你也需要观察这个事件,checkout_cart_update_items_before
ps。ps。也许我应该在评论之前检查一下,哈哈。哈哈,你被禁止进入这里了吗?
https://stackoverflow.com/questions/13083322
复制相似问题