简体   繁体   English

Magento允许产品添加到购物车(如果添加了其他类别的产品)

[英]Magento allow product add to cart if another category product added

I have a store with few categories which are shirt, trousers, accessories,....etc. 我的商店有几类,分别是衬衫,裤子,配饰等。 If user click add to cart button of the trouser then check if already added the shirt before add this trouser to the cart. 如果用户单击该裤子的“添加到购物车”按钮,则在将该裤子添加到购物车之前,请检查是否已经添加了衬衫。 The purpose is allow users to add a shirt before they add a trouser. 目的是允许用户在添加裤子之前先添加衬衫。

Can I do using ajax ? 我可以使用ajax吗?

If anyone have solutions appreciate that. 如果有人有解决方案,请对此表示赞赏。 Thank you 谢谢

You can create your own extension for this but the simple way for this is. 您可以为此创建自己的扩展名,但是简单的方法是。

First you can create product attribute let's say "related_category" 首先,您可以创建产品属性,比如说“ related_category”

and use below extension for Ajax add to cart. 并将以下扩展名用于Ajax添加到购物车。

https://www.magentocommerce.com/magento-connect/ajaxcart-3-15606.html https://www.magentocommerce.com/magento-connect/ajaxcart-3-15606.html

In this extension open this file 在此扩展名中打开此文件

[app\\code\\local\\Hardik\\Ajaxcart\\controllers\\Checkout\\CartController.php] [app \\ code \\ local \\ Hardik \\ Ajaxcart \\ controllers \\ Checkout \\ CartController.php]

and find below code. 并找到以下代码。

$related = $this->getRequest()->getParam('related_product'); $ related = $ this-> getRequest()-> getParam('related_product');

add this code after this line [here i have fetch category ids from new created attribute called "related_category"]. 在此行之后添加此代码[这里我从新创建的名为“ 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();
    }

In Above code i have fetch related category and check in cart whether there products are available or not if it's available [$available = 'true'] add to cart will work normally otherwise [$available = 'false'] it will give error like "Please add related product first!" 在上面的代码中,我获取了相关的类别,并检查了购物车中是否有可用的产品[$ available ='true'],添加到购物车将正常工作,否则[$ available ='false']将显示错误信息“请先添加相关产品!”

So, whenever customer purchase "trouser" and in related_category attribute of this product you have set "shirts" category id it will ask to add shirts product first. 因此,每当客户购买“裤子”并且在此产品的related_category属性中设置了“衬衫”类别ID时,它将要求首先添加衬衫产品。

So, i hope this solution work for you. 因此,我希望该解决方案对您有用。 please check this and let me know in case of any query. 请检查此内容,如有任何查询,请通知我。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM