-
-
Save refaelgold/af2b1db0ce49c50761bfa9a537d030f3 to your computer and use it in GitHub Desktop.
Set shipping method and rate if they do not exist yet in Magento
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Has shipping been applied to quote? | |
* | |
* @var bool | |
*/ | |
protected $_hasShipping = false; | |
/** | |
* Set shipping method and rate if they do not exist yet | |
*/ | |
public function setQuoteShippingMethod() | |
{ | |
if(!$this->_hasShipping) { | |
$this->_hasShipping = true; // This is to avoid loops on totals collecting | |
$quote = Mage::helper('checkout/cart')->getQuote(); | |
if (!$quote->getId()) return; | |
$shippingMethod = $quote->getShippingAddress()->getShippingMethod(); | |
if ($shippingMethod) return; | |
$shippingAddress = $quote->getShippingAddress(); | |
$country = 'FR'; // Some country code | |
$postcode = '75000'; // Some postcode | |
$regionId = '0'; // Some region id | |
$method = 'tablerate_bestway'; // Used shipping method | |
$shippingAddress | |
->setCountryId($country) | |
->setRegionId($regionId) | |
->setPostcode($postcode) | |
->setShippingMethod($method) | |
->setCollectShippingRates(true) | |
; | |
$shippingAddress->save(); | |
$quote->save(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<sales_quote_collect_totals_before> | |
<observers> | |
<!-- Set default shipping method and rate when necessary --> | |
<autoshipping> | |
<class>My_Module_Model_Observer</class> | |
<method>setQuoteShippingMethod</method> | |
</autoshipping> | |
</observers> | |
</sales_quote_collect_totals_before> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment