├── README.md ├── app ├── code │ └── community │ │ └── IntegerNet │ │ └── Autoshipping │ │ ├── Block │ │ └── Country.php │ │ ├── Helper │ │ └── Data.php │ │ ├── Model │ │ ├── Observer.php │ │ └── Source │ │ │ └── ShippingMethods.php │ │ ├── controllers │ │ └── CountryController.php │ │ └── etc │ │ ├── config.xml │ │ └── system.xml ├── design │ └── frontend │ │ └── base │ │ └── default │ │ └── template │ │ └── checkout │ │ └── cart │ │ └── country.phtml ├── etc │ └── modules │ │ └── IntegerNet_Autoshipping.xml └── locale │ └── de_DE │ └── IntegerNet_Autoshipping.csv ├── composer.json └── modman /README.md: -------------------------------------------------------------------------------- 1 | IntegerNet_Autoshipping 2 | ===================== 3 | Allows you to display shipping costs in cart and change the country for which the shipping cost is calculated. 4 | 5 | Facts 6 | ----- 7 | - version: 0.3.0 8 | - extension key: IntegerNet_Autoshipping 9 | - [extension on GitHub](https://github.com/integer-net/Autoshipping) 10 | - [direct download link](https://github.com/integer-net/Autoshipping/archive/master.zip) 11 | 12 | Description 13 | ----------- 14 | This extension is a fork of [PRWD_Autoshipping](http://www.magentocommerce.com/magento-connect/prwd-auto-shipping.html). 15 | It displays the shipping costs on the shopping cart page even if you haven't entered an address yet. It takes the 16 | target country from the configuration. 17 | If there is more than one allowed country, a dropdown is available on the shopping cart page which allow the 18 | customer to change the target country. 19 | You can now exclude shipping methods by configuration (useful for pickup for example) 20 | 21 | Requirements 22 | ------------ 23 | - PHP >= 5.2.0 24 | - Mage_Core 25 | - Mage_Checkout 26 | 27 | Compatibility 28 | ------------- 29 | - Magento >= 1.4 30 | 31 | Installation Instructions 32 | ------------------------- 33 | 1. Clone the module into your document root. 34 | 2. Clear the cache, logout from the admin panel and then login again. 35 | 3. Configure and activate the extension under System - Configuration - Sales - Auto Shipping. 36 | 37 | Uninstallation 38 | -------------- 39 | 1. Remove all extension files from your Magento installation 40 | 41 | Support 42 | ------- 43 | If you have any issues with this extension, open an issue on [GitHub](https://github.com/integer-net/Autoshipping/issues). 44 | 45 | Contribution 46 | ------------ 47 | Any contribution is highly appreciated. The best way to contribute code is to open a [pull request on GitHub](https://help.github.com/articles/using-pull-requests). 48 | 49 | Developer 50 | --------- 51 | Andreas von Studnitz, [integer_net GmbH](http://www.integer-net.de) 52 | 53 | Twitter: [@integer_net](https://twitter.com/integer_net) 54 | 55 | Licence 56 | ------- 57 | [GNU General Public License 3.0](http://www.gnu.org/licenses/) 58 | 59 | Copyright 60 | --------- 61 | (c) 2014 integer_net GmbH 62 | -------------------------------------------------------------------------------- /app/code/community/IntegerNet/Autoshipping/Block/Country.php: -------------------------------------------------------------------------------- 1 | 10 | */ 11 | class IntegerNet_Autoshipping_Block_Country extends Mage_Directory_Block_Data 12 | { 13 | public function __construct() 14 | { 15 | $this->setTemplate('checkout/cart/country.phtml'); 16 | } 17 | 18 | /** 19 | * @return string 20 | */ 21 | public function getSelectedCountryId() 22 | { 23 | return Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getCountryId(); 24 | } 25 | 26 | public function getCountryHtmlSelect($defValue=null, $name='country_id', $id='country', $title='Country') 27 | { 28 | if (is_null($defValue)) { 29 | $defValue = $this->getCountryId(); 30 | } 31 | $cacheKey = 'DIRECTORY_COUNTRY_SELECT_STORE_'.Mage::app()->getStore()->getCode(); 32 | if (Mage::app()->useCache('config') && $cache = Mage::app()->loadCache($cacheKey)) { 33 | $options = unserialize($cache); 34 | } else { 35 | $options = $this->getCountryCollection()->toOptionArray(); 36 | if (Mage::app()->useCache('config')) { 37 | Mage::app()->saveCache(serialize($options), $cacheKey, array('config')); 38 | } 39 | } 40 | $html = $this->getLayout()->createBlock('core/html_select') 41 | ->setName($name) 42 | ->setId($id) 43 | ->setTitle(Mage::helper('directory')->__($title)) 44 | ->setClass('validate-select') 45 | ->setValue($defValue) 46 | ->setOptions($options) 47 | ->getHtml(); 48 | 49 | return $html; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /app/code/community/IntegerNet/Autoshipping/Helper/Data.php: -------------------------------------------------------------------------------- 1 | 10 | * @author PRWD (http://www.prwd.co.uk) 11 | */ 12 | 13 | class IntegerNet_Autoshipping_Helper_Data extends Mage_Core_Helper_Abstract 14 | { 15 | 16 | } 17 | 18 | -------------------------------------------------------------------------------- /app/code/community/IntegerNet/Autoshipping/Model/Observer.php: -------------------------------------------------------------------------------- 1 | 10 | * @author PRWD (http://www.prwd.co.uk) 11 | * */ 12 | 13 | class IntegerNet_Autoshipping_Model_Observer 14 | { 15 | protected $_methodManuallyChanged; 16 | 17 | /** 18 | * Set configured country 19 | * 20 | * @param Varien_Event_Observer $observer 21 | * @event checkout_cart_save_before 22 | */ 23 | public function prepareShippingAddress(Varien_Event_Observer $observer) 24 | { 25 | if (! Mage::getStoreConfigFlag('autoshipping/settings/enabled')) { 26 | return; 27 | } 28 | $quote = $this->_getCheckoutSession()->getQuote(); 29 | if (! $quote->hasItems()) { 30 | return; 31 | } 32 | if (!($country = $this->_getCoreSession()->getAutoShippingCountry())) { 33 | $country = Mage::getStoreConfig('autoshipping/settings/country_id'); 34 | $this->_getCoreSession()->setAutoShippingCountry($country); 35 | } 36 | 37 | $billingAddress = $quote->getBillingAddress(); 38 | if (!$billingAddress->getCountryId()) { 39 | $billingAddress->setCountryId($country); 40 | } 41 | 42 | $shippingAddress = $quote->getShippingAddress(); 43 | $shippingAddress->setCountryId($country); 44 | 45 | if (!$shippingAddress->getFreeMethodWeight()) { 46 | $shippingAddress->setFreeMethodWeight($shippingAddress->getWeight()); 47 | } 48 | 49 | $this->_methodManuallyChanged = $this->_isMethodManuallyChanged($shippingAddress); 50 | } 51 | /** 52 | * Set shipping method 53 | * 54 | * @param Varien_Event_Observer $observer 55 | * @event checkout_cart_save_after 56 | */ 57 | public function addShipping(Varien_Event_Observer $observer) 58 | { 59 | if (! Mage::getStoreConfigFlag('autoshipping/settings/enabled')) { 60 | return; 61 | } 62 | $quote = $this->_getCheckoutSession()->getQuote(); 63 | if (! $quote->hasItems()) { 64 | return; 65 | } 66 | 67 | $shippingAddress = $quote->getShippingAddress(); 68 | $shippingAddress->setCollectShippingRates(true)->collectShippingRates(); 69 | 70 | if($this->_methodManuallyChanged && $shippingAddress->getShippingMethod()) { 71 | // if the manually selected shipping method is still available, do nothing! 72 | return; 73 | } 74 | 75 | $rates = $shippingAddress->getGroupedAllShippingRates(); 76 | 77 | if (count($rates)) { 78 | 79 | $topRates = reset($rates); 80 | foreach($topRates as $topRate) { 81 | 82 | /** @var Mage_Sales_Model_Quote_Address_Rate $topRate */ 83 | 84 | if (in_array($topRate->getCarrier(), explode(',', Mage::getStoreConfig('autoshipping/settings/ignore_shipping_methods')))) { 85 | continue; 86 | } 87 | 88 | try { 89 | $shippingAddress->setShippingMethod($topRate->getCode()); 90 | $shippingDescription = $topRate->getCarrierTitle() . ' - ' . $topRate->getMethodTitle(); 91 | $shippingAddress->setShippingAmount($topRate->getPrice()); 92 | $shippingAddress->setBaseShippingAmount($topRate->getPrice()); 93 | $shippingAddress->setShippingDescription(trim($shippingDescription, ' -')); 94 | 95 | $quote->save(); 96 | 97 | $this->_getCheckoutSession()->resetCheckout(); 98 | 99 | $this->_getCheckoutSession()->setAutoShippingMethod($topRate->getCode()); 100 | 101 | } catch (Mage_Core_Exception $e) { 102 | $this->_getCheckoutSession()->addError($e->getMessage()); 103 | } 104 | catch (Exception $e) { 105 | $this->_getCheckoutSession()->addException( 106 | $e, Mage::helper('checkout')->__('Load customer quote error') 107 | ); 108 | } 109 | 110 | return; 111 | } 112 | } 113 | } 114 | 115 | /** 116 | * Show dropdown for country selection in cart before shipping cost 117 | * 118 | * @param Varien_Event_Observer $observer 119 | * @event core_block_abstract_to_html_before 120 | */ 121 | public function beforeBlockToHtml($observer) 122 | { 123 | $block = $observer->getBlock(); 124 | 125 | if (! Mage::getStoreConfigFlag('autoshipping/settings/show_country_selection_in_cart')) { 126 | return; 127 | } 128 | 129 | if ($block instanceof Mage_Tax_Block_Checkout_Shipping) { 130 | 131 | // show only on cart 132 | if (Mage::app()->getRequest()->getControllerName() != 'cart') { 133 | return; 134 | } 135 | 136 | // don't display if only 1 country allowed 137 | if (sizeof(explode(',', Mage::getStoreConfig('general/country/allow'))) <= 1) { 138 | return; 139 | } 140 | 141 | // replace total title 142 | $block->getTotal()->setTitle( 143 | $block->getLayout()->createBlock('autoshipping/country', 'checkout_cart_country_select')->toHtml() 144 | ); 145 | } 146 | } 147 | 148 | /** 149 | * @return Mage_Checkout_Model_Session 150 | */ 151 | protected function _getCheckoutSession() 152 | { 153 | return Mage::getSingleton('checkout/session'); 154 | } 155 | 156 | /** 157 | * @return Mage_Core_Model_Session 158 | */ 159 | protected function _getCoreSession() 160 | { 161 | return Mage::getSingleton('core/session'); 162 | } 163 | 164 | /** 165 | * @param $shippingAddress 166 | * @return bool 167 | */ 168 | protected function _isMethodManuallyChanged($shippingAddress) 169 | { 170 | return $shippingAddress->getShippingMethod() 171 | && ($shippingAddress->getShippingMethod() != $this->_getCheckoutSession()->getAutoShippingMethod()) 172 | && ($shippingAddress->getCountryId() == $this->_getCoreSession()->getAutoShippingCountry()); 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /app/code/community/IntegerNet/Autoshipping/Model/Source/ShippingMethods.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class IntegerNet_Autoshipping_Model_Source_ShippingMethods 11 | { 12 | /** 13 | * Options getter 14 | * 15 | * @return array 16 | */ 17 | public function toOptionArray() 18 | { 19 | $options = array(array( 20 | 'value' => '', 21 | 'label' => '', 22 | )); 23 | foreach(Mage::getStoreConfig('carriers') as $carrierCode => $carrierConfig) { 24 | $options[] = array( 25 | 'value' => $carrierCode, 26 | 'label' => Mage::getStoreConfig('carriers/' . $carrierCode . '/title') . ' [' . $carrierCode . ']', 27 | ); 28 | } 29 | return $options; 30 | } 31 | } -------------------------------------------------------------------------------- /app/code/community/IntegerNet/Autoshipping/controllers/CountryController.php: -------------------------------------------------------------------------------- 1 | 10 | */ 11 | class IntegerNet_Autoshipping_CountryController extends Mage_Core_Controller_Front_Action 12 | { 13 | public function selectAction() 14 | { 15 | $countryId = $this->getRequest()->getParam('country_id'); 16 | Mage::getSingleton('core/session')->setAutoShippingCountry($countryId); 17 | $this->_redirectReferer(); 18 | } 19 | } -------------------------------------------------------------------------------- /app/code/community/IntegerNet/Autoshipping/etc/config.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | 17 | 0.3.0 18 | 19 | 20 | 21 | 22 | 23 | IntegerNet_Autoshipping_Helper 24 | 25 | 26 | 27 | 28 | 29 | IntegerNet_Autoshipping_Model 30 | 31 | 32 | 33 | 34 | 35 | IntegerNet_Autoshipping_Block 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | singleton 45 | autoshipping/observer 46 | prepareShippingAddress 47 | 48 | 49 | 50 | 51 | 52 | 53 | singleton 54 | autoshipping/observer 55 | addShipping 56 | 57 | 58 | 59 | 60 | 61 | 62 | singleton 63 | autoshipping/observer 64 | beforeBlockToHtml 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | IntegerNet_Autoshipping.csv 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | Auto Shipping 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | IntegerNet_Autoshipping.csv 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | standard 117 | 118 | IntegerNet_Autoshipping 119 | autoshipping 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 1 129 | 130 | 131 | 132 | 133 | -------------------------------------------------------------------------------- /app/code/community/IntegerNet/Autoshipping/etc/system.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | 17 | 18 | sales 19 | text 20 | 314 21 | 1 22 | 1 23 | 1 24 | 25 | 26 | 27 | text 28 | 1 29 | 1 30 | 1 31 | 1 32 | 33 | 34 | 35 | select 36 | adminhtml/system_config_source_yesno 37 | 10 38 | 1 39 | 1 40 | 1 41 | 42 | 43 | 44 | select 45 | countries 46 | adminhtml/system_config_source_country 47 | 20 48 | 1 49 | 1 50 | 1 51 | 52 | 53 | 54 | select 55 | adminhtml/system_config_source_yesno 56 | 30 57 | 1 58 | 1 59 | 1 60 | 61 | 62 | 63 | multiselect 64 | autoshipping/source_shippingMethods 65 | 40 66 | 1 67 | 1 68 | 1 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /app/design/frontend/base/default/template/checkout/cart/country.phtml: -------------------------------------------------------------------------------- 1 | 2 | 3 | __('Shipping cost to', $this->getShippingCostPageUrl()) ?> 4 | 5 | getCountryHtmlSelect($this->getSelectedCountryId(), 'country_id', 'autoshipping_country') ?> 6 | 7 | -------------------------------------------------------------------------------- /app/etc/modules/IntegerNet_Autoshipping.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | true 7 | community 8 | 9 | 10 | -------------------------------------------------------------------------------- /app/locale/de_DE/IntegerNet_Autoshipping.csv: -------------------------------------------------------------------------------- 1 | "Enable Auto Shipping","Autoshipping aktivieren" 2 | "Shipping cost in","Versandkosten in" 3 | "Shipping cost to","Versand nach" 4 | "Default Country","Standard-Land" 5 | "Show Country Selection in Cart","Land-Auswahl im Warenkorb anzeigen" 6 | "Ignore Shipping Methods","Versandarten ignorieren" -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "integer-net/autoshipping", 3 | "type": "magento-module", 4 | "description": "Provide shipping costs in Magento Cart, plus the ability to change the country there", 5 | "homepage": "https://github.com/integer-net/Autoshipping", 6 | "require": { 7 | "magento-hackathon/magento-composer-installer": "*" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /modman: -------------------------------------------------------------------------------- 1 | app/code/community/IntegerNet/Autoshipping app/code/community/IntegerNet/Autoshipping 2 | app/etc/modules/IntegerNet_Autoshipping.xml app/etc/modules/IntegerNet_Autoshipping.xml 3 | app/design/frontend/base/default/template/checkout/cart/country.phtml app/design/frontend/base/default/template/checkout/cart/country.phtml 4 | app/locale/de_DE/IntegerNet_Autoshipping.csv app/locale/de_DE/IntegerNet_Autoshipping.csv 5 | --------------------------------------------------------------------------------