[英]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.