Version Notes
First Release.
Tested under CE 1.7
Download this release
Release Info
Developer | Tony Hou |
Extension | autoshipping |
Version | 0.0.1 |
Comparing to | |
See all releases |
Version 0.0.1
app/code/community/TonyH/AutoShipping/Model/Observer.php
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
|
3 |
+
class TonyH_AutoShipping_Model_Observer {
|
4 |
+
|
5 |
+
public function addShipping($observer) {
|
6 |
+
|
7 |
+
$checkout = Mage::getSingleton('checkout/session');
|
8 |
+
$quote = $checkout->getQuote();
|
9 |
+
$shippingAddress = $quote->getShippingAddress();
|
10 |
+
|
11 |
+
//allow shipping rates recalculation
|
12 |
+
$shippingAddress->setCollectShippingRates(true);
|
13 |
+
|
14 |
+
//rest qutoe item counts so that shipping calculation is based on the correct quantity
|
15 |
+
$quote->collectTotals();
|
16 |
+
|
17 |
+
if ($quote->getItemsCount()) {
|
18 |
+
|
19 |
+
//first use the default Country code
|
20 |
+
$country = Mage::getStoreConfig('general/country/default');
|
21 |
+
|
22 |
+
//check if the customer has logged in
|
23 |
+
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
|
24 |
+
$customer = Mage::getSingleton('customer/session')->getCustomer();
|
25 |
+
if ($customer->getPrimaryShippingAddress() && $customer->getPrimaryShippingAddress()->getCountry()) {
|
26 |
+
//use customer's shipping address country if there's one
|
27 |
+
$country = $customer->getPrimaryShippingAddress()->getCountry();
|
28 |
+
}
|
29 |
+
}
|
30 |
+
|
31 |
+
//only set country if it does not exists already
|
32 |
+
if (!$shippingAddress->getCountryId()) {
|
33 |
+
$shippingAddress->setCountryId($country);
|
34 |
+
}
|
35 |
+
|
36 |
+
//get available rates
|
37 |
+
$rates = $shippingAddress->getGroupedAllShippingRates();
|
38 |
+
|
39 |
+
if (count($rates)) {
|
40 |
+
|
41 |
+
//get the top positioned rate. It's based on the position set up in backend
|
42 |
+
$topRate = reset($rates);
|
43 |
+
$rateToApply = $topRate[0]->getCode();
|
44 |
+
|
45 |
+
try {
|
46 |
+
|
47 |
+
//apply shipping
|
48 |
+
$shippingAddress->setShippingMethod($rateToApply);
|
49 |
+
$quote->save();
|
50 |
+
$checkout->resetCheckout();
|
51 |
+
} catch (Mage_Core_Exception $e) {
|
52 |
+
$checkout->addError($e->getMessage());
|
53 |
+
} catch (Exception $e) {
|
54 |
+
$checkout->addException(
|
55 |
+
$e, Mage::helper('checkout')->__('Load customer quote error')
|
56 |
+
);
|
57 |
+
}
|
58 |
+
}
|
59 |
+
}
|
60 |
+
|
61 |
+
return $this;
|
62 |
+
}
|
63 |
+
|
64 |
+
}
|
app/code/community/TonyH/AutoShipping/etc/config.xml
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0"?>
|
2 |
+
<config>
|
3 |
+
<modules>
|
4 |
+
<TonyH_AutoShipping>
|
5 |
+
<version>0.0.1</version>
|
6 |
+
</TonyH_AutoShipping>
|
7 |
+
</modules>
|
8 |
+
<global>
|
9 |
+
<models>
|
10 |
+
<tonyh_autoshipping>
|
11 |
+
<class>TonyH_AutoShipping_Model</class>
|
12 |
+
</tonyh_autoshipping>
|
13 |
+
</models>
|
14 |
+
</global>
|
15 |
+
<frontend>
|
16 |
+
<events>
|
17 |
+
<checkout_cart_update_items_after>
|
18 |
+
<observers>
|
19 |
+
<tonyh_autoshipping_observer>
|
20 |
+
<type>singleton</type>
|
21 |
+
<class>tonyh_autoshipping/observer</class>
|
22 |
+
<method>addShipping</method>
|
23 |
+
</tonyh_autoshipping_observer>
|
24 |
+
</observers>
|
25 |
+
</checkout_cart_update_items_after>
|
26 |
+
<checkout_cart_product_add_after>
|
27 |
+
<observers>
|
28 |
+
<tonyh_autoshipping_observer>
|
29 |
+
<type>singleton</type>
|
30 |
+
<class>tonyh_autoshipping/observer</class>
|
31 |
+
<method>addShipping</method>
|
32 |
+
</tonyh_autoshipping_observer>
|
33 |
+
</observers>
|
34 |
+
</checkout_cart_product_add_after>
|
35 |
+
<sales_quote_remove_item>
|
36 |
+
<observers>
|
37 |
+
<tonyh_autoshipping_observer>
|
38 |
+
<type>singleton</type>
|
39 |
+
<class>tonyh_autoshipping/observer</class>
|
40 |
+
<method>addShipping</method>
|
41 |
+
</tonyh_autoshipping_observer>
|
42 |
+
</observers>
|
43 |
+
</sales_quote_remove_item>
|
44 |
+
</events>
|
45 |
+
</frontend>
|
46 |
+
</config>
|
app/etc/modules/TonyH_AutoShipping.xml
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0"?>
|
2 |
+
<config>
|
3 |
+
<modules>
|
4 |
+
<TonyH_AutoShipping>
|
5 |
+
<active>true</active>
|
6 |
+
<codePool>community</codePool>
|
7 |
+
</TonyH_AutoShipping>
|
8 |
+
</modules>
|
9 |
+
</config>
|
package.xml
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0"?>
|
2 |
+
<package>
|
3 |
+
<name>autoshipping</name>
|
4 |
+
<version>0.0.1</version>
|
5 |
+
<stability>stable</stability>
|
6 |
+
<license uri="http://www.opensource.org/licenses/osl-3.0.php">OSL v3.0</license>
|
7 |
+
<channel>community</channel>
|
8 |
+
<extends/>
|
9 |
+
<summary>Automatically apply shipping when customers:
|
10 |
+
add items to cart,
|
11 |
+
update items in cart,
|
12 |
+
remove items from cart</summary>
|
13 |
+
<description>This extension automatically applies shipping based on two parameters:
|
14 |
+

