Php 检查订单中是否有具有特定属性值的产品
我要去检查一个产品是否添加到购物车和购买是在一个订单。也就是说,当用户购买可变产品和其他可变产品时,我想检查是否添加和购买了特定的可变产品。这样我就可以动态地在Thankyou页面和发送给管理员和客户的电子邮件上显示一些信息 我曾尝试使用“”应答码,但一旦购买了产品,这在感谢页面上就不起作用了 如果订单中的产品具有“Custom”属性值,则我尝试显示动态内容,如果订单中的产品具有“order”属性值,则在订单表中显示一个附加表行,代码如下:Php 检查订单中是否有具有特定属性值的产品,php,wordpress,woocommerce,orders,taxonomy-terms,Php,Wordpress,Woocommerce,Orders,Taxonomy Terms,我要去检查一个产品是否添加到购物车和购买是在一个订单。也就是说,当用户购买可变产品和其他可变产品时,我想检查是否添加和购买了特定的可变产品。这样我就可以动态地在Thankyou页面和发送给管理员和客户的电子邮件上显示一些信息 我曾尝试使用“”应答码,但一旦购买了产品,这在感谢页面上就不起作用了 如果订单中的产品具有“Custom”属性值,则我尝试显示动态内容,如果订单中的产品具有“order”属性值,则在订单表中显示一个附加表行,代码如下: function add_custom_row_to_
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 );
但是我一直得到“否”(请参见下面的屏幕截图),原因是是\u cart('custom')中的\u attr\u
函数没有检测属性是否符合顺序。在正确的方向上帮助it部门检测订单中是否有具有特定属性值的产品
非常感谢您的帮助。要使其与WooCommerce订单一起工作,您需要另一个自定义条件函数(其中
$attribute\u value
是您目标产品属性的slug值):
现在,此条件函数将在您的“订单接收”页面上的代码中工作(谢谢),如果找到产品属性,则在total表中添加一行“是”,如果没有,则添加一行“否”:
你是个救生员。。。谢谢@LoicTheAztec
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;
}
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;
}
if( is_wc_endpoint_url('order-received') ) {
// The code comes here
}
return $total_rows; // The final filter return outside