在WooCommerce购物车和结帐表中显示产品自定义字段值 [英] Display a product custom field value in WooCommerce cart and checkout table

查看:182
本文介绍了在WooCommerce购物车和结帐表中显示产品自定义字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce中,我为每个产品添加了一个自定义字段说明。
我能够找到一种同时显示标签名称和值的方法:

In WooCommerce and I have added a custom field "description" for each product. I was able to find a way to show both, the label name and the value:

add_filter( 'woocommerce_add_cart_item_data', 'save_days_field', 10, 2 );
function save_days_field( $cart_item_data, $product_id ) {
    $special_item = get_post_meta( $product_id , 'description',true );

    if(!empty($special_item)) {
        $cart_item_data[ 'description' ] = $special_item;

        // below statement make sure every add to cart action as unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
        WC()->session->set( 'description', $special_item );
    }
    return $cart_item_data;
}

// Render meta on cart and checkout
add_filter( 'woocommerce_get_item_data','rendering_meta_field_on_cart_and_checkout', 10, 2 );
function rendering_meta_field_on_cart_and_checkout( $cart_item_data, $cart_item ) {
    if( isset( $cart_item['description'] ) ) {
        $cart_item_data[] = array( "name" => __( "Description", "woocommerce" ), "value" => $cart_item['description'] );
    }
    return $cart_item_data;
}

现在我只需要显示值(而不是标签名称 Description )中的购物车和结帐表中的此自定义字段。我需要显示< small> ,就像我用以下代码显示的属性一样:

Now I need to display ONLY the value (not the label name "Description") of this custom field in the cart and checkout table. I need to display with <small>, like the attribute I am displaying with this code:

add_filter('woocommerce_cart_item_name', 'wp_woo_cart_attributes', 10, 2);
function wp_woo_cart_attributes($cart_item, $cart_item_key){
    $productId = $cart_item_key['product_id'];
    $product = wc_get_product($productId);
    $taxonomy = 'pa_color';
    $value = $product->get_attribute($taxonomy);

    if ($value) {
        $label = get_taxonomy($taxonomy)->labels->singular_name;
        $cart_item .= "<small>$value</small>";
    }
    return $cart_item;
}

如何在此自定义字段中使其仅显示值?

How can I make it for this custom field, displaying the value only?

推荐答案

您无需将产品自定义字段包含为自定义购物车项目数据,因为可以从产品对象直接访问该字段(或产品ID)

You don't need to include a product custom field as custom cart item data, as it's directly accessible from the product object (or the product ID).

注意:在购物车商品变量 $ cart_item ,包含 WC_Product 对象,并可以通过 $ cart_item ['data']

Note: On a cart item variable $cart_item, the WC_Product Object is included and available using $cart_item['data'].

尝试以下操作在购物车和结帐页面中的商品名称后添加自定义字段:

Try the following to add a custom field after the item name in cart and checkout pages:

// Display in cart and checkout pages
add_filter( 'woocommerce_cart_item_name', 'customizing_cart_item_name', 10, 3 );
function customizing_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
    $product = $cart_item['data']; // Get the WC_Product Object

    if ( $value = $product->get_meta('description') ) {
        $product_name .= '<small>'.$value.'</small>';
    }
    return $product_name;
}

要在订单和电子邮件通知中显示,请使用:

To display it on orders and email notifications use:

// Display in orders and email notifications
add_filter( 'woocommerce_order_item_name', 'customizing_order_item_name', 10, 2 );
function customizing_order_item_name( $product_name, $item ) {
    $product = $item->get_product(); // Get the WC_Product Object

    if ( $value = $product->get_meta('description') ) {
        $product_name .= '<small>'.$value.'</small>';
    }
    return $product_name;
}

代码进入活动子主题(或活动主题)的function.php文件)。经过测试,可以正常工作。

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于在WooCommerce购物车和结帐表中显示产品自定义字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