Woocommerce 获取购物车项目元数据 [英] Woocommerce get cart item meta
本文介绍了Woocommerce 获取购物车项目元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的产品页面上有一个自定义字段",我想将其添加到 Woocommerce 购物车页面中的产品标题上方.
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.
这是自定义字段数据:
我用这个 PHP 代码让它在单个产品卡上工作
I got it to work on a single product card with this PHP code
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>';
}
}
我为购物车页面尝试了相同的代码,但对其进行了一些更改,但未加载自定义字段的元数据.我使用过 $get_item_data
和更多但无法让它工作
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
<p class="cart-subtitel">
<?php $beschrijving = get_post_meta( $product->id, 'product_card_beschrijving', true ); echo '<span class="beschrijving">' . $beschrijving . '</span>'?>
</p>
推荐答案
这是改编自我的字幕插件/WooCommerce bridge 插件
This is adapted from my Subtitle plugin/WooCommerce bridge plugin
/**
* 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 );
这篇关于Woocommerce 获取购物车项目元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文