1

I want in cart add also and shipping cost:

enter image description here

Let me know if this possible?

  • I see no default shipping calculator at the screen. If you use it for some address, magento will add the shipping subtotal to the cart automatically. You may, for example, check how it works and run the same methods with default address on page load – Amasty Jul 30 '14 at 20:47
  • can you tell me how i can check? – John Genry Jul 30 '14 at 21:20
  • Take a look at the magento.stackexchange.com/questions/14576/… – Amasty Jul 30 '14 at 21:28
1

First, create a module definition file; in

app/etc/modules, create the file BTS_Checkout.xml:

<config>
    <modules>
        <BTS_Checkout>
            <active>true</active>
            <codePool>local</codePool>
        </BTS_Checkout>
    </modules>
</config>

Create a file:

app/code/local/BTS/Checkout/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <BTS_Checkout>
            <version>0.0.1</version>
        </BTS_Checkout>
    </modules>
    <global>
        <models>
            <bts_checkout>
                <class>BTS_Checkout_Model</class>
            </bts_checkout>
        </models>
    </global>
    <frontend>
        <events>
            <checkout_cart_save_before>
                <observers>
                    <bts_checkout_observer>
                        <type>singleton</type>
                        <class>bts_checkout/observer</class>
                        <method>addShipping</method>
                    </bts_checkout_observer>
                </observers>
            </checkout_cart_save_before>
        </events>
    </frontend>
</config>

Create Observer.php at :

app/code/local/BTS/Checkout/Model

class BTS_Checkout_Model_Observer {

    private $_shippingCode = 'royalmail';
    private $_country = 'GB';

    public function addShipping($params = null) {
        if (Mage::registry('checkout_addShipping')) {
            Mage::unregister('checkout_addShipping');
            return;
        }
        Mage::register('checkout_addShipping',true);

        $cart = Mage::getSingleton('checkout/cart');
        $quote = $cart->getQuote();

        if ($quote->getCouponCode() != '') {
            $c = Mage::getResourceModel('salesrule/rule_collection');
            $c->getSelect()->where("code=?", $quote->getCouponCode());
            foreach ($c->getItems() as $item) { $coupon = $item; }

            if ($coupon->getSimpleFreeShipping() > 0) {
                $quote->getShippingAddress()->setShippingMethod($this->_shippingCode)->save();
                return true;
            }
        }

        try {
            $method = $quote->getShippingAddress()->getShippingMethod();
            if ($method) return; // don't overwrite what's already there if we have one set already

            if ($quote->getShippingAddress()->getCountryId() == '') {
                $quote->getShippingAddress()->setCountryId($this->_country);
            }

            $quote->getShippingAddress()->setCollectShippingRates(true);
            $quote->getShippingAddress()->collectShippingRates();

            $rates = $quote->getShippingAddress()->getAllShippingRates();
            $allowed_rates = array();
            foreach ($rates as $rate) {
                array_push($allowed_rates,$rate->getCode());
            }

            if (!in_array($this->_shippingCode,$allowed_rates) && count($allowed_rates) > 0) {
                $shippingCode = $allowed_rates[0];
            }

            if (!empty($shippingCode)) {
                $address = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress();
                if ($address->getCountryId() == '') $address->setCountryId($this->_country);
                if ($address->getCity() == '') $address->setCity('');
                if ($address->getPostcode() == '') $address->setPostcode('');
                if ($address->getRegionId() == '') $address->setRegionId('');
                if ($address->getRegion() == '') $address->setRegion('');
                $address->setShippingMethod($this->_shippingCode)->setCollectShippingRates(true);
                Mage::getSingleton('checkout/session')->getQuote()->save();
            } else {
                $address = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress();
                if ($address->getCountryId() == '') $address->setCountryId($this->_country);
                if ($address->getCity() == '') $address->setCity('');
                if ($address->getPostcode() == '') $address->setPostcode('');
                if ($address->getRegionId() == '') $address->setRegionId('');
                if ($address->getRegion() == '') $address->setRegion('');
                $address->setShippingMethod($this->_shippingCode)->setCollectShippingRates(true);
                Mage::getSingleton('checkout/session')->getQuote()->save();
            }

            Mage::getSingleton('checkout/session')->resetCheckout();

        }
        catch (Mage_Core_Exception $e) {
            Mage::getSingleton('checkout/session')->addError($e->getMessage());
        }
        catch (Exception $e) {
            Mage::getSingleton('checkout/session')->addException($e, Mage::helper('checkout')->__('Load customer quote error'));
        }


    }

    public function getQuote() {
        if (empty($this->_quote)) {
            $this->_quote = Mage::getSingleton('checkout/session')->getQuote();
        }
        return $this->_quote;
    }

}

This is the Tutorial that I Copied, don't want to share the link alone so added code along

| improve this answer | |
  • ok, done, but i not see changes and in cart block not visible Shipping with cost((, what wrong? i use magento 1.8.1.0 – John Genry Jul 30 '14 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.