简体   繁体   English

有条件地删除购物车中的按钮并隐藏产品类别的价格

[英]Conditionally removing add-to-cart button and hiding prices for product categories

In WooCommerce, I am trying to remove Add-to-cart button from my homepage slider based on certain product categories and hiding the prices. 在WooCommerce中,我试图根据某些产品类别从首页滑块中删除“添加到购物车”按钮并隐藏价格。

I have tried to use that code: 我试图使用该代码:

/* Code for authorisation */

function show_price_logged($price){
if(! is_user_logged_in()) {

        if(has_term( 'teamine', 'product_cat' )){  /* sub categories need not authorisation, but thier parent category needs authorisation */
            return $price;
        }
        if(has_term( 'epicuren', 'product_cat' ) || has_term( 'revision', 'product_cat' ) || has_term( 'image-2', 'product_cat' )){  /* categories needs authorisation */
            remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
            remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );    
            remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );
            return '<a class="amount authorizelink" href="/how-to-get-authorized-its-easy">Authorization needed to purchase <span>Click Here</span></a>';
    }
        if(has_term( 'skinceuticals', 'product_cat' ) || has_term( 'lira', 'product_cat' )){
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
            remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );    
            remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );
            return '<a href="www.radiantskincarespa.com">Please contact the Spa to arrange a consultation and to purchase this product <span></span></a>';
    }
    else{
        return $price;
    }
}
else return $price;
}

But it's not working as expected and add-to-cart buttons are not removed when conditions are matched. 但是,它不能按预期工作,并且条件匹配时,购物车中的按钮也不会移除。

Any help will be appreciated. 任何帮助将不胜感激。 How can I achieve this? 我该如何实现?

Thanks. 谢谢。

UPDATED 更新

错误的屏幕截图

As you don't provide any hook for your function usage, to test it and make it work I have used here woocommerce_price_html and woocommerce_sale_price_html filters hooks (for simple products). 由于您没有为函数使用提供任何挂钩,因此要对其进行测试并使其正常工作,我在这里使用woocommerce_price_htmlwoocommerce_sale_price_html筛选器挂钩(用于简单产品)。

I have changed a little bit your code and it is working successfully now: 我对您的代码做了一些更改,现在可以成功运行:

add_filter('woocommerce_price_html','show_price_logged', 10, 2 );
add_filter('woocommerce_sale_price_html','show_price_logged', 10, 2 );

function show_price_logged( $price, $product ){

    if( !is_user_logged_in() && !has_term( 'teamine', 'product_cat', $product->id ) ){

        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
        remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );

        if( has_term( 'epicuren', 'product_cat', $product->id ) || has_term( 'revision', 'product_cat', $product->id ) || has_term( 'image-2', 'product_cat', $product->id ) )
        {
            $price = '<a class="amount authorizelink" href="/how-to-get-authorized-its-easy">Authorization needed to purchase <span>Click Here</span></a>';
        }
        elseif( has_term( 'skinceuticals', 'product_cat', $product->id ) || has_term( 'lira', 'product_cat', $product->id ) )
        {
            $price = '<a href="www.radiantskincarespa.com">Please contact the Spa to arrange a consultation and to purchase this product <span></span></a>';
        }
        else // set here another text + link in case of…
        {
            $price = '<a href="#">LINK </a><span>NON LOGGED USER / NO TARGETED CATEGORY</span>';
        }
    }

    return $price;
}

I have add a final condition (in case of) that will need a custom text + link… 我添加了一个最终条件(以防万一),这将需要自定义文本+链接…

Code goes in function.php file of your active child theme (or theme) or also in any plugin file. 代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。

This code is tested and works, it should also work for you. 此代码已经过测试并且可以工作,它也应该对您有用。


Update: See the result here on a test server: 更新:在测试服务器上查看结果:

SHOP PAGE (OR ARCHIVES): 商店页面(或档案):

产品在页面上

PRODUCT PAGES: 产品页面:

产品页面

This just works perfectly… 这很完美…

Related to your error in the comments: 与您在评论中的错误有关:
Warning: Missing argument 2 for show_price_logged() … : Warning: Missing argument 2 for show_price_logged() …

This means that you have forget the $product argument in four function. 这意味着您已经忘记了四个函数中的$product参数。 you need to have this: 您需要具备以下条件:

 function show_price_logged( $price, $product ){ 

Instead of this: 代替这个:

 function show_price_logged( $price ){ 

As you are declaring 2 arguments at the end of your add_filter() hooks… 当您在add_filter()挂钩末尾声明2个参数时…

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 添加自定义内容特定产品的下一步“添加到购物车”按钮 - Add custom content Next add-to-cart In button for a specific Product 隐藏产品价格和添加到购物车按钮,但不包括WooCommerce中未注册用户的变体 - Hide product prices and add-to-cart buttons but not variations for unregistered users in WooCommerce 带有magento附加推车的最新产品 - latest product with add-to-cart in magento 在 Woocommerce 单个产品上同步多个 ajax 添加到购物车按钮 - Sync multiple ajax Add-to-cart buttons on Woocommerce single product Woocommerce功能,用于在“ content-product.php”中添加购物车 - Woocommerce function for add-to-cart inside “content-product.php” 根据 WooCommerce 3 中的产品 ID 自定义添加到购物车的消息 - Customizing add-to-cart messages based on the product IDs in WooCommerce 3 在特定的WooCommerce产品类别上自定义“添加到购物车”按钮 - Customize Add to Cart button on specific WooCommerce product categories 根据Woocommerce中的父产品类别更改“添加到购物车”按钮文本 - Change Add To Cart button text based on parent product categories in Woocommerce 如何在woocommerce循环之外使用变化和添加到购物车按钮 - How to use variation and add-to-cart button outside woocommerce loop 在Woocommerce商店页面中更改用于简单产品的购物车按钮 - Change add-to-cart button for simple products in Woocommerce shop page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM