使用function.php中的wc_create_order()函数创建自定义顺序时出错

时间:2016-11-26 11:25:54

标签: php wordpress woocommerce

我正在尝试使用function.php中的wc_create_order()函数在woocommerce中创建自定义顺序。但它会产生错误并创建空订单。但是,如果我在模板中使用此代码,它将完美运行。代码如下:

    function change_stock_status(){
    $loop = new WP_Query( array(  'post__not_in' => array(652), 'post_type' =>        'product', 'posts_per_page' => -1 ) );

    while ( $loop->have_posts()  ):
    $loop->the_post();
    $theid = get_the_ID();

        $targetprice = get_post_meta($theid, '_crowdfundinggettargetprice', true);
        $totalprice = get_post_meta($theid, '_crowdfundingtotalprice', true);
  $stock = get_post_meta($theid, '_stock_status', true);   


        if($stock == 'instock'){
        if (($totalprice == $targetprice) || ($totalprice > $targetprice)) {
        global $woocommerce;
        global $wordpress;

        update_post_meta($theid, '_stock_status', 'outofstock');
        $post_author_id = get_post_field( 'post_author', $theid );

        $product_id = 652;
        $billing_first_name = get_user_meta($post_author_id, 'billing_first_name', true);
        $billing_last_name = get_user_meta($post_author_id, 'billing_last_name', true);
        $billing_company = get_user_meta($post_author_id, 'billing_company', true);
        $billing_address_1 = get_user_meta($post_author_id, 'billing_address_1', true);
        $billing_address_2 = get_user_meta($post_author_id, 'billing_address_2', true);
        $billing_city = get_user_meta($post_author_id, 'billing_city', true);
        $billing_state = get_user_meta($post_author_id, 'billing_state', true);
        $billing_postcode = get_user_meta($post_author_id, 'billing_postcode', true);
        $billing_country = get_user_meta($post_author_id, 'billing_country', true);
       $billing_email = get_user_meta($post_author_id, 'billing_email', true);
       $billing_phone = get_user_meta($post_author_id, 'billing_phone', true);

       $shipping_first_name = get_user_meta($post_author_id, 'shipping_first_name', true);
       $shipping_last_name = get_user_meta($post_author_id, 'shipping_last_name', true);
       $shipping_company = get_user_meta($post_author_id, 'shipping_company', true);
       $shipping_address_1 = get_user_meta($post_author_id, 'shipping_address_1', true);
       $shipping_address_2 = get_user_meta($post_author_id, 'shipping_address_2', true);
       $shipping_city = get_user_meta($post_author_id, 'shipping_city', true);
       $shipping_state = get_user_meta($post_author_id, 'shipping_state', true);
       $shipping_postcode = get_user_meta($post_author_id, 'shipping_postcode', true);
       $shipping_country = get_user_meta($post_author_id, 'shipping_country', true);
       $shipping_email = get_user_meta($post_author_id, 'shipping_email', true);
       $shipping_phone = get_user_meta($post_author_id, 'shipping_phone', true);


        $billing_address = array(
            'first_name' => $billing_first_name,
            'last_name'  => $billing_last_name,
            'company'    => $billing_company,
            'email'      => $billing_email,
            'phone'      => $billing_phone,
            'address_1'  => $billing_address_1,
            'address_2'  => $billing_address_2, 
            'city'       => $billing_city,
            'state'      => $billing_state,
            'postcode'   => $billing_postcode,
            'country'    => $billing_country
           );

           $shipping_address = array(
            'first_name' => $shipping_first_name,
            'last_name'  => $shipping_last_name,
            'company'    => $shipping_company,
            'email'      => $shipping_email,
            'phone'      => $shipping_phone,
            'address_1'  => $shipping_address_1,
            'address_2'  => $shipping_address_2, 
            'city'       => $shipping_city,
            'state'      => $shipping_state,
            'postcode'   => $shipping_postcode,
            'country'    => $shipping_country
            );

            $order = wc_create_order();
            $order->add_product( get_product( $product_id ), 1 ); //(get_product  with id and next is for quantity)
            $order->set_address( $billing_address, 'billing' );
            $order->set_address( $shipping_address, 'shipping' );
            //$order->add_coupon('Fresher','10','2');  // accepted param $couponcode, $couponamount,$coupon_tax
            $order->calculate_totals();
            update_post_meta($order->id, '_customer_user', $post_author_id );
            $order->payment_complete(); 


            } } 
            endwhile;
            }
            change_stock_status();

请帮我解决这个问题。

0 个答案:

没有答案