簡體   English   中英

在 Woocommerce 3+ 中使用訂單項以編程方式創建訂單

[英]Create an order programmatically with line items in Woocommerce 3+

我需要以編程方式創建 Woocommerce 訂單,但是使用“舊”Woocommerce 使這成為一個非常臟的過程。

我不得不手動插入所有類型的數據庫記錄,使用許多 update_post_meta 調用。

尋找更好的解決方案。

使用最新版本的 WooCommerce 可以嘗試像這樣

$address = array(
            'first_name' => 'Fresher',
            'last_name'  => 'StAcK OvErFloW',
            'company'    => 'stackoverflow',
            'email'      => 'test@test.com',
            'phone'      => '777-777-777-777',
            'address_1'  => '31 Main Street',
            'address_2'  => '', 
            'city'       => 'Chennai',
            'state'      => 'TN',
            'postcode'   => '12345',
            'country'    => 'IN'
        );

        $order = wc_create_order();
        $order->add_product( get_product( '12' ), 2 ); //(get_product with id and next is for quantity)
        $order->set_address( $address, 'billing' );
        $order->set_address( $address, 'shipping' );
        $order->add_coupon('Fresher','10','2'); // accepted param $couponcode, $couponamount,$coupon_tax
        $order->calculate_totals();

用你的函數調用上面的代碼,然后它就會相應地工作。

請注意,它不適用於舊版本的 WooCommerce,例如 2.1.12,它僅適用於 WooCommerce 的 2.2。

希望能幫助到你

2017-2021 WooCommerce 3 及更高版本

Woocommerce 3 引入了 CRUD 對象,並且訂單項有很多變化……此外,一些WC_Order方法現在已被棄用,例如add_coupon()

這是一個允許以編程方式很好地創建訂單的函數,其中包含所有必需的數據,包括稅收:

function create_wc_order( $data ){
    $gateways = WC()->payment_gateways->get_available_payment_gateways();
    $order    = new WC_Order();

    // Set Billing and Shipping adresses
    foreach( array('billing_', 'shipping_') as $type ) {
        foreach ( $data['address'] as $key => $value ) {
            if( $type === 'shipping_' && in_array( $key, array( 'email', 'phone' ) ) )
                continue;

            $type_key = $type.$key;

            if ( is_callable( array( $order, "set_{$type_key}" ) ) ) {
                $order->{"set_{$type_key}"}( $value );
            }
        }
    }

    // Set other details
    $order->set_created_via( 'programatically' );
    $order->set_customer_id( $data['user_id'] );
    $order->set_currency( get_woocommerce_currency() );
    $order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
    $order->set_customer_note( isset( $data['order_comments'] ) ? $data['order_comments'] : '' );
    $order->set_payment_method( isset( $gateways[ $data['payment_method'] ] ) ? $gateways[ $data['payment_method'] ] : $data['payment_method'] );

    $calculate_taxes_for = array(
        'country'  => $data['address']['country'],
        'state'    => $data['address']['state'],
        'postcode' => $data['address']['postcode'],
        'city'     => $data['address']['city']
    );

    // Line items
    foreach( $data['line_items'] as $line_item ) {
        $args = $line_item['args'];
        $product = wc_get_product( isset($args['variation_id']) && $args['variation_id'] > 0 ? $$args['variation_id'] : $args['product_id'] );
        $item_id = $order->add_product( $product, $line_item['quantity'], $line_item['args'] );

        $item    = $order->get_item( $item_id, false );

        $item->calculate_taxes($calculate_taxes_for);
        $item->save();
    }

    // Coupon items
    if( isset($data['coupon_items'])){
        foreach( $data['coupon_items'] as $coupon_item ) {
            $order->apply_coupon(sanitize_title($coupon_item['code']));
        }
    }

    // Fee items
    if( isset($data['fee_items'])){
        foreach( $data['fee_items'] as $fee_item ) {
            $item = new WC_Order_Item_Fee();

            $item->set_name( $fee_item['name'] );
            $item->set_total( $fee_item['total'] );
            $tax_class = isset($fee_item['tax_class']) && $fee_item['tax_class'] != 0 ? $fee_item['tax_class'] : 0;
            $item->set_tax_class( $tax_class ); // O if not taxable

            $item->calculate_taxes($calculate_taxes_for);

            $item->save();
            $order->add_item( $item );
        }
    }

    // Set calculated totals
    $order->calculate_totals();
        
    if( isset($data['order_status']) ) {
        // Update order status from pending to your defined status and save data
        $order->update_status($data['order_status']['status'], $data['order_status']['note']);
        $order_id = $order->get_id();
    } else {
        // Save order to database (returns the order ID)
        $order_id = $order->save();
    }
    
    // Returns the order ID
    return $order_id;
}

代碼位於活動子主題(或活動主題)的 function.php 文件或插件文件中。


來自數據數組的用法示例

create_wc_order( array(
    'address' => array(
        'first_name' => 'Fresher',
        'last_name'  => 'StAcK OvErFloW',
        'company'    => 'stackoverflow',
        'email'      => 'test1@testoo.com',
        'phone'      => '777-777-777-777',
        'address_1'  => '31 Main Street',
        'address_2'  => '',
        'city'       => 'Chennai',
        'state'      => 'TN',
        'postcode'   => '12345',
        'country'    => 'IN',
    ),
    'user_id'        => '',
    'order_comments' => '',
    'payment_method' => 'bacs',
    'order_status'   => array(
        'status' => 'on-hold',
        'note'   => '',
    ),
    'line_items' => array(
        array(
            'quantity' => 1,
            'args'     => array(
                'product_id'    => 37,
                'variation_id'  => '',
                'variation'     => array(),
            )
        ),
    ),
    'coupon_items' => array(
        array(
            'code'         => 'summer',
        ),
    ),
    'fee_items' => array(
        array(
            'name'      => 'Delivery',
            'total'     => 5,
            'tax_class' => 0, // Not taxable
        ),
    ),
) );

