Ich m?chte den Bestand von zwei oder mehr Produkten auf Basis von WooCommerce zusammenführen SKU
.
Beispiel: (Zwei Produkte mit demselben Lagerbestand)
(Drei oder mehr Produkte teilen sich den gleichen Bestand)
Die meisten unserer Handyhüllen bestehen aus durchsichtigem Material, daher wollte ich, dass diese Produkte denselben Bestand haben.
在網(wǎng)上找到了一些東西,內(nèi)容是這樣的:
// 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');
唯一的問題是,當(dāng)訂購了 sku1 的產(chǎn)品時,它會更新 sku1 和 sku2 的庫存數(shù)量,但如果訂購了 sku2,則不會。另外,當(dāng)我使用更新按鈕時(我從 此處)它不會更新兩者,只會更新我正在更新的產(chǎn)品。