我有这段代码,通过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; }
它应该按您的预期工作。