根据多个类别为 WooCommerce 订单号添加前缀 [英] Adding prefix to WooCommerce order number based on multiple categories
问题描述
根据我之前的问题,我正在使用 如果订单包含来自特定产品类别的商品,则为 WooCommerce 订单号添加前缀 anwser代码.
From my previous question I am using Adding prefix to WooCommerce order number if order has items from a specific product category anwser code.
现在我想把这个功能扩展到更多的类别,但我似乎找不到.
Now I want to extend this function for more categories, but I can't seem to find it out.
现在我想为其他类别添加更多 if 语句.我在想这样的事情:
Now I want to add more if statements for the other categories. I am thinking of something like this:
if ('category of first product in appearing in cart' === 'category id / category name') {
$prefix = 'AB-';
$new_order_id = $prefix . $order_id;
return $new_order_id;
}
elseif ('category of first product in appearing in cart' === 'category id / category name') {
$prefix = 'CD-';
$new_order_id = $prefix . $order_id;
return $new_order_id;
}
elseif ('category of first product in appearing in cart' === 'category id / category name') {
$prefix = 'EF-';
$new_order_id = $prefix . $order_id;
return $new_order_id;
}
有人可以帮忙吗?
推荐答案
从 添加前缀到 WooCommerce 订单号 if order 中的代码有来自特定产品类别的项目,您可以使用多个类别的答案代码:
To extend the code from Adding prefix to WooCommerce order number if order has items from a specific product category answer code for multiple categories you can use:
function filter_woocommerce_order_number( $order_number, $order ) {
// Loop through order items
foreach ( $order->get_items() as $item ) {
// Product ID
$product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
// Has term (product category)
if ( has_term( array( 'categorie-1', 'categorie-2', 15, 16 ), 'product_cat', $product_id ) ) {
return 'AB-' . $order_number;
} elseif ( has_term( array( 'categorie-3', 'categorie-4' ), 'product_cat', $product_id ) ) {
return 'CD-' . $order_number;
} elseif ( has_term( array( 'categorie-5', 'categorie-6' ), 'product_cat', $product_id ) ) {
return 'EF-' . $order_number;
}
}
return $order_number;
}
add_filter( 'woocommerce_order_number', 'filter_woocommerce_order_number', 10, 2 );
使用if/elseif/else
,您可以添加任意数量的条件
Using if/elseif/else
you can add as many conditions as you prefer
这篇关于根据多个类别为 WooCommerce 订单号添加前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!