0

Magento 1.7

This is the code which gets called via the checkout_cart_product_add_after event when I add a product to the cart:

function addProductComplete(Varien_Event_Observer $observer) {

        $onepage = Mage::getSingleton('checkout/type_onepage');
        $onepage->saveShippingMethod("flatrate_flatrate");

        // Add address to cart
        $address = $onepage->getQuote()->getShippingAddress();

        if (null == $address->getCountryId())
        {
            $country = Mage::helper('core')->getDefaultCountry(); // use default country
            if ( '' == $country )
            {
                $country = 'DE';
            }
            $address->setCountryId($country);
        }

        if ( null == $address->getPostcode() )
        {
            $address->setPostcode('00000');
        }
        $address->setShippingMethod('flatrate_flatrate');
        $address->setCollectShippingrates(true);

        // save settings
        $onepage->getQuote()->collectTotals()->save();
        $onepage->getQuote()->save();

    }

When I add the first product, nothing happens, only after adding the second product, it works. I guess thats because the first time there is no quote object? How to solve that so it works with the first product as well?

1

I would suggest going with sales_quote_product_add_after instead of checkout_cart_product_add_after

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.