0

I want to restrict some products to sell in a particular country, Not all products.

Can you please suggest me how to achieve it?

  • You need to do customization or Use third party extension for this. – Aditya Shah Dec 3 '18 at 17:32
  • Did you solved? @Shesh – zus Oct 10 at 10:35
0

I have created a custom module to achieve this.

app/etc/modules/Custom_Productrestrict.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Custom_Productrestrict>
            <active>true</active>
            <codePool>local</codePool>
        </Custom_Productrestrict>
    </modules>
</config>

app/code/local/Custom/Productrestrict/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Custom_Productrestrict>
            <version>1.0.0</version>
        </Custom_Productrestrict>
    </modules>
    <global>
        <models>
            <productrestrict>
                <class>Custom_Productrestrict_Model</class>
               <resourceModel>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</resourceModel>
            </productrestrict>
        </models>
        <resources>
            <productrestrict_setup>
                <setup>
                    <module>Custom_Productrestrict</module>
                    <class>Mage_Catalog_Model_Resource_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </productrestrict_setup>
            <productrestrict_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </productrestrict_write>
            <productrestrict_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </productrestrict_read>
        </resources>
        <helpers>
            <productrestrict>
                <class>Custom_Productrestrict_Helper</class>
            </productrestrict>
        </helpers>
        <events>
              <sales_quote_address_save_after>
                <observers>
                    <productrestrict_product>
                        <type>singleton</type>
                        <class>Custom_Productrestrict_Model_Observer</class>
                        <method>restrictProductByCountry</method>
                    </productrestrict_product>
                </observers>
            </sales_quote_address_save_after>
        </events>
    </global>
     <default>
        <catalog>
            <productrestrict>
                <enable_disable>1</enable_disable>              
            </productrestrict>
        </catalog>
    </default>
</config>

app/code/local/Custom/Productrestrict/etc/system.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
   <sections>         
        <catalog translate="label">
            <groups>
                <productrestrict translate="label" module="productrestrict">
                    <label>Restrict Product Country</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>10</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <enable_disable translate="label comment">
                            <label>Enable</label>
                            <comment>Enable/Disable extension</comment>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>10</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </enable_disable>
                    </fields>
                </productrestrict>
            </groups>                       
        </catalog>          
    </sections>
</config>

app/code/local/Custom/Productrestrict/Helper/Data.php

<?php

class Custom_Productrestrict_Helper_Data extends Mage_Core_Helper_Abstract {

    public function isEnabled()
    {       
        return Mage::getStoreConfig('catalog/productrestrict/enable_disable');
    }

}

app/code/local/Custom/Productrestrict/Model/Product/Attribute/Source/Custom.php

<?php 
class Custom_Productrestrict_Model_Product_Attribute_Source_Custom
    extends Mage_Catalog_Model_Product_Attribute_Source_Countryofmanufacture
{
    public function getAllOptions()
    {
        $options = parent::getAllOptions();
        $alloptions = array();
        foreach ($options as $key => $option) {
             $alloptions[] = array(
                    'label' => $option['label'],
                    'value' => $option['value']
            );
        }
        return $alloptions;
    }

    public function toOptionArray()
    {
        return $this->getAllOptions();
    }

    public function getOptionText($value)
    {
        $options = $this->getAllOptions(false);
        foreach ($options as $item) {
            if ($item['value'] == $value) {
                return $item['label'];
            }
        }
        return false;
    }
}

app/code/local/Custom/Productrestrict/Model/Observer.php

<?php

class Custom_Productrestrict_Model_Observer  {

    public function restrictProductByCountry($observer) {
        if (Mage::helper('productrestrict')->isEnabled()) {
           $post = Mage::app()->getFrontController()->getRequest()->getPost();
           if(count($post) > 0 ){
                if(isset($post['billing']) && isset($post['billing']['use_for_shipping']) && count($post['billing']) > 0 && $post['billing']['use_for_shipping']==1){
                    $country_code = $post['billing']['country_id'];
                    $this->restrict($country_code);
                }
                if(isset($post['shipping']) && count($post['shipping']) > 0){
                    $country_code = $post['shipping']['country_id'];
                    $this->restrict($country_code);
                }
            }
        }
    }
    private function restrict($country_code){
            $country = Mage::getModel('directory/country')->loadByCode($country_code);
            $products = array();
            $items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
            foreach($items as $item){
                    $product = Mage::getModel('catalog/product')->load($item->getProductId());
                    if($product->getId() > 0){
                        if($product->getRestrictCountries() && in_array($country_code,explode(',',$product->getRestrictCountries()))){
                                $products[] = $product->getName();
                        }
                    }

            }
            if(count($products) == 1){
                Mage::getSingleton('checkout/session')->addError(implode(', ',$products).' product is not allowed in "'.$country->getName().'" country.');
                $this->_redirect();
            }
            if(count($products) > 0 && count($items)  > 0){
                Mage::getSingleton('checkout/session')->addError(implode(', ',$products).' products are not allowed in "'.$country->getName().'" country.');
                $this->_redirect();
            }
    }
    private function _redirect(){
        $url=Mage::getUrl('checkout/onepage');
        $response1 = Mage::app()->getResponse(); // observers have event args
        $response1->setRedirect($url);
        Mage::app()->getFrontController()->sendResponse();
        return;
    }
}

app/code/local/Custom/Productrestrict/Model/Productrestrict.php

<?php

class Custom_Productrestrict_Model_Productrestrict extends Mage_Core_Model_Abstract {

    public function _construct() {
        $this->_init('productrestrict/productrestrict');
    }

}

app/code/local/Custom/Productrestrict/sql/productrestrict_setup/install-1.0.0.php

<?php
$installer = $this;
/* @var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */

$installer->startSetup();

$applyTo = array(
    Mage_Catalog_Model_Product_Type::TYPE_SIMPLE,
    Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE
);
$installer->addAttribute(
    "catalog_product", "restrict_countries",
    array(
        "type" => "text",
        "backend" => "eav/entity_attribute_backend_array",
        'group' => 'General',
        "label" => "Restricted Countries",
        "input" => "multiselect",
        "source" => "productrestrict/product_attribute_source_custom",
        'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
        'is_visible' => 1,
        'required' => 0,
        'searchable' => 0,
        'filterable' => 0,
        'unique' => 0,
        'comparable' => 0,
        'visible_on_front' => 0,
        'user_defined' => 1,
        'apply_to'          => implode(',',$applyTo),
        'note' => 'Shipping of this product will be not allowed in selected countries.'
    )
);
$installer->endSetup();
  • Thanks, I'll try it using the above module. – Shesh Dec 19 '18 at 15:09
  • Hi, I have created a custom module but it is not reflected in the admin setting.how to configure these things. – Shesh Dec 19 '18 at 17:02
  • "Restrict Product Country" is not displayed in admin panel – Shesh Dec 19 '18 at 17:20
  • Did you clear cache and tried again? Also Please check if the module is enabled in System>Configuration>Advanced>Advanced. – Meenu Dec 20 '18 at 5:30
  • Yes, I have checked. The module is enabled in a configuration. – Shesh Dec 20 '18 at 5:43
0

Without requiring customization, you could create multiple websites, each with a product catalog specific to a region. Then restrict shipping methods in each website to the appropriate region.

  • is there any other solution? because I want to restrict only one product. – Shesh Dec 4 '18 at 4:49

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.