检查订单是否包含具有特定属性值 Woocommerce 的产品
Checking that an order has a product with a specific attribute value Woocommerce
我要去检查添加到购物车和购买的产品是否在订单中。即当用户购买可变产品和其他可变产品时,我想检查是否添加和购买了特定的可变产品。这样我就可以在感谢页面和发送给管理员和客户的电子邮件中动态显示一些信息。
我曾尝试使用 "" 答案代码,但是在购买产品后,这在谢谢页面上不起作用.
如果订单中的产品具有 "Custom" 的属性值,我正在尝试显示动态内容,并在订单 table 中显示额外的 table 行] 如果订单中有商品属性值为"order",代码如下:
function add_custom_row_to_order_table( $total_rows, $myorder_obj ) {
if ( is_attr_in_cart('custom') ) {
$cmeasuement = __( 'Yes', 'domain' );
}else{
$cmeasuement = __( 'No', 'domain' );
}
$total_rows['el_custom'] = array(
'label' => __('Custom Required?', 'domain'),
'value' => $cmeasuement,
);
return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'add_custom_row_to_order_table', 10, 2 );
但我一直在获取 "No" (见下面的截图) 原因是 is_attr_in_cart('custom')
函数没有检测属性是否在命令。帮助正确的方向让它检测订单是否有具有特定属性值的产品。
感谢任何帮助。
要使其与 WooCommerce orders 一起使用,您需要另一个自定义条件函数 (其中 $attribute_value
是 slug 您定位的产品属性的值):
function is_attr_in_order( $order, $attribute_value ){
$found = false; // Initializing
// Loop though order items
foreach ( $order->get_items() as $item ){
// Only for product variations
if( $item->get_variation_id() > 0 ){
$product = $item->get_product(); // The WC_Product Object
$product_id = $item->get_product_id(); // Product ID
// Loop through product attributes set in the variation
foreach( $product->get_attributes() as $taxonomy => $term_slug ){
// comparing attribute parameter value with current attribute value
if ( $attribute_value === $term_slug ) {
$found = true;
break;
}
}
}
if($found) break;
}
return $found;
}
现在,此条件函数将在您的订单接收页面 (thankyou) 上的代码中工作,如果 "Yes",则向总数 table 添加一个额外的行找到产品属性或 "No" 如果没有:
add_filter( 'woocommerce_get_order_item_totals', 'add_custom_row_to_order_table', 10, 3 );
function add_custom_row_to_order_table( $total_rows, $order, $tax_display ) {
$domain = 'woocommerce'; // The text domain (for translations)
$term_slug = 'custom'; // <== The targeted product attribute slug value
$total_rows['custom'] = array(
'label' => __( "Custom Required?", $domain ),
'value' => is_attr_in_order( $order, $term_slug ) ? __( "Yes", $domain ) : __( "No", $domain ),
);
return $total_rows;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
如果您只需要定位收到订单的页面,您将在挂钩函数中使用以下条件:
if( is_wc_endpoint_url('order-received') ) {
// The code comes here
}
return $total_rows; // The final filter return outside
我要去检查添加到购物车和购买的产品是否在订单中。即当用户购买可变产品和其他可变产品时,我想检查是否添加和购买了特定的可变产品。这样我就可以在感谢页面和发送给管理员和客户的电子邮件中动态显示一些信息。
我曾尝试使用 "
如果订单中的产品具有 "Custom" 的属性值,我正在尝试显示动态内容,并在订单 table 中显示额外的 table 行] 如果订单中有商品属性值为"order",代码如下:
function add_custom_row_to_order_table( $total_rows, $myorder_obj ) {
if ( is_attr_in_cart('custom') ) {
$cmeasuement = __( 'Yes', 'domain' );
}else{
$cmeasuement = __( 'No', 'domain' );
}
$total_rows['el_custom'] = array(
'label' => __('Custom Required?', 'domain'),
'value' => $cmeasuement,
);
return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'add_custom_row_to_order_table', 10, 2 );
但我一直在获取 "No" (见下面的截图) 原因是 is_attr_in_cart('custom')
函数没有检测属性是否在命令。帮助正确的方向让它检测订单是否有具有特定属性值的产品。
感谢任何帮助。
要使其与 WooCommerce orders 一起使用,您需要另一个自定义条件函数 (其中 $attribute_value
是 slug 您定位的产品属性的值):
function is_attr_in_order( $order, $attribute_value ){
$found = false; // Initializing
// Loop though order items
foreach ( $order->get_items() as $item ){
// Only for product variations
if( $item->get_variation_id() > 0 ){
$product = $item->get_product(); // The WC_Product Object
$product_id = $item->get_product_id(); // Product ID
// Loop through product attributes set in the variation
foreach( $product->get_attributes() as $taxonomy => $term_slug ){
// comparing attribute parameter value with current attribute value
if ( $attribute_value === $term_slug ) {
$found = true;
break;
}
}
}
if($found) break;
}
return $found;
}
现在,此条件函数将在您的订单接收页面 (thankyou) 上的代码中工作,如果 "Yes",则向总数 table 添加一个额外的行找到产品属性或 "No" 如果没有:
add_filter( 'woocommerce_get_order_item_totals', 'add_custom_row_to_order_table', 10, 3 );
function add_custom_row_to_order_table( $total_rows, $order, $tax_display ) {
$domain = 'woocommerce'; // The text domain (for translations)
$term_slug = 'custom'; // <== The targeted product attribute slug value
$total_rows['custom'] = array(
'label' => __( "Custom Required?", $domain ),
'value' => is_attr_in_order( $order, $term_slug ) ? __( "Yes", $domain ) : __( "No", $domain ),
);
return $total_rows;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
如果您只需要定位收到订单的页面,您将在挂钩函数中使用以下条件:
if( is_wc_endpoint_url('order-received') ) {
// The code comes here
}
return $total_rows; // The final filter return outside