【问题标题】:Check if at least one of item has specific attribute term in WooCommerce order检查 WooCommerce 订单中是否至少一项具有特定属性项
【发布时间】:2021-02-01 16:09:39
【问题描述】:
我想检查是否至少有一个项目在 WooCommerce 订单中具有特定的属性术语以在邮购中显示某些内容。
属性税是“pa_labels”,术语是“树”,但缺少一些东西......有什么想法吗?
add_action( 'woocommerce_thankyou', 'webroom_check_product_attr_in_order', 5 );
function webroom_check_product_attr_in_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
$attr_in_order = false;
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item['product_id'];
if ( has_term( 'tree', 'pa_labels', $product_id ) ) {
$attr_in_order = true;
break;
}
}
// Echo content only if $attr_in_order == true
if ( $attr_in_order ) {
add_action( 'woocommerce_email_customer_details', 'custom_woocommerce_email_customer_details', 25);
}
}
【问题讨论】:
标签:
wordpress
woocommerce
hook-woocommerce
wordpress-hook
【解决方案1】:
你可以试试这样的:
// check if in the order there is at least one product with the attribute "pa_labels" => "tree"
add_action( 'woocommerce_thankyou', 'webroom_check_product_attr_in_order', 5 );
function webroom_check_product_attr_in_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
$attr_in_order = false;
foreach ( $order->get_items() as $item ) {
$product = $item->get_product();
if ( has_term( 'tree', 'pa_labels', $product->get_id() ) ) {
$attr_in_order = true;
break;
}
}
// Echo content only if $attr_in_order == true
if ( $attr_in_order ) {
custom_woocommerce_email_customer_details();
}
}
// add content to the customer details in the email
add_action( 'woocommerce_email_customer_details', 'custom_woocommerce_email_customer_details', 25);
function custom_woocommerce_email_customer_details() {
// do stuff
}
代码必须添加到活动主题的functions.php中。