gpt4 book ai didi

How to "bond" two or more products based on their SKU?(如何根据SKU“绑定”两个或多个产品?)

转载 作者:bug小助手 更新时间:2023-10-22 14:29:37 27 4
gpt4 key购买 nike



I would like to combine the stock of two or more products based on their SKU from WooCommerce.

我想根据WooCommerce的SKU组合两种或两种以上产品的库存。


Example: (of two products to share the same stock)

示例:(两种产品共享同一库存)



  • Transparent Phone Case iPhone 11 - SKU 12345

  • Personalized Phone Case iPhone 11 - SKU 56789


(of three or more products to share the same stock)

(三种或三种以上产品共享同一库存)



  • Transparent Phone Case iPhone 12 - SKU 12345

  • Personalized Phone Case iPhone 12 - SKU 56789

  • Collection Phone Case iPhone 12 - SKU 123456


Most of the phone cases we made them from the transparent ones, so I would like to make those products to share one and the same stock.

我们制作的大多数手机壳都是透明的,所以我想让这些产品共享同一库存。


更多回答
优秀答案推荐

Found something on the internet, it goes like this:

在互联网上发现一些东西,它是这样的:


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

// Loop through order items
foreach ($order->get_items() as $item_id => $item) {
$product = $item->get_product();
$product_sku = $product->get_sku();

// Define your SKU mappings
$sku_mapping = array(
'sku1' => array('sku2'),
'sku3' => array('sku4', 'sku5'),
'sku6' => array('sku7', 'sku8', 'sku9'),
);

// Check if the product SKU exists in the mapping
if (isset($sku_mapping[$product_sku])) {
$linked_skus = $sku_mapping[$product_sku];

// Update the stock quantities for linked products
foreach ($linked_skus as $linked_sku) {
$linked_product = wc_get_product_id_by_sku($linked_sku);
if ($linked_product) {
$linked_stock_quantity = get_post_meta($linked_product, '_stock', true);
$updated_stock_quantity = $linked_stock_quantity - $item->get_quantity();
update_post_meta($linked_product, '_stock', $updated_stock_quantity);
}
}
}
}
}

add_action('woocommerce_new_order', 'custom_sync_stock_on_order');
add_action('woocommerce_order_status_processing', 'custom_sync_stock_on_order');

The only problem is, when the product with sku1 is ordered, it'll update both sku1 and sku2 the quantity stock, but if the the sku2 is ordered, it won't. Also when I use the update button (which I implemented from here) it won't update both, only the product that I'm updating.

唯一的问题是,当订购带有sku1的产品时,它会更新sku1和sku2的库存数量,但如果订购了sku2,它不会。此外,当我使用更新按钮(我从这里实现)时,它不会同时更新这两个按钮,只更新我正在更新的产品。


更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com