Jump to content

Adding carrier programmatically

Recommended Posts



I'm makeing a module that contains it's own little order procces, but it's still using validateOrder from PrestaShop. I'm running

I got all but settings carrier to work.

The carrier id I want to set it to is 8, but when view it in  BO after it's been placed, it's using my default carrier with id 7. What am I missing?


$cart->id_carrier = 8;
$delivery_option = $cart->getDeliveryOption();
$delivery_option[$cart->id_address_delivery] =  8;

$id_product = (int)Tools::getValue('id_product');
$id_product_attribute = (int)Tools::getValue('id_product_attribute');
$quantity = 1;

$res = $cart->updateQty($quantity, $id_product, $id_product_attribute, false, 'up', $cart->id_address_delivery, $shop);
Edited by PhpMadman (see edit history)

Share this post

Link to post
Share on other sites

Hi Madman!


Maybe a bit late, but I've figured out the solution.


One problem is, that both id_carrier and $delivery_option[$cart->id_address_delivery] are not integers, but strings!


Also, the "$delivery_option[$cart->id_address_delivery]" string has a comma after it.


You have to also update the total price before you call the validateOrder function.



This code should work on PS


(if you plan to add a product to the cart, recalculate the $total variable after you change the cart content)

$cart->id_carrier = "8";
$delivery_option = $cart->getDeliveryOption();
$delivery_option[$cart->id_address_delivery] =  "8,";

$total = $this->context->cart->getOrderTotal(true, Cart::BOTH);

$this->module->validateOrder((int)$this->context->cart->id, Configuration::get('PS_OS_PREPARATION'), $total, $this->module->displayName, null, array(), null, false, $customer->secure_key);

I hope this will help somebody else.

Share this post

Link to post
Share on other sites


I am using below code for cart creation and inserting carrier.   Carrier is set perfectly in the cart but in validateOrder() method another carrier set.

            $cart = new Cart();
            $cart->id_shop_group = 1;
            $cart->id_shop = 1;
            $cart->id_customer = $this->id_customer;
            $cart->id_address_invoice = $this->id_billing_address;
            $cart->id_address_delivery = $this->id_shipping_address;
            $cart->id_carrier = 9;
            $cart->delivery_option = @serialize(array($this->id_shipping_address => '9,'));
            $cart->id_lang = 1;
            $cart->id_currency =1;
            $cart->secure_key = $customer->secure_key;
            $cart->recyclable = 0;
            $cart->gift = 0;

Anyone has a good idea to set the carrier in order which we set in cart?


Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More