php - 检查订单中是否有具有特定属性值的产品 Woocommerce

标签 php wordpress woocommerce orders taxonomy-terms

我要去检查添加到购物车并购买的产品是否在订单中。即,当用户购买可变产品以及其他可变产品时,我想检查是否添加并购买了特定的可变产品。这样我就可以在感谢页面上动态显示一些信息以及发送给管理员和客户的电子邮件。

我尝试使用"Checking that a specific attribute value is used in a cart Item (product variation) "应答代码,但在购买产品后这在感谢页面上不起作用

我正在尝试的是,如果订单中的产品具有“自定义”属性值,则显示动态内容;如果订单中存在具有该属性的产品,则在订单表中显示附加表行值“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 );

但我一直得到“否”(参见下面的屏幕截图),原因是 is_attr_in_cart('custom') 函数未检测该属性是否为按顺序。帮助正确的方向让它检测订单是否包含具有特定属性值的产品。

enter image description here

感谢任何帮助。

最佳答案

要使其与 WooCommerce 订单 配合使用,您需要另一个自定义条件函数(其中 $attribute_value 是 a 的 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;
}

现在,此条件函数将在订单接收页面上的代码中运行(谢谢),如果找到产品属性,则在总计表中添加一行"is",否则添加“否” :

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 文件中。经过测试并有效。


如果您只需要定位收到订单的页面,您将在 Hook 函数中使用以下条件:

if( is_wc_endpoint_url('order-received') ) {
    // The code comes here
}
return $total_rows; // The final filter return outside

关于php - 检查订单中是否有具有特定属性值的产品 Woocommerce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58122510/

相关文章:

php - 在下拉列表中显示预先选择的值

php - "Caching"mySQL 查询,这是一个好的做法吗?

html - 将鼠标悬停在图像上时切换颜色

wordpress - 如何根据邮政编码隐藏 Woocommerce 中的付款方式

woocommerce - 如何通过 cron 作业在 WooCommerce -> 状态 -> 工具上运行自动重新生成产品查找表?

php - Codeigniter Select_Sum 返回 "array"不是数值?

php - 获取类中函数的返回值

php - 结合谷歌字体请求不起作用

php - 在 WordPress 小部件中集成图像上传框

php - WooCommerce 运费含税