【问题标题】:Stock of parent sku must be equal to stock of all variations父 sku 的库存必须等于所有变体的库存
【发布时间】:2023-04-11 04:14:01
【问题描述】:

我有不同的库存商品,所有这些商品都有库存管理;问题是当我出售库存变化项目时,父项目的库存数量不会减少!

示例:- 我有一个父代码,它是一件 T 恤和 3 个变体 S/M/L,这些变体的总库存值为 30(每种尺寸 10 个),然后我想要我的父代码也显示股票价值 30;目前,当我出售库存变化项目时,它只会减少该项目的库存,而不是父项目(sku)。

是否有插件或代码可以让我这样做?

【问题讨论】:

  • 尼克,你指导我的页面是指“默认的 WooCommecrce 产品类别小部件”,我似乎找不到这个小部件,任何帮助将不胜感激。

标签: woocommerce


【解决方案1】:

我测试了代码并且它可以工作,但是请在让它生效之前做更多的测试。代码转到您的主题或子主题的functions.php。

add_action( 'woocommerce_reduce_order_stock', 'lets_reduce_parent' );

function lets_reduce_parent( $order_id ) {
  $order = wc_get_order( $order_id );
  foreach( $order->get_items() as $item_id => $item ){
      $parent_id = $item->get_product_id();
      $child_id = $item->get_variation_id();
      $quantity = $item->get_quantity();

      if($child_id == 0) return;
      else
      {
          $parent_product = wc_get_product( $parent_id );
          $stock = $parent_product->get_stock_quantity();
          $new_stock = $stock - $quantity;
          wc_update_product_stock( $parent_product, $quantity, 'decrease' );
          $order->add_order_note( "Parent Product ID: $parent_id - Stock Reduced: $stock -> $new_stock");
      }
  }
}

Ref-1:Woocommerce custom stock update after successful order

Ref-2:Get Order items and WC_Order_Item_Product in WooCommerce 3

我希望这会有所帮助。如果您有任何问题,请通知我。祝你有美好的一天。

【讨论】:

  • 将整个代码复制并粘贴到您的主题(或子主题)的functions.php中
  • 请注意订单取消/退款,因为此代码仅适用于减少库存数量。
猜你喜欢
  • 2021-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-24
  • 1970-01-01
  • 1970-01-01
  • 2016-08-13
  • 1970-01-01
相关资源
最近更新 更多