Top 5 Programming Questions and Answers Regarding Magento Shipping Restrictions - Part 1

February 27, 2017,
Top 5 Programming Questions and Answers Regarding Magento Shipping Restrictions - Part 1
Restricting your product shipping to a specific country or region based on certain attributes is an important activity in the modern eCommerce industry because in some cases, you cannot ship your products to other countries/regions. Besides this in various cases, you need to ship some products on a specific carrier. For example, you can restrict by road shipping method for the products which have a short life as by road shipping usually takes a week or so to deliver products. There are lots of developers who often face problems while applying restrictions on shipping. Looking at this real world problem on the programmer's end, FMEAddons have picked up a list of top 5 common issues regarding Magento shipping restrictions from the top Magento experts. Question No. 1 The user has a multi-store website in Magento, He wants to restrict countries on one store that he can make shipping. Additionally, he wants the payment to be received from all around the world. He tried
system --> configuration --> web --> general
And
system --> configuration --> shipping method --> specify country
The problem is that the user wants to display countries names in a drop-down menu of billing information and a specific country in the dropdown of shipping information. How to perform this? Answer: As a guideline use the code given below, hopefully, it will resolve your issue From Class: abstract class
Mage_Checkout_Block_Onepage_Abstract
public function getCountryHtmlSelect($type)
$select = $this->getLayout()->createBlock('core/html_select')
            ->setName($type.'[country_id]')
            ->setId($type.':country_id')
            ->setTitle(Mage::helper('checkout')->__('Country'))
            ->setClass('validate-select')
            ->setValue($countryId)
            ->setOptions($this->getCountryOptions());
setOptions($this->getCountryOptions()
This will take care of shipping countries in drop down list Question No. 2 How to restrict other shipping methods in Magento? By using  $result = Mage::getModel('shipping/rate_result'); user is able to create shipping rate. But he is worried about the restrictions of other shipping methods. Answer: Search for all your allowed shipping methods, loop through each one and apply conditions where it matches your required one.
$activeCarriers = Mage::getModel('shipping/config')->getActiveCarriers();
foreach($activeCarriers as $code => $method) {
    if($code == 'yourcode') {
         $result = Mage::getModel('shipping/rate_result');
    }

}   
Question No. 3 How to remove shipping method in Magento? The user have the below code in the extension
CompanyName/
  ExtensionName/
     Block/
       Onepage/
          Abstract.php
     controllers/
       OnepageController.php
     etc/
       config.xml
     Model/
       Type/
          Onepage.php
Config.xml file
xml version="1.0"?> 
    
        
            1.0
        
    
    
        
            
                
                    CompanyName_ExtensionName_Block_Onepage
                
            
        
        
            
                
                    standard
                    
                        
                             before="Mage_Checkout">
                                CompanyName_ExtensionName
                            
                        
                    
                
            
        
        
            
                
                    CompanyName_ExtensionName_Model_Type_Onepage
                
            
            
        
Block/Onepage/Abstract.php
abstract class CompanyName_ExtensionName_Onepage_Abstract extends Mage_Checkout_Block_Onepage_Abstract
{

    protected function _getStepCodes()
    {
        return array('login','billing', 'payment', 'review');
    }
}
controllers/OnepageController.php
require_once 'Mage/Checkout/controllers/OnepageController.php';

class CompanyName_ExtensionName_OnepageController extends Mage_Checkout_OnepageController
{
    protected $_sectionUpdateFunctions = array(
        'payment-method' => '_getPaymentMethodsHtml',
        'review' => '_getReviewHtml',
    );

    public function saveBillingAction()
    {
        if ($this->_expireAjax()) {
            return;
        }
        $data = $this->getRequest()->getPost('billing', array());
        $customerAddressId = $this->getRequest()->getPost('billing_address_id', false);
        if (isset($data['email'])) {
            $data['email'] = trim($data['email']);
        }
        $result = $this->getOnepage()->saveBilling($data, $customerAddressId);
        if (!isset($result['error'])) {
            /* check quote for virtual */
            if ($this->getOnepage()->getQuote()->isVirtual()) {
                $result['goto_section'] = 'payment';
                $result['update_section'] = array(
                    'name' => 'payment-method',
                    'html' => $this->_getPaymentMethodsHtml()
                );
            } else {
                $result['goto_section'] = 'payment';
            }
        }
        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
    }

    public function saveShippingAction()
    {
        if ($this->_expireAjax()) {
            return;
        }
        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost('shipping', array());
            $customerAddressId = $this->getRequest()->getPost('shipping_address_id', false);
            $result = $this->getOnepage()->saveShipping($data, $customerAddressId);

            if (!isset($result['error'])) {
                $result['goto_section'] = 'payment';
                $result['update_section'] = array(
                    'name' => 'payment-method',
                    'html' => $this->_getShippingMethodsHtml()
                );
            }
            $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
        }
    }
}
Model/Type/Onepage.php
    class CompanyName_ExtensionName_Model_Type_Onepage extends Mage_Checkout_Model_Type_Onepage
    {

        public function saveShippingMethod($shippingMethod)
        {


   if (empty($shippingMethod)) {
            $shippingMethod = 'freeshipping_freeshipping';
        }
        $rate = $this->getQuote()->getShippingAddress()->getShippingRateByCode($shippingMethod);
        if (!$rate) {
            return array('error' => -1, 'message' => Mage::helper('checkout')->__('Invalid shipping method.'));
        }
        $this->getQuote()->getShippingAddress()
            ->setShippingMethod($shippingMethod);

        $this->getCheckout()
            ->setStepData('shipping_method', 'complete', true)
            ->setStepData('payment', 'allow', true);

        return array();
    }
}
While looking for your extension, you are trying to override the block Mage_Checkout_Block_Onepage_Abstract. For this, use code inside your config.xml, below is an example of how your code looks like:
    
        
            
                CompanyName_ExtensionName_Block_Onepage_Abstract
            
        
    
Question No. 4 How to block free shipping method on the frontend in Magento? For example, customer purchased a product along with shipping charges but it is not delivered. The website admin wants to deliver the same product free of cost and shipping charges, so in this case, free shipping method should show in the backend. When the store admin enables free shipping on the backend this is shown on the frontend also. So how to block free shipping on the checkout page and show only in the admin panel? Answer: For this, you just need to edit the template of shipping methods and use an extra 'if condition' by shipping method code. Or you can do this by inheritance i.e. extend the shipping method and set the isActive method to create a difference between frontend and backend. Question No. 5 How to get shipping data of an order in Magento? Answer: In Magento order doesn't have shipping date but they have shipments and each of shipment have a creation date. In order to access order shipments you can use
$order->getShipmentsCollection();
After this, on the shipment, you can call getCreatedAt() to get the date. Complete code for retrieving date is
/** @var $order Mage_Sales_Model_Order */
foreach($order->getShipmentsCollection() as $shipment){
    /** @var $shipment Mage_Sales_Model_Order_Shipment */
    echo $shipment->getCreatedAt();
}

Buy Magento Shipping Restrictions Extension 

restrict-shipping