0

when I create a new order in function.php of the theme using woocommerce plugin

$order = wc_create_order();

It shows me an error as

Fatal error: Call to a member function get_order() on a non-object in ../wc-order-functions.php on line 58

& when I open the file it contains

return WC()->order_factory->get_order( $the_order );

I got the documentation as

    $order = wc_create_order();
    $order->add_product( get_product( '2319' ), 2 );
    $order->set_address( $address, 'billing' );
    $order->set_address( $address, 'shipping' );
    $order->calculate_totals();

given on github but not working.

Please help me in this.Thanks in advance.

ngulhane02
  • 53
  • 1
  • 7

1 Answers1

0
 $order = wc_create_order();
 $custaddress=array(
       // array fields here
    );
        $order->add_product( get_product( '12' ), 2 ); //(get_product with id and next is for quantity)
        $order->set_address( $custaddress, 'billing' );
        $order->set_address( $custaddress, 'shipping' );
        $order->add_coupon('Fresher','10','2'); // accepted param $couponcode, $couponamount,$coupon_tax
        $order->calculate_totals();

Call this above code with your function then it will work accordingly.

Note : code is not working with earlier version 2.2.

Vasim Shaikh
  • 4,485
  • 2
  • 23
  • 52