Skip to content

Commit

Permalink
Move observer to events around cart initialization and also save ship…
Browse files Browse the repository at this point in the history
…ping description

This prevents duplicate totals collection and still makes sure, everything is up to date.
  • Loading branch information
schmengler committed Feb 4, 2016
1 parent 21c1101 commit c86420e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
48 changes: 33 additions & 15 deletions app/code/community/IntegerNet/Autoshipping/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,61 @@

class IntegerNet_Autoshipping_Model_Observer
{
protected $_methodManuallyChanged;

/**
* Set configured country
*
* @param Varien_Event_Observer $observer
* @event controller_action_predispatch_checkout_cart_index
* @event checkout_cart_save_before
*/
public function addShipping(Varien_Event_Observer $observer)
public function prepareShippingAddress(Varien_Event_Observer $observer)
{
if (! Mage::getStoreConfigFlag('autoshipping/settings/enabled')) {
return;
}
if (!($country = $this->_getCoreSession()->getAutoShippingCountry())) {
$country = Mage::getStoreConfig('autoshipping/settings/country_id');
$this->_getCoreSession()->setAutoShippingCountry($country);
}

$quote = $this->_getCheckoutSession()->getQuote();
if (! $quote->hasItems()) {
return;
}
if (!($country = $this->_getCoreSession()->getAutoShippingCountry())) {
$country = Mage::getStoreConfig('autoshipping/settings/country_id');
$this->_getCoreSession()->setAutoShippingCountry($country);
}

$billingAddress = $quote->getBillingAddress();
if (!$billingAddress->getCountryId()) {
$billingAddress->setCountryId($country);
}

$shippingAddress = $quote->getShippingAddress();

$shippingAddress->setCountryId($country);
$shippingAddress->setCollectShippingRates(true);

if (!$shippingAddress->getFreeMethodWeight()) {
$shippingAddress->setFreeMethodWeight($shippingAddress->getWeight());
}

$isMethodManuallyChanged = $this->_isMethodManuallyChanged($shippingAddress);
$this->_methodManuallyChanged = $this->_isMethodManuallyChanged($shippingAddress);
}
/**
* Set shipping method
*
* @param Varien_Event_Observer $observer
* @event checkout_cart_save_after
*/
public function addShipping(Varien_Event_Observer $observer)
{
if (! Mage::getStoreConfigFlag('autoshipping/settings/enabled')) {
return;
}
$quote = $this->_getCheckoutSession()->getQuote();
if (! $quote->hasItems()) {
return;
}

$quote->collectTotals();
$shippingAddress = $quote->getShippingAddress();

if($isMethodManuallyChanged && $shippingAddress->getShippingMethod()) {
if($this->_methodManuallyChanged && $shippingAddress->getShippingMethod()) {
// if the manually selected shipping method is still available, do nothing!
return;
}
Expand All @@ -62,20 +79,21 @@ public function addShipping(Varien_Event_Observer $observer)
foreach($topRates as $topRate) {

/** @var Mage_Sales_Model_Quote_Address_Rate $topRate */
$code = $topRate->code;

if (in_array($topRate->getCarrier(), explode(',', Mage::getStoreConfig('autoshipping/settings/ignore_shipping_methods')))) {
continue;
}

try {
$shippingAddress->setShippingMethod($code);
$shippingAddress->setShippingMethod($topRate->getCode());
$shippingDescription = $topRate->getCarrierTitle() . ' - ' . $topRate->getMethodTitle();
$shippingAddress->setShippingDescription(trim($shippingDescription, ' -'));

$quote->save();

$this->_getCheckoutSession()->resetCheckout();

$this->_getCheckoutSession()->setAutoShippingMethod($code);
$this->_getCheckoutSession()->setAutoShippingMethod($topRate->getCode());

} catch (Mage_Core_Exception $e) {
$this->_getCheckoutSession()->addError($e->getMessage());
Expand Down
13 changes: 11 additions & 2 deletions app/code/community/IntegerNet/Autoshipping/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,24 @@
</global>
<frontend>
<events>
<controller_action_predispatch_checkout_cart_index>
<checkout_cart_save_before>
<observers>
<autoshipping>
<type>singleton</type>
<class>autoshipping/observer</class>
<method>prepareShippingAddress</method>
</autoshipping>
</observers>
</checkout_cart_save_before>
<checkout_cart_save_after>
<observers>
<autoshipping>
<type>singleton</type>
<class>autoshipping/observer</class>
<method>addShipping</method>
</autoshipping>
</observers>
</controller_action_predispatch_checkout_cart_index>
</checkout_cart_save_after>
<core_block_abstract_to_html_before>
<observers>
<autoshipping>
Expand Down

0 comments on commit c86420e

Please sign in to comment.