|
15 |
+
Country:
|
16 |
+

|
17 |
+
When a customer first go to the shopping cart, shipping method is calculated using the store's default country (System->Configuration->General->Countries Options->Default Country).
|
18 |
+
If the customer is logged in, the country of the customer's shipping address is used.
|
19 |
+
If a customer selected a country and clicked 'get quote', the selected is used.
|
20 |
+

|
21 |
+
Sort order:
|
22 |
+

|
23 |
+
Sort order of shipping methods can be set up at System->Configuration->Shipping Methods->Shipping Method Name->Sort Order
|
24 |
+

|
25 |
+
Shipping method has the smallest sort order value will get applied.
|
26 |
+

|
27 |
+
For examle,
|
28 |
+

|
29 |
+
Set Free shipping to have a sort order of 1, Flat Rate to have a sort order of 2.
|
30 |
+

|
31 |
+
When freeshipping is available, it will be applied. When it is not available, flat rate is applied.
|
32 |
+

|
33 |
+
Shipping rates will get recalculated when the customer:
|
34 |
+

|
35 |
+
add items to cart
|
36 |
+
update items in cart
|
37 |
+
remove items from cart</description>
|
38 |
+
<notes>First Release.
|
39 |
+
Tested under CE 1.7</notes>
|
40 |
+
<authors><author><name>Tony Hou</name><user>tony2523</user><email>t@tonyhou.com</email></author></authors>
|
41 |
+
<date>2012-06-09</date>
|
42 |
+
<time>16:15:14</time>
|
43 |
+
<contents><target name="magecommunity"><dir name="TonyH"><dir name="AutoShipping"><dir name="Model"><file name="Observer.php" hash="09960487de4c45684bcea027cacc954b"/></dir><dir name="etc"><file name="config.xml" hash="030bff6fb5a13927164d4928979ec6c8"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="TonyH_AutoShipping.xml" hash="37a3f804f1e14b7c72a9dede8f455fcf"/></dir></target></contents>
|
44 |
+
<compatible/>
|
45 |
+
<dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php></required></dependencies>
|
46 |
+
</package>
|