Top 5 Programming Questions and Answers Regarding Magento Shipping Restrictions - Part 1
February 27, 2017,
system --> configuration --> web --> generalAnd
system --> configuration --> shipping method --> specify countryThe 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
public function getCountryHtmlSelect($type)Mage_Checkout_Block_Onepage_Abstract
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.setOptions($this->getCountryOptions()$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());
Question No. 3 How to remove shipping method in Magento? The user have the below code in the extension$activeCarriers = Mage::getModel('shipping/config')->getActiveCarriers(); foreach($activeCarriers as $code => $method) { if($code == 'yourcode') { $result = Mage::getModel('shipping/rate_result'); } }
Config.xml fileCompanyName/ ExtensionName/ Block/ Onepage/ Abstract.php controllers/ OnepageController.php etc/ config.xml Model/ Type/ Onepage.php
controllers/OnepageController.phpBlock/Onepage/Abstract.phpxml version="1.0"?>
1.0 CompanyName_ExtensionName_Block_Onepage standard before="Mage_Checkout"> CompanyName_ExtensionName CompanyName_ExtensionName_Model_Type_Onepage abstract class CompanyName_ExtensionName_Onepage_Abstract extends Mage_Checkout_Block_Onepage_Abstract { protected function _getStepCodes() { return array('login','billing', 'payment', 'review'); } }
Model/Type/Onepage.phprequire_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)); } } }
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: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(); } }
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
CompanyName_ExtensionName_Block_Onepage_Abstract
After this, on the shipment, you can call getCreatedAt() to get the date. Complete code for retrieving date is$order->getShipmentsCollection();
/** @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