简体   繁体   English

Woocommerce 获取购物车项目元

[英]Woocommerce get cart item meta

I have a 'Custom field' on my product page that I want to add above the title of a product in a Woocommerce cart page.我的产品页面上有一个“自定义字段”,我想在 Woocommerce 购物车页面中的产品标题上方添加它。

This is the custom field data:这是自定义字段数据:

在此处输入图像描述

I got it to work on a single product card with this PHP code我用这个 PHP 代码让它在单个产品卡上工作

add_action( 'woocommerce_after_shop_loop_item_title', 'product_card_beschrijving', 2);
 function product_card_beschrijving(){
    global $product;
    $beschrijving = get_post_meta( $product->id, 'product_card_beschrijving', true ); 
    if ( ! empty( $beschrijving ) ) {
        echo '<span class="beschrijving">' . $beschrijving . '</span>';
    }
 }

I've tried this same code for the cart page but altered it a little as such but the meta data of the custom field isn't being loaded.我已经为购物车页面尝试了相同的代码,但对其进行了一些更改,但未加载自定义字段的元数据。 I've used $get_item_data and more but can't get it to work我已经使用$get_item_data等等,但无法让它工作

<p class="cart-subtitel">
    <?php $beschrijving = get_post_meta( $product->id, 'product_card_beschrijving', true ); echo '<span class="beschrijving">' . $beschrijving . '</span>'?>
</p>

This is adapted from my Subtitle plugin/WooCommerce bridge plugin这是改编自我的字幕插件/WooCommerce 桥插件

/**
 * Cart product title.
 *
 * @param string $title - The product title.
 * @param array  $cart_item - The array of cart item product data.
 * @return string
 */
function kia_add_subtitle_to_cart_product( $title, $cart_item ){
    $_product = $cart_item['data'];
    $meta     = $_product->get_meta( 'product_card_beschrijving', true );
    if( $meta ) {
        $title .= '<span class="meta">' . $meta . '</span>';
    }
    return $title;
}
add_filter( 'woocommerce_cart_item_name', 'kia_add_subtitle_to_cart_product', 10, 2 );

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

相关问题 通过使用 Woocommerce 中的自定义元数据的 Get 请求将商品添加到购物车 - Add item to cart through a Get request with custom meta data in Woocommerce 在 WooCommerce 中添加自定义字段作为购物车项目元和订单项目元 - Add custom fields as cart item meta and order item meta in WooCommerce 在 WooCommerce 购物车中获取购物车项目的产品 ID - Get in WooCommerce cart the product ID of a cart item woocommerce_order_item_meta_end挂钩中的get_cart()方法出错 - Error with get_cart() method in woocommerce_order_item_meta_end hook 将自定义购物车商品值添加到WooCommerce订单商品元数据 - Add a custom cart item value to WooCommerce order item meta data 在Woocommerce中按商品ID获取购物车商品数量 - Get the cart item quantity by item id in Woocommerce WooCommerce - 2 个产品是否会显示为具有相同购物车项目元数据的唯一产品 - WooCommerce - Will 2 products show as unique with the same cart item meta 使用 Woocommerce 3 中的购物车项目自定义数据更新订单元数据 - Update order meta with cart item custom data in Woocommerce 3 仅当购物车项目具有特定元数据时,才从购物车页面删除 WooCommerce 购物车数量选择器 - Remove WooCommerce cart quantity selector from cart page only if cart item has specific meta data 获取所有woocommerce购物车商品名称 - Get ALL woocommerce cart item names
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM