首页 > 解决方案 > WooCommerce 父订单前缀

问题描述

我正在尝试在“父订单”和“子订单”之间实现订单前缀。目前我的订单前缀适用于特定类别的产品,因此“商品”类别和其中的产品具有订单号前缀“M”,“咖啡”中的类别和产品具有前缀“C”,这一切都很好,精彩地看到所附的图像。

我在商店里确实有订阅产品和单一产品,但我主要只是希望一个“P”出现在主订单中,而不管产品属于哪个类别。

我需要并希望帮助为父订单添加一个“P”,以便我可以区分父订单和子订单,(请参阅附图)我一生都无法弄清楚这个功能目前我的代码是这样的:

add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
function change_woocommerce_order_number( $order_id ) {
    $order = wc_get_order( $order_id );
    $category = array();
    if ( ! empty($order) ) {
        foreach ( $order->get_items() as $item_id => $item ) {
            $product_id = $item['product_id'];
            $terms = get_the_terms ( $product_id, 'product_cat' );
            $parent_id = $order->get_parent_id( $parent, 'product_id' );
            if ( ! empty($terms) ) {
                foreach ( $terms as $term ) {
                    $category[] = $term->term_id; 
                }
            }
        }
    }
    if ( $parent ) {
    $order_id = 'P - '. $order_id;
    } else {
        if ( in_array( 23, $category ) ) {
            $order_id = 'M - '. $order_id;
        } else {
            if ( in_array( 16, $category ) ) {
                $order_id = 'C - '. $order_id;
            }
        }
        return $order_id;
    }
}

'M' 和 'C' 完美地工作......只是父订单 'P' 不是这样,而不是 'P' 我在父订单部分有似乎是随机的 M 或 C。我所有的订单都按类别划分,这仍然按预期工作(请参见图片)。我主要只需要附图左侧的“P”主父订单

父订单前缀“P”位置

提前致谢。

标签: wordpresswoocommerceprefix

解决方案


您当前仅在 else 语句中返回订单号。

一个值应该总是在函数结束时返回。

此外,未设置$parent变量,并且您以错误的方式获取父 ID。

试试这个,希望它有效。

如果该函数没有父订单,则该get_parent_id()方法返回。0

add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
function change_woocommerce_order_number( $order_id ) {
    $order = wc_get_order( $order_id );
    $category = array();
    if ( ! empty($order) ) {
        foreach ( $order->get_items() as $item_id => $item ) {
            $product_id = $item['product_id'];
            $terms = get_the_terms ( $product_id, 'product_cat' );
            if ( ! empty($terms) ) {
                foreach ( $terms as $term ) {
                    $category[] = $term->term_id; 
                }
            }
        }
    }

    $parent_id = $order->get_parent_id();

    if ( $parent_id > 0 ) {
        if ( in_array( 23, $category ) ) {
            $order_id = 'M - '. $order_id;
        } else {
            if ( in_array( 16, $category ) ) {
                $order_id = 'C - '. $order_id;
            }
        }
    } else {
        $order_id = 'P - '. $order_id;
    }
    return $order_id;
}

您也可以像这样优化功能:

// change the WooCommerce order number
add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
function change_woocommerce_order_number( $order_id ) {

    $order = wc_get_order( $order_id );

    $prefix = '';
    $has_cat_id_23 = false;
    $has_cat_id_16 = false;

    // get the parent order id
    $parent_id = $order->get_parent_id();

    // if it does not have a parent order it returns the order number with the prefix "P - "
    if ( $parent_id == 0 ) {
        $prefix = 'P - ';
        return $prefix . $order_id;
    // otherwise set the prefix to the child orders
    } else {
        
        foreach ( $order->get_items() as $item_id => $item ) {
            // if the product belongs to the product category with ID 23
            if ( has_term( 23, 'product_cat', $item['product_id'] ) ) {
                $has_cat_id_23 = true;
                break;
            }
            // if the product belongs to the product category with ID 16
            if ( has_term( 16, 'product_cat', $item['product_id'] ) ) {
                $has_cat_id_16 = true;
                break;
            }
        }

        // if the product belongs to the product category with ID 23 set the prefix "M - "
        if ( $has_cat_id_23 ) {
            $prefix = 'M - ';
        }
    
        // if the product belongs to the product category with ID 16 set the prefix "C - "
        if ( $has_cat_id_16 ) {
            $prefix = 'C - ';
        }
    }

    return $prefix . $order_id;
}

该代码已经过测试并且可以运行(我没有安装插件,但实际上只使用了 Wordpress 和 WooCommerce 功能和方法)。结果将如下所示:

在此处输入图像描述

试试看,让我知道它是否也适合你。


推荐阅读