Jump to content

The best way to create a new order in my custom module


Stepel

Recommended Posts

Hi,

what is the best way to create new order in my custom module ?
Should I create all elements by new Order(), new Cart() etc etc manually or there is some suggested method or class to do it ?


Thanks for your help.

Link to comment
Share on other sites

You will need to use or create a customer and an address to use as a delivery and billing address.

After that you will have to create a shopping cart, and add the products in it.

Next, you will need to call the validatedOrder() function in the PaymentModule class to create the order, and of course you need to assign it a carrier, and create the OrderCarrier object for the current order.

  • Like 1
Link to comment
Share on other sites

Hmm I have some doubts about using validatedOrder() method.

I created customer, addresses and cart but I see that i can add products by id_product. I would like to have influence on product price and carrier price. I see that I can set those prices in order_details and order so I am not sure if I should use validatedOrder method. I think that it will work like by API and will change prices to be equel with those prices in official product list etc..

Link to comment
Share on other sites

  • 1 month later...

Hi again. I did it like you suggested. Thank you very much. It looks almost all is OK but I have case like i created SpecificPrice.. and in my mind was that price included tax but in order finally product has calculated tax again hm. Where and when I can set kind of "use_tax" parameter ?

Link to comment
Share on other sites

	$sp = new SpecificPrice();
        $sp->id_shop = 0;
        $sp->id_cart = $cart_id;
        $sp->id_product = $product_id;
        $sp->id_currency = 0;
        $sp->id_country = 0;
        $sp->id_group = 0;
        $sp->id_shop_group = 0;
        $sp->id_customer = $customer_id;
        $sp->price = $price;
        $sp->from_quantity = 1;
        $sp->reduction = 0;
        $sp->reduction_tax = 1;
        $sp->reduction_type = 'amount';
        $sp->from = '0000-00-00 00:00:00';
        $sp->to = '0000-00-00 00:00:00';
        if ($sp->add()) {
            return $sp->id;
        } else {
            return null;
        }

I do it like that so I set price directly and reduction as 0.

Link to comment
Share on other sites

  • 2 weeks later...

Hmm I have weird situation yet. Maybe it's fault of some cache..
I changed email address in database for customer but this:
 

$this->context->customer = new Customer((int) $this->context->cart->id_customer);


returns email before changed 😕

Link to comment
Share on other sites

  • 3 weeks later...

To add a new order, you need to do the exact steps in checkout process.

1. Login as a customer or a guest

2. Add Ites to cart

3. Add a new address or create one and set as delivery and invoice address

4. Set a carrier

5. Place order with validateOrder method.

In order to use validate order, you need to create a payment module.

In conclusion, you have to create a payment module, which lets you validate an order and submit that.

 

Link to comment
Share on other sites

36 minutes ago, Stepel said:

@Ress Can you tell me what is the easiest or the best way to check availability/stock product before add to cart ?

You should use the StockAvailable class to get available quantities of a product, either simple or with combinations

Link to comment
Share on other sites

17 hours ago, Stepel said:

@Ress Can you tell me what is the easiest or the best way to check availability/stock product before add to cart ?

You can see exactly what checks are normally done, in CartController, processChangeProductInCart method. You can follow the same steps.

  • Like 1
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...