[英]Magento allow product add to cart if another category product added
我的商店有几类,分别是衬衫,裤子,配饰等。 如果用户单击该裤子的“添加到购物车”按钮,则在将该裤子添加到购物车之前,请检查是否已经添加了衬衫。 目的是允许用户在添加裤子之前先添加衬衫。
我可以使用ajax吗?
如果有人有解决方案,请对此表示赞赏。 谢谢
您可以为此创建自己的扩展名,但是简单的方法是。
首先,您可以创建产品属性,比如说“ related_category”
并将以下扩展名用于Ajax添加到购物车。
https://www.magentocommerce.com/magento-connect/ajaxcart-3-15606.html
在此扩展名中打开此文件
[app \\ code \\ local \\ Hardik \\ Ajaxcart \\ controllers \\ Checkout \\ CartController.php]
并找到以下代码。
$ related = $ this-> getRequest()-> getParam('related_product');
在此行之后添加此代码[这里我从新创建的名为“ related_category”的属性中获取类别ID]。
$related_category = Mage::getModel('catalog/product')->load($product->getId())->getRelatedCategory();
$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems(); $available = 'false';
foreach ($cartItems as $item) {
$productId = $item->getProductId();
$_product = Mage::getModel('catalog/product')->load($productId);
$productCats = $_product->getCategoryIds();
if (count(array_intersect($related_category,$productCats))) {
$available = 'true'; break;
}
}
if($available == 'false'){
$_response = Mage::getModel('ajaxcart/response');
$_response->setError(true);
$messages = array_unique(explode("\n", "Please add related product first!"));
$json_messages = array();
foreach ($messages as $message) {
$json_messages[] = Mage::helper('core')->escapeHtml($message);
}
$_response->setMessages($json_messages);
$url = $this->_getSession()->getRedirectUrl(true);
$_response->send();
}
在上面的代码中,我获取了相关的类别,并检查了购物车中是否有可用的产品[$ available ='true'],添加到购物车将正常工作,否则[$ available ='false']将显示错误信息“请先添加相关产品!”
因此,每当客户购买“裤子”并且在此产品的related_category属性中设置了“衬衫”类别ID时,它将要求首先添加衬衫产品。
因此,我希望该解决方案对您有用。 请检查此内容,如有任何查询,请通知我。
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.