簡體   English   中英

在 Woocommerce 訂閱中以編程方式創建訂單

[英]Create an order programmatically in Woocommerce Subscription

我想創建訂閱產品的訂單(以編程方式)。 我從這個問題中找到了代碼: Programmatically created new order in Woocommerce ,我曾經嘗試讓它工作。

無論是“簡單訂閱”還是“簡單產品”,我都無法使其正常工作。

 function create_vip_order() {

  global $woocommerce;
  $product_id = 123;

  $address = array(
      'first_name' => '111Joe',
      'last_name'  => 'Conlin',
      'company'    => 'Speed Society',
      'email'      => 'joe@testing.com',
      'phone'      => '760-555-1212',
      'address_1'  => '123 Main st.',
      'address_2'  => '104',
      'city'       => 'San Diego',
      'state'      => 'Ca',
      'postcode'   => '92121',
      'country'    => 'US'
  );

  // Now we create the order
  $order = wc_create_order();

  // The add_product() function below is located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php
  $order->add_product( get_product( $product_id ), 1 ); // This is an existing SIMPLE product
  $order->set_address( $address, 'billing' );
  //
  $order->calculate_totals();
  $order->update_status("Completed", 'Imported order', TRUE);

}

add_action( 'woocommerce_checkout_process', 'create_vip_order' );

如果您有任何解決方案,我將不勝感激。 :)

最好的問候,樂東

編輯:似乎我的產品設置方式導致該功能不起作用。 我目前使用的是簡單訂閱產品,選中“虛擬”參數,訂閱期限為 3 個月。

在這里您可以看到適用於我的設置的當前代碼:

    function create_test_sub() {

    $email = 'test@test.se';

    $start_date = date( "Y-m-d H:i:s", strtotime( "now" ) );


    $address = array(
        'first_name' => 'Firstname',
        'last_name'  => 'Secondname',
        'company'    => 'Company',
        'email'      => $email,
        'phone'      => '',
        'address_1'  => 'Streetname 123',
        'address_2'  => '',
        'city'       => 'City',
        'postcode'   => '12345',
        'country'    => 'Country'
    );

    $default_password = wp_generate_password();

    if ( ! $user = get_user_by( 'login', $email ) ) $user = wp_create_user( $email, $default_password, $email );

    // I've used one product with multiple variations

    $parent_product = wc_get_product( 3901 );

    $args = array(
        'attribute_billing-period'    => 'Yearly',
        'attribute_subscription-type' => 'Both'
    );

    $product_variation = $parent_product->get_matching_variation( $args );

    $product = wc_get_product( $product_variation );

    $product_month_length = $parent_product->subscription_length;
    $end_date = date( "Y-m-d H:i:s", strtotime( $product_month_length . "months" ) );

    $quantity = 1;

    // As far as I can see, you need to create the order first, then the sub

    $order = wc_create_order( array( 'customer_id' => $user->id ) );

    $order->add_product( $parent_product, $quantity, $args );
    $order->set_address( $address, 'billing' );
    $order->set_address( $address, 'shipping' );


    $order->calculate_totals();

    $order->update_status( "completed", 'Imported order', TRUE );

    // Order created, now create sub attached to it -- optional if you're not creating a subscription, obvs

    // Each variation has a different subscription period

    $period = WC_Subscriptions_Product::get_period( $parent_product );
    $interval = WC_Subscriptions_Product::get_interval( $product );


    $sub = wcs_create_subscription( array( 'status' => 'active', 'order_id' => $order->id, 'billing_period' => $period, 'billing_interval' => $interval, 'start_date' => $start_date, 'end' => $end_date ) );

    if ( ! is_wp_error( $sub ) ){
      $sub->add_product( $parent_product, $quantity, $args );

      $sub->set_address( $address, 'billing' );
      $sub->set_address( $address, 'shipping' );
      $dates = array(
        'end' => $end_date,
      );
      $sub->update_dates( $dates );
      $sub->calculate_totals();
    }
    WC_Subscriptions_Manager::activate_subscriptions_for_order( $order );

    print "<a href='/wp-admin/post.php?post=" . $sub->id . "&action=edit'>Sub created! Click here to edit</a>";
    $returnarray = array(
      'subscription_id' => $sub->id,
      'order_id'        => $order->id,
    );

    return  $returnarray;
}
$test = create_test_sub();

這是我基於我在 SO 上找到的所有答案並挖掘訂閱代碼庫而構建的自定義函數。

測試過

  • WordPress 5.2.5
  • WooCommerce 3.8.0
  • WooCommerce 訂閱 2.6.1

https://gist.github.com/tripflex/a3123052f36daf18f7cb05391d752223

function give_user_subscription( $product, $user_id, $note = '' ){
    // First make sure all required functions and classes exist
    if( ! function_exists( 'wc_create_order' ) || ! function_exists( 'wcs_create_subscription' ) || ! class_exists( 'WC_Subscriptions_Product' ) ){
        return false;
    }

    $order = wc_create_order( array( 'customer_id' => $user_id ) );
    if( is_wp_error( $order ) ){
        return false;
    }

    $user = get_user_by( 'ID', $user_id );

    $fname     = $user->first_name;
    $lname     = $user->last_name;
    $email     = $user->user_email;
    $address_1 = get_user_meta( $user_id, 'billing_address_1', true );
    $address_2 = get_user_meta( $user_id, 'billing_address_2', true );
    $city      = get_user_meta( $user_id, 'billing_city', true );
    $postcode  = get_user_meta( $user_id, 'billing_postcode', true );
    $country   = get_user_meta( $user_id, 'billing_country', true );
    $state     = get_user_meta( $user_id, 'billing_state', true );
    $address         = array(
        'first_name' => $fname,
        'last_name'  => $lname,
        'email'      => $email,
        'address_1'  => $address_1,
        'address_2'  => $address_2,
        'city'       => $city,
        'state'      => $state,
        'postcode'   => $postcode,
        'country'    => $country,
    );

    $order->set_address( $address, 'billing' );
    $order->set_address( $address, 'shipping' );
    $order->add_product( $product, 1 );

    $sub = wcs_create_subscription(array(
        'order_id' => $order->get_id(),
        'status' => 'pending', // Status should be initially set to pending to match how normal checkout process goes
        'billing_period' => WC_Subscriptions_Product::get_period( $product ),
        'billing_interval' => WC_Subscriptions_Product::get_interval( $product )
    ));

    if( is_wp_error( $sub ) ){
        return false;
    }

    // Modeled after WC_Subscriptions_Cart::calculate_subscription_totals()
    $start_date = gmdate( 'Y-m-d H:i:s' );
    // Add product to subscription
    $sub->add_product( $product, 1 );

    $dates = array(
        'trial_end'    => WC_Subscriptions_Product::get_trial_expiration_date( $product, $start_date ),
        'next_payment' => WC_Subscriptions_Product::get_first_renewal_payment_date( $product, $start_date ),
        'end'          => WC_Subscriptions_Product::get_expiration_date( $product, $start_date ),
    );

    $sub->update_dates( $dates );
    $sub->calculate_totals();

    // Update order status with custom note
    $note = ! empty( $note ) ? $note : __( 'Programmatically added order and subscription.' );
    $order->update_status( 'completed', $note, true );

    // Also update subscription status to active from pending (and add note)
    $sub->update_status( 'active', $note, true );
    return $sub;
}

暫無
暫無

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

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