【问题标题】:Woocommerce autocomplete orders with only subscriptions只有订阅的 Woocommerce 自动完成订单
【发布时间】:2020-01-17 16:42:14
【问题描述】:

如何将这些结合起来,以便仅使用虚拟订阅文章自动完成订单?

add_action( 'woocommerce_payment_complete', 'woocommerce_subscriptions_auto_complete_order' );
function woocommerce_subscriptions_auto_complete_order( $order_id ) { 
    if ( ! $order_id ) {
        return;
    }
    $order = wc_get_order( $order_id );
    $order->update_status( 'completed' );
}

这会自动完成虚拟产品。虽然我的订阅是虚拟的,但这不起作用

/**
 * Auto Complete all WooCommerce virtual orders.
 * 
 * @param  int  $order_id The order ID to check
 * @return void
 */
function custom_woocommerce_auto_complete_virtual_orders( $order_id ) {
    // if there is no order id, exit
    if ( ! $order_id ) {
        return;
    }
    // get the order and its exit
    $order = wc_get_order( $order_id );
    $items = $order->get_items();
    // if there are no items, exit
    if ( 0 >= count( $items ) ) {
        return;
    }
    // go through each item
    foreach ( $items as $item ) {
        // if it is a variation
        if ( '0' != $item['variation_id'] ) {
            // make a product based upon variation
            $product = new WC_Product( $item['variation_id'] );
        } else {
            // else make a product off of the product id
            $product = new WC_Product( $item['product_id'] );
        }
        // if the product isn't virtual, exit
        if ( ! $product->is_virtual() ) {
            return;
        }
    }
    /*
     * If we made it this far, then all of our items are virtual
     * We set the order to completed.
     */
    $order->update_status( 'completed' );
}
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_virtual_orders' );

【问题讨论】:

    标签: php woocommerce


    【解决方案1】:

    我在https://action-a-day.com/autocomplete-orders-in-woocommerce-3-0/找到了一种更简单的方法

    add_filter( 'woocommerce_order_item_needs_processing' , 'filter_woo_item_needs_processing', 10, 3 );
    
    function filter_woo_item_needs_processing( $needs_processing, $product, $order_ID ) {
        $product_type = $product->get_type();
        if ( $product->is_virtual() 
        && ( 'subscription' == $product_type || 'subscription_variation' == $product_type || 'variable-subscription' == $product_type ) ) {
            return false;
        }
    
        return $needs_processing;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-17
      • 2019-09-18
      • 2019-04-09
      • 2016-06-11
      • 2021-12-27
      • 1970-01-01
      • 2012-08-19
      • 1970-01-01
      • 2023-03-27
      相关资源
      最近更新 更多