How To Create Order In Prestashop Programatically PHP

In this tutorial I will show you step by step how to create a new order in prestashop programatically, to achive this we will need to complete few steps before get to the order creation. Before to go to the order creation we need to already have an updated cart with all products we need to be added in our order, so if you don't know how to create cart programatically in prestashop you can use our tutorials on the website. Once you have the cart already created you can get all details you need from cart variable, customer and cookie so we can create our custom function for orders.

Create orders programatically function

You will have to change few parameters in the below function. For this tutorial I have used bank_wire transfer, as you can see in the second line I get the bank wire order status, the third line is for status "Payment accepted". You can use any status you need in your website. You will have to call the below function in createCart function, so after you create the cart then call the below function.


public function createOrder($address_id){
    $bank_wire_status = Configuration::get('PS_OS_BANKWIRE');
    $accepted_payment_status = Configuration::get('PS_OS_PAYMENT');
    $order_status = null;
    $custom_address_id = $this->context->cookie->custom_address_id;
    $order_object = new Order();
        $order_object->id_address_delivery = $address_id;
        $order_object->id_address_invoice = $address_id;
        $order_object->id_cart = $this->context->cart->id;
    $carrier = null;
    if (!$this->context->cart->isVirtualCart() && isset($package['id_carrier'])) {
        $carrier = new Carrier((int)$package['id_carrier'], (int)$this->context->cart->id_lang);
        $order_object->id_carrier = (int)$carrier->id;
        $id_carrier = (int)$carrier->id;
    } else {
        $order_object->id_carrier = 0;
        $id_carrier = 0;
        $order_object->id_currency = $this->context->cart->id_currency;
        $order_object->id_customer = $this->context->customer->id;
        $CarrierObject = new CarrierCore();
        $CarrierObject->delay[1] = "2-4";
        $CarrierObject->active = 1;
        $CarrierObject->name = "Custom Order Function";
        $id_carrier = $CarrierObject->id;
        $order_object->id_carrier = $id_carrier;
        $order_object->payment = "Payment";
        $order_object->module = "dashboard";
    $products = $this->context->cart->getProducts();
    $order_object->product_list = $this->context->cart->getProducts();
        $order_object->valid = 1;
    $order_object->total_products = (float)$this->context->cart->getOrderTotal(false, Cart::ONLY_PRODUCTS, $order_object->product_list, $id_carrier);
    $order_object->total_products_wt = (float)$this->context->cart->getOrderTotal(true, Cart::ONLY_PRODUCTS, $order_object->product_list, $id_carrier);
    $order_object->total_discounts_tax_excl = (float)abs($this->context->cart->getOrderTotal(false, Cart::ONLY_DISCOUNTS, $order_object->product_list, $id_carrier));
    $order_object->total_discounts_tax_incl = (float)abs($this->context->cart->getOrderTotal(true, Cart::ONLY_DISCOUNTS, $order_object->product_list, $id_carrier));
    $order_object->total_discounts = $order_object->total_discounts_tax_incl;
    $order_object->total_shipping_tax_excl = (float)$this->context->cart->getPackageShippingCost((int)$id_carrier, false, null, $order_object->product_list);
    $order_object->total_shipping_tax_incl = (float)$this->context->cart->getPackageShippingCost((int)$id_carrier, true, null, $order_object->product_list);
    $order_object->total_shipping = $order_object->total_shipping_tax_incl;
        $order_object->id_customer = (int)$this->context->cart->id_customer;
    $order_object->id_address_invoice = $address_id;
    $order_object->id_address_delivery = $address_id;
    $order_object->id_currency = $this->context->currency->id;
    $order_object->id_lang = (int)$this->context->cart->id_lang;
    $order_object->id_cart = (int)$this->context->cart->id;
    $order_object->reference = Order::generateReference();
    $order_object->id_shop = (int)$this->context->shop->id;
    $order_object->id_shop_group = (int)$this->context->shop->id_shop_group;
    $order_object->secure_key = $this->context->customer->secure_key;
    $order_object->payment = 'Bank Wire'; 
    $order_object->current_state = (int)$bank_wire_status; 
    $order_status = (int)$bank_wire_status;
    $order_object->total_wrapping_tax_excl = (float)abs($this->context->cart->getOrderTotal(false, Cart::ONLY_WRAPPING, $order_object->product_list, $id_carrier));
    $order_object->total_wrapping_tax_incl = (float)abs($this->context->cart->getOrderTotal(true, Cart::ONLY_WRAPPING, $order_object->product_list, $id_carrier));
    $order_object->total_wrapping = $order_object->total_wrapping_tax_incl;
    $order_object->conversion_rate = $this->context->currency->conversion_rate;
    $order_object->total_paid_tax_excl = (float)Tools::ps_round((float)$this->context->cart->getOrderTotal(false, Cart::BOTH, $order_object->product_list, $id_carrier), _PS_PRICE_COMPUTE_PRECISION_);
    $order_object->total_paid_tax_incl = (float)Tools::ps_round((float)$this->context->cart->getOrderTotal(true, Cart::BOTH, $order_object->product_list, $id_carrier), _PS_PRICE_COMPUTE_PRECISION_);
    $order_object->total_paid = $order_object->total_paid_tax_incl;
    $order_object->total_paid_real =$order_object->total_paid_tax_incl;
    $order_object->round_mode = Configuration::get('PS_PRICE_ROUND_MODE');
    $order_object->round_type = Configuration::get('PS_ROUND_TYPE');
    $order_date = new \DateTime();
    $order_date = $order_date->format('Y-m-d H:i:s');
    $order_object->invoice_date = $order_date;
    $order_object->delivery_date = $order_date;
        $order_id = $order_object->add();
    $order_list[] = $order_object;
    // Insert new Order detail list using cart for the current order
    $order_detail = new OrderDetail(null, null, $this->context);
    $order_detail->createList($order_object, $this->context->cart, $order_status, $order_object->product_list, 0, true);
    $order_detail_list[] = $order_detail;
    // Adding an entry in order_carrier table
    if (!is_null($carrier)) {
        $order_carrier = new OrderCarrier();
        $order_carrier->id_order = (int)$order_object->id;
        $order_carrier->id_carrier = (int)$id_carrier;
        $order_carrier->weight = (float)$order_object->getTotalWeight();
        $order_carrier->shipping_cost_tax_excl = (float)$order_object->total_shipping_tax_excl;
        $order_carrier->shipping_cost_tax_incl = (float)$order_object->total_shipping_tax_incl;
    $_GET['id_order'] = (int)$order_object->id; 

As you can see in the first line of this function I pass customer address id because we need to add this id to more fields in the $order object. Here you have all parameters you need to create order, save and add it in the back office prestashop. The order will appear on all stats and dashboard too. However this function is not complete because the above function only create the order and save it in the database but it won't create the invoice for the order and won't send the email confirmation to the client. To achive this we need to implement another function which will send email to the customer. I will show you how to send the email in a future tutorial. Let me know if this function worked for you as you expected and if not, let me know and I will help you to implement it on your own website.

4 1 vote
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x