1

A new regulation is active since december 3rd of 2018. It prohibits vendors from blocking purchases from anywhere inside the EU. You can still, however, prevent people from demanding shipment to their respective countries.

That means that people living in the border regions of foreign countries are now allowed to buy wares from vendors of different countries, if they are fine with picking them up from the vendor or shipping them only to the vendor country (a storage space, P.O. box).

Now, for Magento 1 there is a configuration for allowed countries in the backend (System -> Configuration -> General -> Allowed countries [general/country/allow]), which is documented as follows:

The Countries Options establish the country where your business is located, and the countries from which you accept payment.

That, however, is not what the option is doing. Allowing all countries here allows all these countries for both invoice address (accept payment from) and delivery address (ship to).

To comply with the Geoblocking legislative, we would need an option to restrict shipping to certain countries only, and allow invoicing to all EU (or only configured) countries.

We are currently using OneStepCheckout, and they answered that they handle it the way the standard checkout works.

How do you go about implementing this change?

1 Answer 1

0

Please check this article ... Sorry, we can’t ship there. It should be exactly what you're looking for ...


Add a product attribute via installer to display countries as dropdown ...

/* @var $installer Mage_Eav_Model_Entity_Setup */
$installer = $this;
$installer->startSetup();

$installer->addAttribute(
    Mage_Catalog_Model_Product::ENTITY,
    'shipping_restriction',
    array(
        'type'      => 'varchar',
        'group'     => 'General',
        'input'     => 'multiselect',
        'label'     => 'Disallow Shipping to',
        'source'    => 'inchoo_shippingrestriction/attribute_source_country',
        'backend'   => 'inchoo_shippingrestriction/attribute_backend_country',
        'global'    => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
        'apply_to'  => 'simple,configurable,bundle,grouped',
        'required'  => false
    )
);

$installer->endSetup();

Source model ...

class Inchoo_ShippingRestriction_Model_Attribute_Source_Country
    extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
    implements Mage_Eav_Model_Entity_Attribute_Source_Interface
{
    /**
     * Get list of all available countries
     *
     * @return array|mixed
     */
    public function getAllOptions()
    {
        $cacheKey = 'DIRECTORY_COUNTRY_SELECT_STORE_' . Mage::app()->getStore()->getCode();
        if (Mage::app()->useCache('config') && $cache = Mage::app()->loadCache($cacheKey)) {
            $options = unserialize($cache);
        } else {
            $collection = Mage::getModel('directory/country')->getResourceCollection();
            $options = $collection->toOptionArray();
            if (Mage::app()->useCache('config')) {
                Mage::app()->saveCache(serialize($options), $cacheKey, array('config'));
            }
        }
        return $options;
    }
}

Backend model

class Inchoo_ShippingRestriction_Model_Attribute_Backend_Country
    extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
    implements Mage_Eav_Model_Entity_Attribute_Backend_Interface
{
    public function beforeSave($object)
    {
        $attrCode = $this->getAttribute()->getAttributeCode();
        $object->setData($attrCode, implode(',', $object->getData($attrCode)));

        return $this;
    }

    public function afterLoad($object)
    {
        $attrCode = $this->getAttribute()->getAttributeCode();
        $object->setData($attrCode, explode(',', $object->getData($attrCode)));

        return $this;
    }
}

Shipping model

class Inchoo_ShippingRestriction_Model_Shipping extends Mage_Shipping_Model_Shipping
{
    public function collectRates(Mage_Shipping_Model_Rate_Request $request)
    {
        if ($this->isShippingRestriction($request)) {
            return $this;
        }

        return parent::collectRates($request);
    }

    /**
     * Check if shipping restrictions apply
     *
     * @param Mage_Shipping_Model_Rate_Request $request
     * @return bool
     */
    public function isShippingRestriction(Mage_Shipping_Model_Rate_Request $request)
    {
        $options = array();

        /** @var Mage_Sales_Model_Quote_Item $quoteItem */
        foreach ($request->getAllItems() as $quoteItem) {
            $options = array_merge(
                $options,
                explode(',', $quoteItem->getProduct()->getShippingRestriction())
            );
        }

        return in_array($request->getDestCountryId(), $options);
    }
}
4
  • Thanks for your suggestion. If I understand it correctly, implementing it would mean to add a custom attribute to all products, then configure the shipping restriction on all of them. As the requirement applies to the whole country, it would make much more sense to configure or implement a solution based on the store or storeview. That saves altering all current products (about 25k) and the manual process of configuring that for new products as well.
    – 0xCAFEBABE
    Commented Dec 10, 2018 at 12:28
  • @0xCAFEBABE You're right. We needed a solution for individual products, so I had to use product attributes. It's also possible to add a new config section. I can update answer later if wanted :)
    – sv3n
    Commented Dec 10, 2018 at 17:01
  • If that is possible, I'd appreciate it. We have so far not found anybody who has this problem on their radar. Our Magento implementor had no experience and no requests to look into this problem besides ours, so it seems we're on our own.
    – 0xCAFEBABE
    Commented Dec 13, 2018 at 7:34
  • @0xCAFEBABE no time for ATM ... may during next weeks. The code is reusable for config based setting. Maybe ask a new question that refers to here?
    – sv3n
    Commented Dec 19, 2018 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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