【问题标题】:Checking that an order has a product with a specific attribute value Woocommerce检查订单是否包含具有特定属性值 Woocommerce 的产品
【发布时间】:2019-09-27 10:18:58
【问题描述】:

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

我尝试使用“Checking that a specific attribute value is used in a cart Item (product variation)”回答代码,但是一旦购买产品,这在Thankyou页面上不起作用。

如果订单中的产品具有“自定义”属性值,我正在尝试显示动态内容,并且如果订单中的产品具有该属性,则在订单表中显示一个附加表行值“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') 函数没有检测到属性是否在顺序中。帮助其检测订单是否包含具有特定属性值的产品。

感谢任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce orders taxonomy-terms


    【解决方案1】:

    要使其与 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;
    }
    

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

    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
    

    【讨论】:

    • 你是救生员...谢谢@LoicTheAztec
    猜你喜欢
    • 1970-01-01
    • 2020-06-03
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    • 2014-11-15
    相关资源
    最近更新 更多