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

杰森

我正在尝试将产品类别添加到以下(有效的)代码中,但不确定如何执行。

我试过使用,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;
}
7uc1f3r

以下代码还将在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 );

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

根据WooCommerce结帐页面上的产品类别应用自定义数量参数

在特定的Woocommerce产品类别档案页面上显示产品属性

在Woocommerce商店和产品类别页面上显示高级自定义字段

在Woocommerce的任何页面上仅显示一个链接的产品类别文本

根据 WooCommerce 商店和存档页面上的产品类别在产品价格下方显示图像

在Woocommerce单一产品页面上排除特定的产品类别

限制woocommerce产品类别页面中显示的产品数量

在归档页面上隐藏父产品类别链接

产品类别页面上的结构化数据?

如何使导航栏仅显示在产品类别页面上?

Woocommerce页面产品类别页面的条件标签

WooCommerce在产品标题中显示产品类别

仅显示Woocommerce子产品类别

仅显示 WooCommerce 主要产品类别

在Woocommerce结帐中为产品类别中的特定虚拟产品启用送货地址

更改WooCommerce产品类别标题?

Woocommerce:产品类别小部件

woocommerce获取产品类别ID

Woocommerce的分层产品类别插件

基于Woocommerce中产品类别的条件自定义结帐字段

WooCommerce根据产品类别替换购物车/结帐中的“按需订购”

基于特定产品类别的WooCommerce结帐消息

根据Woocommerce中的产品类别添加自定义结帐字段

在WooCommerce结帐中制作特定产品类别所需的订单注释

从Entrada主题的特定页面上的产品类别中排除产品

(1.9)获取所有产品类别的ID(在产品页面上)

在Woocommerce中显示类别产品之后移动产品类别描述

从显示类别为Woocommerce的子类别的产品类别中删除边栏

在Woocommerce产品类别页面中更改特定的产品按钮文本