【问题标题】:Hide price for specific categories on cart page在购物车页面上隐藏特定类别的价格
【发布时间】:2018-09-06 11:41:40
【问题描述】:

我正在尝试从产品页面 + 购物车中隐藏特定类别的价格,并且我正在使用我在其他地方找到的代码 sn-p。

<?php 
add_filter( 'woocommerce_get_price_html', function( $price, $product ) {
    if ( is_admin() ) return $price;
    // Hide for these category slugs / IDs
    $hide_for_categories = array( 'berkley','cotton-lite','kinna','linen','luster','nairobi','panama','plisse','prints','sequoia','shantung','brocade','boucle','dover','lite-out','lite-out-duplex','moire','sheerweave-blackout','sutton','texas-green','windsor','sheer','sheerweave-3-5-10','sheerweave-specialty');
    // Don't show price when its in one of the categories
    if ( has_term( $hide_for_categories, 'product_cat', $product->get_id() ) ) {
        return '';
    }
    return $price; // Return original price
}, 10, 2 );
add_filter( 'woocommerce_cart_item_price', '__return_false' );
add_filter( 'woocommerce_cart_item_subtotal', '__return_false' );
 ?>

我面临的问题是此代码隐藏了购物车中的所有类别价格。它非常适合产品页面,但不适用于购物车。任何人都可以帮助我在代码中进行哪些更改?我是 WordPress 编码的新手。

非常感谢。

【问题讨论】:

    标签: php wordpress woocommerce content-management-system cart


    【解决方案1】:

    这应该可以解决问题,将在您的类别列表中的每个产品的购物车页面上隐藏价格和总计。

    add_filter('woocommerce_cart_item_price', 'hide_woocommerce_cart_item_price', 10, 3);
    add_filter('woocommerce_cart_item_subtotal', 'hide_woocommerce_cart_item_price', 10, 3);
    
    function hide_woocommerce_cart_item_price( $price,  $cart_item, $cart_item_key ) {
        $product = wc_get_product($cart_item['product_id']);
        $hide_for_categories = array( 'berkley','cotton-lite','kinna','linen','luster','nairobi','panama','plisse','prints','sequoia','shantung','brocade','boucle','dover','lite-out','lite-out-duplex','moire','sheerweave-blackout','sutton','texas-green','windsor','sheer','sheerweave-3-5-10','sheerweave-specialty');
        // Don't show price when its in one of the categories
        if ( has_term( $hide_for_categories, 'product_cat', $product->get_id() ) ) {
            return '';
        }
        return $price; 
    }
    

    【讨论】:

    • 非常感谢,伙计。它完全符合我的要求。还有一件事,当我将鼠标悬停在购物车图标上时,它仍会显示价格,并且会出现带有价格的产品下拉列表,尽管在购物车页面上它是隐藏的。
    • @AzeemAmin 听起来这是一个主题相关的问题。请提出一个新问题,我会协助解决。谢谢。
    • 谢谢,安德鲁,当我发布这个问题时,我会在这里通知你,因为我是新来的,所以我每天只能发布 1 个问题。
    • stackoverflow.com/questions/49532567/… 这是安德鲁到我的新问题的链接。
    猜你喜欢
    • 2017-05-02
    • 2020-06-06
    • 2023-04-07
    • 1970-01-01
    • 2021-08-24
    • 2021-07-22
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    相关资源
    最近更新 更多