[英]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.