如何以编程方式创建订单

时间:2019-10-27 07:06:08

标签: wordpress woocommerce

wc_create_order运行多个,按顺序记录多行,我在发布更新前调用打击功能

函数custom_wc_create_order _($ id,$ user_id){

global $woocommerce;

$address = array(
    'first_name' => 'Remi',
    'last_name'  => 'Corson',
    'company'    => 'Automattic',
    'email'      => 'no@spam.com',
    'phone'      => '123-123-123',
    'address_1'  => '123 Main Woo st.',
    'address_2'  => '100',
    'city'       => 'San Francisco',
    'state'      => 'Ca',
    'postcode'   => '92121',
    'country'    => 'US'
);

// Now we create the order
$order = wc_create_order(array('_customer_user'=>$user_id));

// The add_product() function below is located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php
$order->add_product( get_product( $id ), 1); // Use the product IDs to add

// Set addresses
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );

// Set payment gateway
$payment_gateways = WC()->payment_gateways->payment_gateways();
$order->set_payment_method( $payment_gateways['bacs'] );

// Calculate totals
$order->calculate_totals();
$order->update_status( 'wc-completed', 'Order created dynamically - ', TRUE);

}

1 个答案:

答案 0 :(得分:0)

在woocomerce源代码中查找wc_create_order后,请参见此代码
并在使用此功能时对我来说很好

$order = new WC_Order(  );

$order->set_status( 'wc-completed' );
// Update other order props set automatically.
$order->set_customer_id( is_numeric( $user_id ) ? absint( $user_id ) : 0 );
$order->add_product( get_product( $id ), 1 );
$order->save();