wordpress 将所有订单商品SKU添加到Woocommerce中的订单号

6vl6ewon  于 2023-08-03  发布在  WordPress
关注(0)|答案(1)|浏览(131)

我有这个代码,通过functions.php将产品名称前缀到订单号中,但我想将其更改为添加SKU。如果每个SKU的数量也能插入,那就太好了。
类似于:2xSKUA_1xSKUB_Ordernumber
有人能告诉我正确的方向吗?

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 );

字符串

f0brbegy

f0brbegy1#

要将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;
}

字符串
它应该像你期望的那样工作。

相关问题