隨着 WC 2 的新版本,它變得更好了。

然而:

  • 我不想使用 REST API,因為我直接從我自己的 WP 插件進行調用。 我認為對我的本地主機做 curl 沒有用
  • WooCommerce REST API 客戶端庫”對我沒有用,因為它在 REST API 上中繼,並且不支持創建訂單調用

老實說,WooCom 的API Docs是有限的,也許他們還在更新它。 他們目前沒有告訴我如何創建新訂單,需要哪些參數等。

無論如何,我想出了如何使用 REST API 使用的類和函數創建帶有行訂單(您的產品)的訂單,我想分享它!

我創建了自己的 PHP 類:

class WP_MyPlugin_woocommerce
{

public static function init()
{
    // required classes to create an order
    require_once WOOCOMMERCE_API_DIR . 'class-wc-api-exception.php';
    require_once WOOCOMMERCE_API_DIR . 'class-wc-api-server.php';
    require_once WOOCOMMERCE_API_DIR . 'class-wc-api-resource.php';
    require_once WOOCOMMERCE_API_DIR . 'interface-wc-api-handler.php';
    require_once WOOCOMMERCE_API_DIR . 'class-wc-api-json-handler.php';
    require_once WOOCOMMERCE_API_DIR . 'class-wc-api-orders.php';
}

public static function create_order()
{
    global $wp;

    // create order
    $server = new WC_API_Server( $wp->query_vars['wc-api-route'] );
    $order = new WC_API_Orders( $server );

    $order_id = $order->create_order( array
    (
        'order'             => array
        (
           'status'            => 'processing'
        ,  'customer_id'       =>  get_current_user_id()
        // ,   'order_meta'        => array
        //     (
        //        'some order meta'         => 'a value
        //     ,   some more order meta'    => 1
        //     )
        ,   'shipping_address'        => array
            (
                'first_name'          => $firstname
            ,   'last_name'           => $lastname
            ,   'address_1'           => $address
            ,   'address_2'           => $address2
            ,   'city'                => $city
            ,   'postcode'            => $postcode
            ,   'state'               => $state
            ,   'country'             => $country
            )

        ,   'billing_address'        => array(..can be same as shipping )

        ,   'line_items'        => array
            (
                array
                (
                    'product_id'         => 258
                ,   'quantity'           => 1
                )
            )
        )
    ) );
    var_dump($order_id);
    die();
}
}

重要的:

  • 'WOOCOMMERCE_API_DIR' 常量指向插件目錄中的 '/woocommerce/includes/api/'。
  • 訂單被分配給一個客戶,在我的例子中是當前登錄的用戶。 確保您的用戶具有能夠讀取、編輯、創建和刪除訂單的角色。 我的角色是這樣的:

     $result = add_role( 'customer' , __( 'Customer' ) , array ( 'read' => true // , 'read_private_posts' => true // , 'read_private_products' => true , 'read_private_shop_orders' => true , 'edit_private_shop_orders' => true , 'delete_private_shop_orders' => true , 'publish_shop_orders' => true // , 'read_private_shop_coupons' => true , 'edit_posts' => false , 'delete_posts' => false , 'show_admin_bar_front' => false ) );
  • 如果您想查看店長的權限,請查看

    var_dump(get_option('wp_user_roles'));

我的 create_order 函數很好地創建了一個訂單,其中 lineitem 位於 order_items 表中。

希望我能幫到你,我花了一段時間才弄好。

很多答案是否顯示了要使用的最佳過濾器(掛鈎)。 我搜索了好幾天,因為當我使用“init”時,它不斷發出更多訂單。 在 2 分鍾內,我有 30 個訂單。 無論如何使用此代碼,將創建 1 個訂單。 另外,將 $product = wc_get_product( '1001' ) 更改為您的產品 ID。參考在第 144 行: https : //github.com/dipolukarov/wordpress/blob/master/wp-content/plugins/woocommerce/classes/ class-wc-checkout.php

function my_init2() {
    $order    = wc_create_order();
    $order_id = $order->get_id();
    
    $product = wc_get_product( '10001' );
    
    $address = array(
        'first_name' => 'John2',
        'last_name'  => 'Smith1',
        'email'      => 'johnsmith1@gmail.com',
    );
    //$order->date_created(2020-07-21 );
    $order->add_product( $product, 1 );
    $order->set_address( $address, 'billing' );
    $order->set_created_via( 'programatically' );
    $order->calculate_totals();
    $order->set_total( $product->get_price() );
    $order->update_status( 'wc-completed' );
    
    error_log( '$order_id: ' . $order_id );
    
}          
//add_action('admin_init','my_init2');
function my_run_only_once() {
 
    if ( get_option( 'my_run_only_once_20' ) != 'completed' ) {
  my_init2();
        update_option( 'my_run_only_once_20', 'completed' );
    }
}
add_action( 'admin_init', 'my_run_only_once' );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM