【问题标题】:Woocommerce get cart item metaWoocommerce 获取购物车项目元
【发布时间】:2020-02-28 17:51:59
【问题描述】:

我的产品页面上有一个“自定义字段”,我想在 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>

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案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 );
    

    【讨论】:

    • 这很有魅力!非常感谢你的帮助!我在代码中使用了$_product-&gt;get_meta,这正是我想要的!
    猜你喜欢
    • 2017-04-27
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    • 2014-04-10
    • 2015-12-22
    • 1970-01-01
    • 2017-12-13
    相关资源
    最近更新 更多