【问题标题】:Change order status for virtual, downloadable, free or backorder products in WooCommerce在 WooCommerce 中更改虚拟、可下载、免费或延期交货产品的订单状态
【发布时间】:2020-08-05 19:56:56
【问题描述】:

我尝试通过 +1 检查插件 located here 稍作修改 因此,对于所有 Virtual Downloadable Free (price=0,00) 和延期交货产品,我希望 Woocommerce 将订单状态设置为“处理中”

我使用下面的代码得到的结果 - Woocommerce 设置订单状态“待付款” 是否有任何想法如何将其切换为“处理”:

add_action('woocommerce_checkout_order_processed', 'handmade_woocommerce_order');

function handmade_woocommerce_order( $order_id ) 
{
    $order = wc_get_order($order_id);
    foreach ($order->get_items() as $item_key => $item_values):
        $product_id = $item_values->get_product_id(); //get product id

    //get prodct settings i.e virtual
    $virtual_product = get_post_meta($product_id,'_virtual',true);
    $downloadable_product = get_post_meta($product_id,'_downloadable',true);
    $product_backordered=backorders_allowed($product_id,'_backorders',true);

    $price = get_post_meta($product_id,'_regular_price',true);

    $virtuald=get_option('hmade_vd');

    if($virtuald=='yes' && $downloadable_product=='yes' && $virtual_product=='yes' && $product_backordered=='yes')
    {
        if($price=='0.00')
        {
            $order->update_status( 'processing' );
        }

    }


endforeach;
}

【问题讨论】:

标签: php wordpress woocommerce product orders


【解决方案1】:

注意1:改用woocommerce_thankyou钩子


注意2:要知道产品是虚拟的还是可下载的,您可以使用以下函数$product->is_virtual();$product->is_downloadable();对面get_post_meta();

更多信息:https://docs.woocommerce.com/wc-apidocs/class-WC_Product.html


注意3:最好不要在foreach循环中执行操作,之后使用检查

function handmade_woocommerce_order( $order_id ) {
    if( ! $order_id ) return;

    // Get order
    $order = wc_get_order( $order_id );

    // get order items = each product in the order
    $items = $order->get_items();

    // Set variable
    $found = false;

    foreach ( $items as $item ) {
        // Get product id
        $product = wc_get_product( $item['product_id'] );

        // Is virtual
        $is_virtual = $product->is_virtual();

        // Is_downloadable
        $is_downloadable = $product->is_downloadable();
        
        // Backorders allowed
        $backorders_allowed = $product->backorders_allowed();

        if( $is_virtual && $is_downloadable && $backorders_allowed ) {
            $found = true;
            // true, break loop
            break;
        }
    }

    // true
    if( $found ) {
        $order->update_status( 'processing' );
    }
}
add_action('woocommerce_thankyou', 'handmade_woocommerce_order', 10, 1 );

【讨论】:

  • 很好的例子,谢谢!唯一需要提及的是 - codeadd_action('woocommerce_checkout_order_processed', 'handmade2_woocommerce_order', 10, 1 );code 仍在使用我的现场代码版本,因为对于免费产品,“谢谢”页面对我来说是不可接受的(它显示下载链接)。还是谢谢!
猜你喜欢
  • 2018-07-14
  • 1970-01-01
  • 2021-07-16
  • 2018-08-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多