我和Magento有问题。
我有一个类别,客户每个订单只能从该类别中购买一种产品。我已成功设置设置,以便在产品设置中只能添加一个项目。但是,如果客户返回该类别,他仍然可以在该类别中添加其他产品。假设他从A类中选择产品A,他可以返回A类选择产品B.我想要的是任何时候,对于该订单,他只能从A类购买一件产品。
如果无法做到这一点,如果从该类别中选择了多个产品,我希望增加额外的运费。
有人有解决方案吗?
答案 0 :(得分:1)
请参阅Magento: limit 3 products from category per order
为事件checkout_cart_product_add_after
<events>
<checkout_cart_product_add_after>
<observers>
<enableduplicateproductstatus>
<type>singleton</type>
<class>limitcartproductbycategory/observer</class>
<method>cartlimit</method>
</enableduplicateproductstatus>
</observers>
</checkout_cart_product_add_after>
</events>
创建:app / code / local / MagePal / LimitCartProductByCategory / Model / Observer.php
class MagePal_LimitCartProductByCategory_Model_Observer
{
public function cartlimit(Varien_Event_Observer $observer)
{
$category_ids = array();
$quote = Mage::getSingleton('checkout/session')->getQuote();
foreach($quote->getAllVisibleItems() as $item){
$product = Mage::getModel('catalog/product')->load($item->getId());
$product_category_ids = explode(",", $product->getCategoryIds());
//$product_category_ids = $product->getCategoryIds();
array_push($category_ids, $product_category_ids);
}
$justAdded = $observer->getQuoteItem();
$productJustAdded = Mage::getModel('catalog/product')->load($justAdded->getId());
//total the category id in $category_ids
//if $productJustAdded->getCategoryIds exist in $category_ids,
//then check to see if category id count greater than 3
// if true then add error msg and try setting the qty to 0
return $this;
}
}