简体   繁体   English

在 WooCommerce 结帐页面上显示产品类别

[英]Display product category on the WooCommerce checkout page

I'm trying to add the product category to the below (working) code but I'm not sure how to go about it.我正在尝试将产品类别添加到以下(工作)代码中,但我不确定如何 go 关于它。

I've tried using product_cat but as I'm not that experienced with php, I'm just guessing how to achieve this.我尝试过使用product_cat ,但由于我对 php 没有那么丰富的经验,所以我只是在猜测如何实现这一点。

add_filter( 'woocommerce_get_item_data', 'display_custom_product_field_data', 10, 2 );
function display_custom_product_field_data( $cart_data, $cart_item ) {
    $meta_keys = array('time','date');
    $dictionary = array('time'=>'Time:','date'=>'Date:' );
    $product_id = $cart_item['product_id'];

    foreach($meta_keys as $key=>$meta_key){

        $meta_value = get_post_meta( $product_id, $meta_key, true );

        if( !empty( $cart_data ) ) $custom_items = $cart_data;

        if( !empty($meta_value) ) {
            $custom_items[] = array(
                'key'       => __( $dictionary[$meta_key] , 'woocommerce'), //or user $meta_key
                'value'     => $meta_value,
                'display'   => $meta_value,
            );
        }
    }

    return $custom_items;
}

Following code will also display the product categories on the WooCommerce checkout page以下代码还将在 WooCommerce 结帐页面上显示产品类别

function display_custom_product_field_data( $cart_item_data, $cart_item ) {
    // Product ID
    $product_id = $cart_item['product_id'];

    // Get post meta
    $time = get_post_meta( $product_id, 'time', true );

    // NOT empty
    if( ! empty( $time ) ) {
        $cart_item_data[] = array(
            'key'     => __( 'Time', 'woocommerce'),
            'value'   => $time,
        );
    }
    
    // Get terms
    $term_names = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'names' ) );

    if( ! empty( $term_names ) ) {
        $cart_item_data[] = array(
            'key'     => __( 'Categories', 'woocommerce'),
            'value'   => implode( ", ", $term_names )
        );
    }
    
    // Get post meta
    $date = get_post_meta( $product_id, 'date', true );

    // NOT empty
    if( ! empty( $date ) ) {
        $cart_item_data[] = array(
            'key'     => __( 'Date', 'woocommerce'), 
            'value'   => $date,
        );
    }

    return $cart_item_data;
}
add_filter( 'woocommerce_get_item_data', 'display_custom_product_field_data', 10, 2 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM