Woocommerce 在wc上添加装运创建订单(woo商务)

Woocommerce 在wc上添加装运创建订单(woo商务),woocommerce,Woocommerce,我已经成功地通过functions.php文件创建了订单 我需要帮助添加到订单的免费送货。你能帮我打这一行吗 我用于在functions.php上创建订单的代码: $lp_order = wc_create_order(); $lp_order->add_product( get_product( $lp_product_id ), 1 ); //(get_product with id and next is for quantity) $lp_order->

我已经成功地通过functions.php文件创建了订单

我需要帮助添加到订单的免费送货。你能帮我打这一行吗

我用于在functions.php上创建订单的代码:

    $lp_order = wc_create_order();
    $lp_order->add_product( get_product( $lp_product_id ), 1 ); //(get_product with id and next is for quantity)
    $lp_order->set_address( $lp_address_full, 'billing' );
    $lp_order->set_address( $lp_address_full, 'shipping' );
    $lp_order->update_status('processing');
    $lp_order->add_shipping();   
在最后一行,我应该向add_shipping函数传递什么数组?我试着用

$lp_订单->添加_配送(“免费配送”)

但它不起作用

functions.php的完整代码

function lp_create_order() {

    $lp_email = $_POST["lp_email"];
    $lp_firstname = $_POST["lp_firstname"];
    $lp_lastname = $_POST["lp_lastname"];
    $lp_phone = $_POST["lp_phone"];
    $lp_address_1 = $_POST["lp_address_1"];
    $lp_address_2 = $_POST["lp_address_2"];
    $lp_city = $_POST["lp_city"];
    $lp_state = $_POST["lp_state"];
    $lp_postcode = $_POST["lp_postcode"];
    $lp_product_id = $_POST["lp_product_id"];


    if ( isset( $lp_product_id ) ) {

        $lp_address_full = array(
            'first_name' => $lp_firstname,
            'last_name'  => $lp_lastname,
            'company'    => '',
            'email'      => $lp_email,
            'phone'      => $lp_phone,
            'address_1'  => $lp_address_1,
            'address_2'  => $lp_address_2, 
            'city'       => $lp_city,
            'state'      => $lp_state,
            'postcode'   => $lp_postcode,
            'country'    => 'CA'
        );

        $lp_order = wc_create_order();
        $lp_order->add_product( get_product( $lp_product_id ), 1 ); //(get_product with id and next is for quantity)
        $lp_order->set_address( $lp_address_full, 'billing' );
        $lp_order->set_address( $lp_address_full, 'shipping' );
        $lp_order->update_status('processing');
        //$lp_order->add_shipping('free_shipping',0);      
        //$lp_order->calculate_shipping();
        $lp_order->calculate_totals();

        print "Order is placed";

    } // end if

} // end lp_create_order
add_action( 'init', 'lp_create_order' );
add_shipping()

做这样的事

$lp_order->add_shipping(
   array (
        'id' => 'free_shipping',
        'label'    => "Free Shipping",
        'cost'     => 0.00,
        'taxes'    => array(),
        'calc_tax'  => 'per_order'
    )
);

请尝试以下配送方式:

$shippingAddress = array(
        'first_name' => $lp_firstname,
        'last_name'  => $lp_lastname,
        'company'    => '',
        'address_1'  => $lp_address_1,
        'address_2'  => $lp_address_2, 
        'city'       => $lp_city,
        'state'      => $lp_state,
        'postcode'   => $lp_postcode,
        'country'    => 'CA'
    );

只需删除电子邮件和电话,您就可以了。

对于订单的添加装运方法,这是我在Woo Commerce 2.6上成功实现的

$ship_rate_ob = new WC_Shipping_Rate();
$ship_rate_ob->id=0;
$ship_rate_ob->label='Free Shipping';
$ship_rate_ob->taxes=array(); //not required in your situation
$ship_rate_ob->cost=0; //not required in your situation
$lp_order->add_shipping($ship_rate_ob); 

我正在将运费的id设置为0。它目前正在工作,但我建议将其设置为数据库中现有的免费运费。您可以在其编辑页面的url中找到您的运费ID。

我得到了这个返回:警告:array_map():Argument#2应该是第338行/httpdocs/wp content/plugins/woocmerce/includes/abstracts/abstract-wc-order.php中的一个数组