WooCommerce 订单状态完成时更新产品库存数量和库存状态

发布于 2025-01-10 08:08:47 字数 1632 浏览 2 评论 0原文

我们销售手工制品。可变产品只能购买一种变体。

订购变体后,该变体产品的库存将变为 0,并且在订单完成之前无法订购该产品。我可以按如下方式设置库存状态和库存数量:

//when order is completed the stock and stock status are reset to instock and 1
add_action( 'woocommerce_order_status_completed', 'my_function' );

function my_function($order_id) {

    $order = wc_get_order( $order_id );
    $items = $order->get_items();
    $product_id;
    foreach ( $items as $item ) {
        $product_id = $item->get_product_id();
    } //end foreach

    $new_stock_status = 'instock';

    // 1. Updating the stock quantity
    update_post_meta($product_id, '_stock', 1);

    // 2. Updating the stock quantity
    update_post_meta( $product_id, '_stock_status', wc_clean( $new_stock_status ) );

    // 3. Updating post term relationship
    wp_set_post_terms( $product_id, 'outofstock', 'product_visibility', true );

    // And finally (optionally if needed)
    wc_delete_product_transients( $product_id ); // Clear/refresh the variation cache        
}

我不确定此代码是否正确,但它似乎有效。

但单品页面依然显示缺货信息。仅当我添加:

//reset stock in product
add_action( 'woocommerce_admin_process_product_object', 'lumi_reset_stock' );
function lumi_reset_stock( $product ) {

    // Using WC setters
    $product->set_manage_stock(true);
    $product->set_stock_quantity(1);
    $product->set_stock_status('instock');
}

并将产品保存在管理后端时,才能再次购买该产品。

这有点复杂。订单完成后,如何重置产品的库存和 stock_status?

查看数据库> wp_wc_product_meta_lookup 即使保存产品后,库存状态仍显示缺货且数量为 0。我必须重置这些值吗?

We sell handmade products. Only one variation of a variable product is purchasable.

When a variation is ordered, the stock of that variable product goes to 0 and the product cannot be ordered until the order is completed. I can set the instock status and the stock quantity as follows:

//when order is completed the stock and stock status are reset to instock and 1
add_action( 'woocommerce_order_status_completed', 'my_function' );

function my_function($order_id) {

    $order = wc_get_order( $order_id );
    $items = $order->get_items();
    $product_id;
    foreach ( $items as $item ) {
        $product_id = $item->get_product_id();
    } //end foreach

    $new_stock_status = 'instock';

    // 1. Updating the stock quantity
    update_post_meta($product_id, '_stock', 1);

    // 2. Updating the stock quantity
    update_post_meta( $product_id, '_stock_status', wc_clean( $new_stock_status ) );

    // 3. Updating post term relationship
    wp_set_post_terms( $product_id, 'outofstock', 'product_visibility', true );

    // And finally (optionally if needed)
    wc_delete_product_transients( $product_id ); // Clear/refresh the variation cache        
}

I am not sure whether this code is correct, but it seems to be working.

However, the single product page persists in showing the out of stock message. Only when I add:

//reset stock in product
add_action( 'woocommerce_admin_process_product_object', 'lumi_reset_stock' );
function lumi_reset_stock( $product ) {

    // Using WC setters
    $product->set_manage_stock(true);
    $product->set_stock_quantity(1);
    $product->set_stock_status('instock');
}

And save the product in the admin backend, then the product is purchasable again.

This is a bit complicated. How can i also reset the stock and stock_status for a product when an order is completed?

Looking at the database > wp_wc_product_meta_lookup the stock-status shows outofstock and the quantity is 0, even after saving the product. Do i have to reset these values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是暖光i 2025-01-17 08:08:47

尽管您的代码有效,但您缺少一些检查/点:

所以你得到:

function action_woocommerce_order_status_completed( $order_id ) {
    // The WC_Order instance Object
    $order = wc_get_order( $order_id );
    
    // Is a order
    if ( is_a( $order, 'WC_Order' ) ) {
        // Loop through order items
        foreach ( $order->get_items() as $key => $item ) {
            // The WC_Product instance Object
            $product = $item->get_product();

            // Product type
            if ( $product->get_type() == 'variation' ) {
                // NOT in stock
                if ( ! $product->is_in_stock() ) {
                    // Update a product's stock amount
                    wc_update_product_stock( $product, 1, 'set', 'false' );

                    // Update a product's stock status
                    wc_update_product_stock_status( $product->get_id(), 'instock' );
                }
            }
        }   
    }
}
add_action( 'woocommerce_order_status_completed', 'action_woocommerce_order_status_completed', 10, 1 );

Although your code works, you are missing some checks/points:

So you get:

function action_woocommerce_order_status_completed( $order_id ) {
    // The WC_Order instance Object
    $order = wc_get_order( $order_id );
    
    // Is a order
    if ( is_a( $order, 'WC_Order' ) ) {
        // Loop through order items
        foreach ( $order->get_items() as $key => $item ) {
            // The WC_Product instance Object
            $product = $item->get_product();

            // Product type
            if ( $product->get_type() == 'variation' ) {
                // NOT in stock
                if ( ! $product->is_in_stock() ) {
                    // Update a product's stock amount
                    wc_update_product_stock( $product, 1, 'set', 'false' );

                    // Update a product's stock status
                    wc_update_product_stock_status( $product->get_id(), 'instock' );
                }
            }
        }   
    }
}
add_action( 'woocommerce_order_status_completed', 'action_woocommerce_order_status_completed', 10, 1 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文