In my functions.php file I have some remove_actions and add_filters that run for woocommerce but the problem is these functions run for all woocommerce product types.
I'd like to wrap a simple if statement around the hooks/filters I have to only run for grouped product pages the problem is I dont know how woocommerce refers to these pages heres what I have at the moment.
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
Heres what i'd like to do but I dont know the correct reference for $grouped_product.
if ($grouped_product) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
I have two add_filter and one remove action i'd like to append in my functions.php to only execute on grouped product pages I just need the correct reference in the first set of brackets in the second code block above.
Tried this code but it doesn't work..
if (is_product() and $product->is_type('grouped')) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
functions php file
<?php
function theme_enqueue_styles() {
$parent_style = 'parent-style';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style )
);
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
//Remove cart options from under short description.
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
//Filter below adds new tab and returns cart options within tab.
add_filter( 'woocommerce_product_tabs', 'woo_paym_product_tab' );
function woo_paym_product_tab( $tabs ) {
// Adds the new tab
$tabs['paym-plans'] = array( 'title' => __( 'Contract Deals', 'woocommerce' ), 'priority' => 10, 'callback' => 'woo_paym_product_tab_content' );
return $tabs;
}
function woo_paym_product_tab_content() {
// The new tab content
woocommerce_template_single_add_to_cart();
}
//Filter below changes tab priority to display price plans first.
add_filter( 'woocommerce_product_tabs', 'sb_woo_move_description_tab', 98);
function sb_woo_move_description_tab($tabs) {
$tabs['paym-plans']['priority'] = 10;
$tabs['description']['priority'] = 20;
$tabs['additional_information']['priority'] = 30;
return $tabs;
}
?>
Solved the problem trick was to change it to a filter.
add_filter( 'woocommerce_single_product_summary', 'filter_grouped_cart');
function filter_grouped_cart(){
global $post;
if( function_exists('get_product') ){
$product = get_product( $post->ID );
if( $product->is_type( 'grouped' ) ){
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
}
Checking $product->is_type('grouped') will not help here...
Please check if the code provides any help to you..
global $post;
if( $post->post_parent != 0 ){
echo 'is part of a group';
}
Related
I want to hide the price for the users that aren't logged on to my site. I managed to hide the price for single products and for variable products. In variable products that have different prices for single variants, this code does not work. How can I fix it?
add_action( 'init', 'nascondi_prezzo_se_non_loggato' );
function nascondi_prezzo_se_non_loggato() {
if ( ! is_user_logged_in() ) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_filter( 'woocommerce_is_purchasable', '__return_false' );
add_action( 'woocommerce_single_product_summary', 'mostra_testo_alternativo', 31 );
add_action( 'woocommerce_after_shop_loop_item', 'mostra_testo_alternativo', 11 );
}
}
function mostra_testo_alternativo() {
echo '<div class="button-prezzo">' . __( 'Accedi per vedere i prezzi', 'JupiterX' ) . '</div>';
}
This is the code that I used and that only partially solves the problem.
Try the code snippet below. The price is set to an empty string that won't display the price for users who aren't logged in. This uses the filter woocommerce_get_price_html which returns the code that outputs the price. In the empty string, add what you want displayed instead of the price.
add_filter( 'woocommerce_get_price_html', 'hide_price_not_logged_in', 10, 2 );
function hide_price_not_logged_in( $price, $product ) {
if ( ! is_user_logged_in() ) {
$price = '';
}
return $price;
}
To help everyone: I use the code above that hides the price and part of my code to hide the add to cart button
add_filter( 'woocommerce_get_price_html', 'hide_price_not_logged_in', 10, 2 );
function hide_price_not_logged_in( $price, $product ) {
if ( ! is_user_logged_in() ) {
$price = '';
}
return $price;
}
add_action( 'init', 'nascondi_prezzo_se_non_loggato' );
function nascondi_prezzo_se_non_loggato() {
if ( ! is_user_logged_in() ) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_filter( 'woocommerce_is_purchasable', '__return_false' );
add_action( 'woocommerce_single_product_summary', 'mostra_testo_alternativo', 31 );
add_action( 'woocommerce_after_shop_loop_item', 'mostra_testo_alternativo', 11 );
}
}
function mostra_testo_alternativo() {
echo '<div class="button-prezzo">' . __( 'Accedi per vedere i prezzi', 'your-theme-name' ) . '</div>';
}
I am attempting to alter the "Add to Cart" button test on single product pages only when a product is £40 or more. I have attempted the below snippet but it does not alter anything.
/**
* Change Add to Cart button text for products £40 or more
*
* #return string
*/
function banks_cart_button_text() {
global $product;
if( $product->get_regular_price >= '40' ) {
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text' );
function woocommerce_custom_single_add_to_cart_text() {
return __( 'Add to Cart with FREE UK Shipping', 'woocommerce' );
}
}
return $product->get_regular_price();
}
I was about to post and came across the below post which I updated but this also does not work.
Link: Change add to cart button and text based on WooCommerce product type Author: LoicTheAztec
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
// Products above X amount
if( $product->get_regular_price >= '40' ) {
$button_text = __( "Add to Cart with FREE UK Shipping", "woocommerce" );
}
return '<a class="view-product button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
UPDATE:
Adapted another snippet I used a while back but does not work. Is the issue the this line:
if( $product->get_regular_price >= '40' ) {
Snippet
// Replacing the single product button add to cart by a custom button when store is closed
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
global $product, $url, $request ;
// Products equal to or more than X amount
if( $product->get_regular_price >= '40' ) {
// For variable product types (keeping attribute select fields)
if( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_single_variation', 'custom_product_button', 20 );
}
// For all other product types
if( $product->is_type( 'simple' ) ) {
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
add_action( 'woocommerce_simple_add_to_cart', 'custom_product_button', 30 );
}
// For all other product types
else {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_simple_add_to_cart', 'custom_product_button', 30 );
}
}
else {
// Products equal to or more than X amount
if( $product->get_regular_price >= '40' ) {
// For variable product types (keeping attribute select fields)
if( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_single_variation', 'custom_product_button', 20 );
}
// For all other product types
if( $product->is_type( 'simple' ) ) {
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
add_action( 'woocommerce_simple_add_to_cart', 'custom_product_button', 30 );
}
// For all other product types
else {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_simple_add_to_cart', 'custom_product_button', 30 );
}
}
}
}
function custom_product_button(){
// Display button
printf( '<a class="button disabled">%s</a>', __( "Add to Cart with FREE UK Shipping", "woocommerce" ) );
}
no need to place a function inside another. this should be enough.
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text', 100, 2 );
function woocommerce_custom_single_add_to_cart_text( $add_to_cart_text, $product ) {
if ( $product->get_regular_price() >= '40' ) {
return __( 'Add to Cart with FREE UK Shipping', 'woocommerce' );
}
return $add_to_cart_text;
}
Try
if( $product->get_regular_price() >= 40 ) {
(brackets on the end and compare price against int not a string).
Also check that you're getting a value.
Depending on your setup you may need to check for
$product->get_sale_price()
or
$product->get_price()
You can use the functions above, or by using Locotranslate plugin, it creates templates for the site's texts (separated by files based on the origin of the word, for example woocommerce file, theme file, plugins file) for each language, so you just replace the "add to cart" word in the english language template of woocommerce file with the text you want.
I am trying to make some changes to code I have found off here that will allow me to hide all the prices of my woocommerce store EXCEPT for one category.
The category I want to display prices for all the time is 'courses' but I want to hide the rest of the products prices until the user is logged in. I am trying to make changes to this code
add_action( 'init', 'bbloomer_hide_price_add_cart_not_logged_in' );
function bbloomer_hide_price_add_cart_not_logged_in() {
if (! is_user_logged_in() &&
! has_term( 'courses', '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_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
add_action( 'woocommerce_single_product_summary', 'bbloomer_print_login_to_see', 31 );
add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_print_login_to_see', 11 );
}
}
function bbloomer_print_login_to_see() {
echo '<a class="button product_type_simple" href="' .
get_permalink(wc_get_page_id('myaccount')) . '">' .
__('Login to see prices', 'theme_name') . '</a>';
}
Any help would be much appreciated!
Instead of removing every action that outputs the price simply, remove the price.
This code would filter the get_price_html() function from the WC_Product class and return an empty string.
function bbloomer_hide_price_add_cart_not_logged_in() {
global $product;
if (! is_user_logged_in() &&
!has_term( 'courses', 'product_cat', $product->get_id()))
{
add_filter('woocommerce_get_price_html', '__return_empty_string', 10);
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_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
}
or... to add your click to login button...
function bbloomer_hide_price_add_cart_not_logged_in() {
global $product;
if (!is_user_logged_in() &&
!has_term( 'courses', 'product_cat', $product->get_id())) // add product ID to check if that specific product is part of courses category.
{
add_filter('woocommerce_get_price_html', 'bbloomer_print_login_to_see'),
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_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 ); 10);
}
}
function bbloomer_print_login_to_see() {
// Modify the function to return the value, not echo it.
return '<a class="button product_type_simple" href="' .
get_permalink(wc_get_page_id('myaccount')) . '">' .
__('Login to see prices', 'theme_name') . '</a>';
}
Revised Answer Additions
Access the current $product inside your loop to check if it's part of courses category.
Remove add to cart button from single product pages if the product is part of the category courses
On my site (using Woocommerce 3.2.6), I want to hide "Add to Cart" button only for logged in users.
I have this code:
add_action('init', 'hide_price_add_cart_logged_in');
function hide_price_add_cart_logged_in() {
if ( is_user_logged_in() ) {
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 );
}
}
EDIT: Someone suggested me to use this:
add_action('init', 'hide_price_add_cart_logged_in');
function hide_price_add_cart_logged_in() {
if ( is_user_logged_in() ) {
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 );
return WooCommerce::instance();
}
}
But that did not work...
I have inserted this code into functions.php file into my theme, but it don't seems to make any change. I still see add to cart button when check some product.
How to remove that button? Where is error in my function?
You should try this instead:
add_action( 'woocommerce_after_shop_loop_item', 'remove_loop_add_to_cart_button', 1 );
function remove_loop_add_to_cart_button() {
// Only for logged in users
if( ! is_user_logged_in() ) return;
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}
add_action( 'woocommerce_single_product_summary', 'remove_add_to_cart_button', 1 );
function remove_add_to_cart_button() {
// Only for logged in users
if( ! is_user_logged_in() ) return;
global $product;
// For variable product types (keeping attribute select fields)
if( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
}
// For all other product types
else {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works
Instead of hiding add to cart button on archives pages (like shop) you could replace it with a button linked to the product. So the code will be instead:
// Replacing the button add to cart by a link to the product in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
// Only for logged in users
if( ! is_user_logged_in() ) return;
$button_text = __( "View product", "woocommerce" );
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
return $button;
}
add_action( 'woocommerce_single_product_summary', 'remove_add_to_cart_button', 1 );
function remove_add_to_cart_button() {
// Only for logged in users
if( ! is_user_logged_in() ) return;
global $product;
// For variable product types (keeping attribute select fields)
if( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
}
// For all other product types
else {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );;
}
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works
add_action( 'init', 'shessvy_hide_price_add_cart_not_logged_in' );
function shessvy_hide_price_add_cart_not_logged_in() {
if ( ! is_user_logged_in() ) {
add_filter( 'woocommerce_is_purchasable', '__return_false');
add_action( 'woocommerce_single_product_summary', 'shessvy_print_login_to_see', 31 );
add_action( 'woocommerce_after_shop_loop_item', 'shessvy_print_login_to_see', 11 );
add_action( 'woocommerce_simple_add_to_cart', 'shessvy_print_login_to_see', 30 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
function shessvy_print_login_to_see() {
echo '' . __('Login to add this item to your cart', 'theme_name') . '';
}
I have an issue how to remove a cart from a category product. It works just fine if I apply it to a specific id or all in general, but I am unable to do it for a category. Below is my code I have done regarding it.
Also, I am struggling to apply this same pattern to Related Articles section, so any help would be appreciated.
Thank you.
//function for deleting ....
function remove_product_description_add_cart_button(){
global $product;
//Remove Add to Cart button from product description of product with id 1234
if ($product->id == 188){
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
add_action('wp','remove_product_description_add_cart_button');
}
Nov 2020 Update
To make it work with a product category you can use the WordPress conditional function has_term() this way:
add_action('woocommerce_single_product_summary', 'remove_product_description_add_cart_button', 1 );
function remove_product_description_add_cart_button() { // function for deleting ...
// Set HERE your category ID, slug or name (or an array)
$categories = array('your-category-1');
//Remove Add to Cart button from product description of product with id 1234
if ( has_term( $categories, 'product_cat', get_the_id() ) ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
Code goes in function.php file of your active child theme (or active theme). Or also in any plugin php file. Tested and works.
You can try something like this
function western_custom_buy_buttons(){
$product = get_product();
if ( has_term( 'category1', 'product_cat') ){
// removing the purchase buttons
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
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 );
remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );
}
}
add_action( 'wp', 'western_custom_buy_buttons' );
Reference : https://www.themelocation.com/how-to-remove-add-to-cart-button-from-a-certain-category-woocommerce/
you can add your own category name in line 3
add_filter('woocommerce_is_purchasable', 'set_catalog_mode_on_for_category', 10, 2 );
function set_catalog_mode_on_for_category( $is_purchasable, $product ) {
if( has_term( 'Category Name', 'product_cat', $product->get_id() ) ) { //Change Category name here
return false; }
return $is_purchasable;
}
Source: https://webtalkhub.com/how-to-remove-add-to-cart-button-in-woocommerce/
You have try something like this:
function remove_product_description_add_cart_button(){
global $product;
$termsOfProduct = wp_get_post_terms( $product->id, 'product_cat' );
if (in_array("CatToFind", $termsOfProduct)) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
add_action('wp','remove_product_description_add_cart_button');
}
Category products in Woocommerce are simply terms. Wp_get_post_terms allow you to find any categories associated to post (product id).
Reference: https://codex.wordpress.org/Function_Reference/wp_get_post_terms
Use this code
/*
Plugin Name: Remove 'Add to cart' conditionally
Plugin URI: https://www.damiencarbery.com/2020/03/remove-add-to-cart-conditionally/
Description: Conditionally remove the 'Add to cart' button in WooCommerce.
Author: Damien Carbery
Version: 0.2
*/
class IsPurchasableConditionalFiltering {
// A reference to an instance of this class.
private static $instance;
// Store whether 'Add to cart' button should be displayed.
private $purchasable;
// Returns an instance of this class.
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new IsPurchasableConditionalFiltering;
}
return self::$instance;
}
// Initialize the plugin variables.
public function __construct() {
$this->purchasable = array();
$this->init();
}
// Set up WordPress specfic actions.
public function init() {
add_filter( 'woocommerce_is_purchasable', array( $this, 'is_purchasable_conditionals' ), 10, 2 );
add_filter( 'woocommerce_variation_is_purchasable', array( $this, 'variation_is_purchasable_conditionals' ), 10, 2 );
// Remove variations dropdown and 'Add to cart' button for variable products.
add_action( 'woocommerce_before_single_product_summary', array( $this, 'before_single_product_summary' ) );
}
public function is_purchasable_conditionals( $whether_purchasable, $product ) {
// Return cached result.
if ( array_key_exists( $product->get_id(), $this->purchasable ) ) {
return $this->purchasable[ $product->get_id() ];
}
// Only do our conditional checks if WooCommerce deems the item to be purchasable.
if ( $whether_purchasable ) {
$result = true; // Default to allowing purchase.
// Check our specific conditions - some examples.
/* // Product over a certain price.
if ( $product->get_price() > 2 ) {
$result = false;
}*/
// Check if product in a certain categores.
if ( has_term( array( 'hoodies', 'accessories' ), 'product_cat', $product->get_id() ) ) {
$result = false;
}
$this->purchasable[ $product->get_id() ] = $result;
}
else {
// Store that this item cannot be purchased.
$this->purchasable[ $product->get_id() ] = false;
}
return $this->purchasable[ $product->get_id() ];
}
public function variation_is_purchasable_conditionals( $whether_purchasable, $product ) {
return $whether_purchasable;
}
public function before_single_product_summary() {
$product_id = get_the_ID();
if ( array_key_exists( $product_id, $this->purchasable ) && !$this->purchasable[ $product_id ] ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
}
}
}
$IsPurchasableConditionalFiltering = new IsPurchasableConditionalFiltering;
Reference : https://www.damiencarbery.com/2020/03/remove-add-to-cart-button-conditionally/