wc_create_order上的add_shipping(woo commerce)

时间:2014-12-19 21:00:09

标签: woocommerce

我已成功通过functions.php文件创建订单。

我需要帮助将free_shipping添加到订单中。你能帮忙解决这个问题吗?

我用于在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_order-> add_shipping( 'free_shipping');

但它不起作用。

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' );

3 个答案:

答案 0 :(得分:1)

对于订单的add_shipping方法,这是我能够在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.它现在正在运行,但我建议将其设置为数据库中现有的free_shipping率。您可以在其编辑页面的网址中找到运费率ID。

答案 1 :(得分:0)

add_shipping()预计WC_Shipping_Rate数组,使用Woocommerce默认免费送货服务包。

做这样的事情。

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

答案 2 :(得分:0)

请尝试以下运费:

$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'
    );

只需删除电子邮件和手机就可以了。