我有這段程式碼,透過functions.php將產品名稱作為前綴添加到訂單號碼中,但我想更改它以添加SKU。 另外,如果能同時插入每個 SKU 的數量就更好了。
類似: 2xSKUA_1xSKUB_訂單號碼
有人能指出我正確的方向嗎?
function filter_woocommerce_order_number( $this_get_id, $instance ) { $order = new WC_Order( $this_get_id ); $items = $order->get_items(); foreach ( $items as $item ) { $product_name = preg_replace('/\s+/', '_', $item['name']);; break; } $new_id = $product_name.'_'.$this_get_id; return $new_id; }; add_filter( 'woocommerce_order_number', 'filter_woocommerce_order_number', 10, 2 );
P粉9498488492023-09-12 17:26:24
要在訂單商品(產品)數量前面加上 SKU,在訂單號碼後面使用。以下重新造訪的程式碼:
add_filter( 'woocommerce_order_number', 'filter_woocommerce_order_number', 10, 2 ); function filter_woocommerce_order_number( $order_number, $order ) { $prefix = array(); // Initializing // Loop through order items foreach ( $order->get_items() as $item ) { $product = $item->get_product(); // Get the product object // Add the quantity with the sku to the array $prefix[] = $item->get_quantity() . 'X' . $product->get_sku(); } // return everything merged in a string return implode('_', $prefix) . '_' . $order_number; }
它應該按您的預期工作。