自动完成 WooCommerce 支付订单的专属类别,否则设置自定义状态

时间:2021-01-15 22:19:58

标签: php wordpress woocommerce hook-woocommerce orders

我尝试在付款时制作一个代码来验证订单中的产品类别,我已经测试过但没有用,我用信用卡测试过,但状态自动完成。

当订单中的产品类别为“pack”并且没有任何其他不同类别时,我希望订单进入“已完成”状态,如果有任何其他类别的产品,我希望订单去状态为“aguardando-envio”。

你能帮我解决这个问题吗?

add_action( 'woocommerce_payment_complete', function ( $order_id ) {
    
    if( ! $order_id ) return;
    
    $order = wc_get_order( $order_id );
  
    // 2. Initialize $cat_in_order variable
    $cat_in_order = false;
  
    // 3. Get order items and loop through them...
    // ... if product in category, edit $cat_in_order
    $items = $order->get_items(); 
      
    foreach ( $items as $item ) {      
       $product_id = $item->get_product_id();  
       if ( has_term( 'videos-personalizaveis', $product_id ) ) {
          $cat_in_order = true;
          break;
       }
       if ( has_term( 'fotos-personalizaveis', $product_id ) ) {
          $cat_in_order = true;
          break;
       }
       if ( has_term( 'audios-personalizaveis', $product_id ) ) {
          $cat_in_order = true;
          break;
       }
       if ( has_term( 'produtos-pessoais', $product_id ) ) {
          $cat_in_order = true;
          break;
       }
    }
   
    if ( $cat_in_order ) {
        $order->update_status( 'aguardando-envio' );
    }
    
    if ( $cat_in_order == false ) {
        $order->update_status( 'completed' );
    }
}, 10, 3 );

2 个答案:

答案 0 :(得分:1)

请尝试以下操作,这将自动完成独家产品类别“pack”的订单状态,否则会将订单状态设置为“aguardando-envio”:

add_action( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 );
function wc_auto_complete_paid_order( $status, $order_id, $order ) {
    
    $exclusive_category = array("pack"); // <== Here set your exclusive category
    $others_found       = false; // Initializing
    
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        if( ! has_term( $exclusive_category, 'product_cat', $item->get_product_id() ) ) {
            $others_found = true;
            break;
        }
    }

    return $others_found ? 'aguardando-envio' : 'completed';
}

代码位于活动子主题(或活动主题)的functions.php 文件中。它应该有效。

注意事项:

  • 需要在 WooCommerce 订单之前通过一些自定义代码或插件创建自定义状态“aguardando-envio”。
  • 这不适用于“bacs”、“cod”或“cheque”付款方式 ID,其中付款需要由商店经理通过管理员更改 oder 状态来确认。

相关:WooCommerce: Auto complete paid orders

答案 1 :(得分:0)

您在检查产品是否属于所需类别时出错。另一方面,订单状态可能未正确注册。在这里你会找到你需要的一切:

/**
 * Register new status
*/
add_action( 'init', 'ywp_register_aguardando_envio_shipment_order_status' );
function ywp_register_aguardando_envio_shipment_order_status() {
    register_post_status( 'wc-aguardando-envio', array(
        'label'                     => 'Aguardando Envio',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Aguardando Envio (%s)', 'Aguardando Envio (%s)' )
    ) );
}

/**
 * Add to list of WC Order statuses
*/
add_filter( 'wc_order_statuses', 'ywp_add_aguardando_envio_shipment_to_order_statuses' );
function ywp_add_aguardando_envio_shipment_to_order_statuses( $order_statuses ) {
 
    $new_order_statuses = array();
 
    // add new order status after processing
    foreach ( $order_statuses as $key => $status ) {
 
        $new_order_statuses[ $key ] = $status;
 
        if ( 'wc-processing' === $key ) {
            $new_order_statuses['wc-aguardando-envio'] = 'Aguardando Envio';
        }
    }
 
    return $new_order_statuses;
}

/**
 * Change order status
*/
add_action( 'woocommerce_thankyou', 'ywp_change_order_status_to_aguardando_envio', 99 );
function ywp_change_order_status_to_aguardando_envio( $order_id ) {
    if( ! $order_id ) return;

    $order = wc_get_order( $order_id );

    // 2. Initialize $cat_in_order variable
    $cat_in_order = false;

    // 3. Get order items and loop through them...
    // ... if product in category, edit $cat_in_order
    $items = $order->get_items(); 

    foreach( $items as $item ) {      
        $product_id = $item->get_product_id();  

        if( has_term( array( 'videos-personalizaveis', 'fotos-personalizaveis', 'audios-personalizaveis', 'produtos-pessoais' ), 'product_cat', $product_id ) ) {
            $cat_in_order = true;
            break;
        }
    }
   
    if ( $cat_in_order ) {
        $order->update_status( 'aguardando-envio' );
    }
}

代码位于活动主题/子主题的 functions.php 文件中。经测试有效。