Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php woocommerce通过编程添加多个产品_Php_Wordpress_Woocommerce_E Commerce_Product - Fatal编程技术网

Php woocommerce通过编程添加多个产品

Php woocommerce通过编程添加多个产品,php,wordpress,woocommerce,e-commerce,product,Php,Wordpress,Woocommerce,E Commerce,Product,您好,我想知道如何使用编码在订单中添加多个产品。 我目前正在使用此代码 $address = array( 'first_name' => 'random', 'last_name' => 'random', 'company' => '', 'email' => 'random@random.com', 'phone' => '987987987987',

您好,我想知道如何使用编码在订单中添加多个产品。 我目前正在使用此代码

 $address = array(
        'first_name' => 'random',
        'last_name'  => 'random',
        'company'    => '',
        'email'      => 'random@random.com',
        'phone'      => '987987987987',
        'address_1'  => 'hawa hawa havai',
        'address_2'  => '', 
        'city'       => 'Delhi',
        'state'      => 'New Delhi',
        'postcode'   => '11447788',
        'country'    => 'IN'
    );

    $order = wc_create_order();
    $order->add_product( wc_get_product('819'), 2 ); //(get_product with id and next is for quantity)
    $order->set_address( $address, 'billing' );
    $order->set_address( $address, 'shipping' );
    $order->calculate_totals();
我能够成功地添加一个产品,但如何同时添加多个产品。 我尝试了这个代码,但没有帮助

更新1

此代码有效

$order = wc_create_order();
$order->add_product( wc_get_product('819'), 2 ); // 819, 815... etc are product id.
$order->add_product( wc_get_product('815'), 1 ); // 2, 1, 3 are quantity of products.
$order->add_product( wc_get_product('835'), 3 ); 
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );
$order->calculate_totals();