Jump to content

Adding carrier programmatically


PhpMadman

Recommended Posts

Hello

 

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

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->save();

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

$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)
Link to comment
Share on other sites

  • 2 years later...

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 1.6.1.5:

 

(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,";
$cart->setDeliveryOption($delivery_option);
$cart->update();

$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.

Link to comment
Share on other sites

  • 2 years later...

Hello,

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;
            $cart->add();

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

 

Link to comment
Share on other sites

  • 1 year later...
On 7/17/2019 at 8:39 AM, bhargavlalo said:

Hello,

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;
            $cart->add();

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

 

Hello, did you find a solution? I am experiencing the same problem.

I set delivery_option with the following code:

                $delivery_options = $cart->getDeliveryOptionList();                
                $delivery_options = array_values(reset($delivery_options));
                foreach ($delivery_options as $value) {
                    if (count($value['carrier_list']) == 1) {
                            if(array_key_first($value['carrier_list']) == $cart->id_carrier){
                                $cart->setDeliveryOption($value);
                            }
                    }
                }

Could anyone help me please?

Link to comment
Share on other sites

  • 1 year later...
On 7/17/2019 at 9:39 AM, bhargavlalo said:

Hello,

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;
            $cart->add();

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

 

every ID has to be integer (only numbers). Your id is with comma. Remove it.

p.s. I don't use "delivery_option" at all if only one option exist.

Screenshot_6.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...