首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >停止WooCommerce降低废弃行政管理新秩序库存

停止WooCommerce降低废弃行政管理新秩序库存
EN

Stack Overflow用户
提问于 2019-09-20 09:22:22
回答 1查看 424关注 0票数 1

如果管理员去创建一个订单,但是放弃了它,那么库存水平仍然会降低。

复制步骤:

WordPress

  • install WooCommerce

  • create
  1. 安装简单产品并勾选“管理库存?”并将前端的股票级别设置为10
  2. 视图(见屏幕截图before.png),
  3. 作为管理员创建新订单,但不要保存它(新建-> order ->添加项目退出页面)前端的
  4. 视图(见屏幕截图),即使这些订单未被保存,
  5. 视图也已降低。

有什么可以避免的吗?

Before.png:

After.png

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-20 13:11:40

我已经研究过这个问题,并为此编写了一个基本代码。

使用钩子: "woocommerce_order_item_add_action_buttons“用于管理订单添加项,"woocommerce_process_shop_order_meta”用于管理订单创建/更新。

第一部分:在尚未创建订单的情况下,停止减少添加的项的库存。

代码语言:javascript
复制
// define the woocommerce_order_item_add_action_buttons callback 
function action_woocommerce_order_item_add_action_buttons( $order ) { 
    $orderID = $order->ID;
    //check if this is the admin manual order creation 
    if(get_post_status($orderID) == "auto-draft" && get_post_type($orderID) == "shop_order")
    {
        foreach( $order->get_items() as $item_id => $item )
        {
            $product_id = $item->get_product_id();
            $variation_id = $item->get_variation_id();
            $product_quantity = $item->get_quantity();

            if($variation_id == 0)
            {
                $product = wc_get_product($product_id);
                wc_update_product_stock($product, $product_quantity, 'increase');
            }
            else
            {
                $variation = wc_get_product($variation_id);
                wc_update_product_stock($variation, $product_quantity, 'increase' );
            }

            // The text for the note
            $note = __("Stock incremented due to the auto draft post type. Stock for each item will be decremented when this order created.");
            // Add the note
            $order->add_order_note( $note );
        }
    }
}; 

// add the action 
add_action( 'woocommerce_order_item_add_action_buttons', 'action_woocommerce_order_item_add_action_buttons', 10, 1 );

第二部分:如果创建了订单,将减少添加的项的库存。

代码语言:javascript
复制
add_action( 'woocommerce_process_shop_order_meta', 'woocommerce_process_shop_order', 10, 2 );
function woocommerce_process_shop_order ( $post_id, $post ) {
    $order = wc_get_order( $post_id );

    //check if this is order create action, not an update action
    if(get_post_status($post_id) == "draft" && get_post_type($post_id) == "shop_order")
    {
        foreach( $order->get_items() as $item_id => $item )
        {
            $product_id = $item->get_product_id();
            $variation_id = $item->get_variation_id();
            $product_quantity = $item->get_quantity();

            if($variation_id == 0)
            {
                $product = wc_get_product($product_id);
                wc_update_product_stock($product, $product_quantity, 'decrease');
            }
            else
            {
                $variation = wc_get_product($variation_id);
                wc_update_product_stock($variation, $product_quantity, 'decrease' );
            }

            // The text for the note
            $note = __("Stock decremented for all items in this order.");
            // Add the note
            $order->add_order_note( $note );
    }
    }
}

测试并运行良好。我希望这能帮到你。祝你今天愉快。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58025497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档