【发布时间】:2021-03-23 07:24:12
【问题描述】:
在购物车中只能有一件商品的网店中,当订单包含特定类别的商品时,我需要在订单号上添加前缀
为此,我编写了以下代码:
add_action( 'woocommerce_prefix', 'check_product_category_in_order', 5 );
function check_product_category_in_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
$category_in_order = false;
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item['product_id'];
if ( has_term( 'MY-PRODUCT-CATEGORY', 'product_cat', $product_id ) ) {
$category_in_order = true;
break;
}
}
if ( $category_in_order ) {
*New funtion here*
}
}
如果$category_in_order,现在我需要运行以下函数:
add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
function change_woocommerce_order_number( $order_id ) {
$prefix = 'AB-';
$new_order_id = $prefix . $order_id;
return $new_order_id;
}
但我似乎无法找到答案。我可以在 if 语句中添加过滤器和函数吗?
【问题讨论】:
标签: php wordpress woocommerce orders prefix