7

I'm creating an order programmatically with code I wrote a while ago which functions correctly, when using the free shipping method (i.e freeshipping_freeshipping)

However, I'm now trying to place the order using a shipping method (which is from a module by WebShopApps).

$shipping_address->setCollectShippingRates(true)->collectShippingRates()
        ->setShippingMethod($delivery)
        ->setPaymentMethod($payment);

Where $delivery is the shipping method code. When I place the order, I get given an error

a:5:{i:0;s:33:"Please specify a shipping method.";i:1;s:1554:"#0 /chroot/home/site/site/html/app/code/core/Mage/Sales/Model/Service/Quote.php(303): Mage::throwException('Please specify ...')

When navigating to this file (Quote.php) on line 303:

protected function _validate()
{
    if (!$this->getQuote()->isVirtual()) {
        $address = $this->getQuote()->getShippingAddress();
        $addressValidation = $address->validate();
        if ($addressValidation !== true) {
            Mage::throwException(
                Mage::helper('sales')->__('Please check shipping address information. %s', implode(' ', $addressValidation))
            );
        }
        $method= $address->getShippingMethod();
        $rate  = $address->getShippingRateByCode($method);
        if (!$this->getQuote()->isVirtual() && (!$method || !$rate)) {
            Mage::throwException(Mage::helper('sales')->__('Please specify a shipping method.')); //this is line 303
        }
    }
    ....

To try and solve this, I went the route of creating an order via Adminhtml, and modifying Quote.php for testing purposes, to:

echo ($method . " and " . $rate);die();

I'm then given a blank page with the method Magento is expecting as well as the rate (which is blank), yet the order still gets created fine. The method Magento prints out is also the exact same value that my $delivery variable had set.

What am I doing wrong?

3
  • 1
    Please check free shipping is enable in admin
    – Amit Bera
    Commented Feb 10, 2014 at 11:32
  • Free shipping is enabled
    – Moose
    Commented Feb 10, 2014 at 11:33
  • Did you try setting the shipping method before calling collectShippingRates()? Commented Mar 29, 2014 at 10:02

4 Answers 4

2

You should add $quote->getShippingAddress()->collectTotals(); before your above code. It will work :

/* Important*/
 $quote->getShippingAddress()->collectTotals();// if not will show Please specify shipping methods.
 /* Important*/

$shipping_address->setCollectShippingRates(true)->collectShippingRates()
        ->setShippingMethod($delivery)
        ->setPaymentMethod($payment);
1
  • 2
    tried too. the rate not calculated so rate always return false/empty. Magento 1.9.x
    – Osify
    Commented Jul 27, 2016 at 10:49
1
$shipping_method = "flatrate_flatrate";
$shipping_address->setShippingMethod($shipping_method)    
$shipping_address->setCollectShippingRates(true)->collectShippingRates();

$quoteObj->collectTotals()->save();

Above worked for me.

2
  • adding your solution into sales_quote_collect_totals_before gives me Fatal error: Maximum function nesting level of '500' reached, aborting!
    – rbncha
    Commented Mar 27, 2015 at 17:23
  • 2
    tried, not work... setCollectShippingRates the rate not calculated so rate always return false/empty. Magento 1.9.x
    – Osify
    Commented Jul 27, 2016 at 10:49
0

This seems completely unrelated to the WebShopApps carrier itself, it seems that there are no shipping rates returned from for your request.

Can you investigate whether your shippingAddress and quote object are correctly populated when you are programatically creating them? I suggest testing with another Magento in built carrier such as Tablerates, as the free_shipping carrier does not use any fields on the request to return shipping rates.

0

for each quote item:

$quoteItem->setFreeShipping(true);

then you can set freeshipping method to your quote

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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