Create an order programmatically with line items in Woocommerce

前端 未结 3 776
一个人的身影
一个人的身影 2020-12-01 04:00

I needed to create a Woocommerce order programatically, however using the \'old\' Woocommerce made this a very dirty procedure.

I had to insert all kind of database

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 04:14

    With latest version of WooCommerce is possible try this as something like

    $address = array(
                'first_name' => 'Fresher',
                'last_name'  => 'StAcK OvErFloW',
                'company'    => 'stackoverflow',
                'email'      => 'test@test.com',
                'phone'      => '777-777-777-777',
                'address_1'  => '31 Main Street',
                'address_2'  => '', 
                'city'       => 'Chennai',
                'state'      => 'TN',
                'postcode'   => '12345',
                'country'    => 'IN'
            );
    
            $order = wc_create_order();
            $order->add_product( get_product( '12' ), 2 ); //(get_product with id and next is for quantity)
            $order->set_address( $address, 'billing' );
            $order->set_address( $address, '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 it not work with old version of WooCommerce like 2.1.12, It works only from 2.2 of WooCommerce.

    Hope it helps

提交回复
热议问题