在WooCommerce中更改虚拟产品、可下载产品、免费产品或回购产品的订单状态。

问题描述 投票:0回答:1

我试着用+1检查插件稍作修改。位于此因此,对于所有的虚拟可下载的免费(价格=0,00)&对Backorder产品,我想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 hook-woocommerce
1个回答
0
投票

注意事项1: 使用 woocommerce_thankyou 钩子


注意事项2.要想知道产品是虚拟的还是可下载的,您可以使用以下功能。 要知道一个产品是虚拟的还是可下载的,你可以使用以下功能。$product->is_virtual();$product->is_downloadable(); 相反 get_post_meta();

更多信息。https:/docs.woocommerce.comwc-apidocsclass-WC_Product.html。


注意事项3.不要在foreach循环中执行操作。 最好不要在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 );
© www.soinside.com 2019 - 2024. All rights reserved.