4

I'm running a Magento 1.9 CE shop with two different stores (each store has two StoreViews) and I'd like to disable the cart sharing feature. So when I switch from one store to the other store, the product in my cart shouldn't be valid or deleted. How should I solve this situation?

4

Not sure if this will work, but it could be a starting point.
There are 2 methods that allow sharing the quote between store views under the same website: Mage_Checkout_Model_Session::_getQuoteIdKey - that generates the session key for the quote. By default it returns quote_id_{website_id}. where {website_id} is the current website id.
You should change this to quote_id_{store_id} where {store_id} is the current store id.
Then there is the method that checks which stores are allowed to share the quote: Mage_Sales_Model_Quote::getSharedStoreIds().
By default it returns the ids of all the store views under the current website. You should make it return and array with the current store view id.

Based on the above, you will need to create a module to rewrite these methods.

Let's call this module StackExchange_NoShare.
you will need these files:

app/etc/modules/StackExchange/NoShare.xml - the declaration file

<?xml version="1.0"?>
<config>
    <modules>
        <StackExchange_NoShare>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Checkout />
                <Mage_Sales />
            </depends>
        </StackExchange_NoShare>
    </modules>
</config>

app/code/local/StackExchange/NoShare/etc/config.xml - the configuration file

<?xml version="1.0"?>
<config>
    <modules>
        <StackExchange_NoShare>
            <version>1.0.0</version>
        </StackExchange_NoShare>
    </modules>
    <global>
        <models>
            <sales>
                 <rewrite>
                     <quote>StackExchange_NoShare_Model_Quote</quote>
                 </rewrite>
            </sales>
            <checkout>
                <rewrite>
                    <session>StackExchange_NoShare_Model_Checkout_Session</session>
                </rewrite>
             </checkout>
         </models>
         <helpers>
             <stackexchange_noshare>
                 <class>StackExchange_NoShare_Helper</class>
             <stackexchange_noshare>
         </helpers>
    </global>
    <default>
        <checkout>
            <options>
                 <no_share>1</no_share><!-- this is for the fail-safe -->
            </options>
        </checkout>
   </default>
</config>

app/code/local/StackExchange/NoShare/Helper/Data.php - The module main helper. The method getNoShare checks if you have the cart 'no-sharing' enabled

<?php 
class StackExchange_NoShare_Helper_Data extends Mage_Core_Helper_Abstract
{
    public function getNoShare()
    {
       return Mage::getStoreConfigFlag('checkout/options/no_share');
    }
}

app/code/local/StackExchange/NoShare/Model/Quote.php - the rewrite for the quote model

<?php 
class StackExchange_NoShare_Model_Quote extends Mage_Sales_Model_Quote
{
    public function getSharedStoreIds()
    {
        if (Mage::helper('stackexchange_noshare')->getNoShare()){//if behavior is not diasabled
            return array($this->getStoreId());
        }
        return parent::getSharedStoreIds();
     }
}

app/code/local/StackExchange/NoShare/Model/Checkout/Session.php - the rewrite for the checkout session

<?php 
class StackExchange_NoShare_Model_Checkout_Session extends Mage_Checkout_Model_Session
{
    protected function _getQuoteIdKey()
    {
        if (Mage::helper('stackexchange_noshare')->getNoShare()){//if behavior is not diasabled
            return 'quote_id_'.Mage::app()->getStore()->getId();
        }
        return parent::_getQuoteIdKey();
    }
}

app/code/local/StackExchange/NoShare/etc/system.xml - the system configuration file so you can disable this feature.

<?xml version="1.0"?>
<config>
    <sections>
        <checkout>
            <groups>
                <options>
                    <fields>
                        <no_share translate="label" module="stackexchange_noshare">
                            <label>Cart available per store view</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>100</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>0</show_in_website>
                            <show_in_store>0</show_in_store>
                        </no_share>
                    </fields>
                </options>
            </groups>
        </checkout>
     </sections>
</config>
1
  • Hello Marius, Thank you for your quick reply, but it doesn't work. It's still possible to a have a common cart and the option is not available in the configuration > Checkout section.
    – teon
    Nov 28 '15 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.