Woocommerce获取购物车项目元

时间:2019-11-02 23:10:34

标签: php wordpress woocommerce

我要在Woocommerce购物车页面中产品标题上方的产品页面上添加一个“自定义字段”。

这是自定义字段数据:

enter image description here

我可以使用此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>

1 个答案:

答案 0 :(得分:1)

此内容来自我的字幕插件/ 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 );