我的产品页面上有一个“自定义字段”,我想将其添加到 Woocommerce 购物车页面中的产品标题上方。
这是自定义字段数据:
我用这个 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>';
}
}
我已经为购物车页面尝试了相同的代码,但对其进行了一些更改,但未加载自定义字段的元数据。我用过$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>
这是改编自我的字幕插件/WooCommerce 桥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 );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)