Sorry I am somewhat of a novice at php but am wondering if someone might be able to point me in the right direction. I have a function created that works perfect. It is used in woocommerce and for a certain category only, removes the 'add to cart' button and replaces with a link to another page. There is one product in this category that I need to ignore the function for. The working code is:
function fishing_buttons(){
// die early if we aren't on a product
if ( ! is_product() ) return;
$product = get_product();
if ( has_term( 'fishing', '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 );
// adding our own custom text
add_action( 'woocommerce_after_shop_loop_item', 'fishing_priceguide' );
add_action( 'woocommerce_single_product_summary', 'fishing_priceguide', 30 );
add_action( 'woocommerce_simple_add_to_cart', 'fishing_priceguide', 30 );
add_action( 'woocommerce_grouped_add_to_cart', 'fishing_priceguide', 30 );
add_action( 'woocommerce_variable_add_to_cart', 'fishing_priceguide', 30 );
add_action( 'woocommerce_external_add_to_cart', 'fishing_priceguide', 30 );} // fishing_buttons
add_action( 'wp', 'fishing_buttons' );
/**
* Our custom button
*/
function fishing_priceguide(){
echo do_shortcode( '[pl_button type="fish" link="http://localhost:8888/fish/fishing-price-guide/"]View our price guide[/pl_button]' );
} // fishing_priceguide
The product ID is 1268 that I want to ignore (i.e. keep the add to cart button). Is it possible to include an 'and' condition in the if statement? I have tried
if ( has_term( 'fishing', 'product_cat' ) && product_id != '1268' ){
But have not had any success
product means nothing as you have it. $product is an object and the product ID is stored as the class variable $product->id. Therefore your code should be:
if ( has_term( 'fishing', 'product_cat' )&& $product->id != '1268' )
Also what hook is fishing_buttons attached to? You can probably just use the global $product but don't need to retrieve the product again.
function fishing_buttons(){
global $product;
if ( has_term( 'fishing', 'product_cat' )&& $product->id != '1268' ){
// your stuff
}
}
add_action( 'woocommerce_before_shop_loop_item', 'fishing_buttons' );
Related
as the title states, I'm trying to remove the buy button for virtual products. I am using the Astra theme in WordPress alongside Woocommerce, and Elementor.
Now my issue is that with my current implementation, it does remove the button from the individual product page. However it still displays the button on the categories page, which is a pain. I have tried setting the price to nothing, and while that does work. It doesn't help as I still want the products to have their prices listed.
I have added the following code to the functions.php section of my duplicated theme file;
function buy_filter()
{
if ( ! is_product() ) return;
$product = get_product();
if ($product->is_virtual('yes'))
{
//$product->is_purchasable('false')
//remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
//remove_action('woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30);
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', 'buy_filter');
Any help would be greatly appreciated, and I'm happy to give any more details I can to aid in the matter.
Thank you kindly!
You need to get the specific product by it's ID, I have modified a few part of your code and removed few of the hooks you used. Please add those if required.
function woocommerce_remove_cart()
{
if ( ! is_product() ) return;
$product_id=get_the_ID();
$product = wc_get_product($product_id);
if ($product->is_virtual('yes'))
{
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 );
}
}//end of function
add_action('wp_head','woocommerce_remove_cart');
Hope it works.
add_filter( 'woocommerce_is_purchasable', 'woocommerce_isproduct_purchasable', 10, 2 );
function woocommerce_isproduct_purchasable( $purchasable, $product ) {
return ($product->is_virtual( 'yes' ) ) ? false : $purchasable;
}
This is the hook you are looking for
On my site, I want to hide "Add to Cart" button only for logged out user /"Guest Users"
This is the code that I have added to my functions.php file into my theme.
// For product archives pages
add_action( 'init', 'hide_product_archives_prices' );
function hide_product_archives_prices(){
if( is_user_logged_in() ) return;
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) ;
}
//
add_action( 'woocommerce_single_product_summary', 'hide_single_product_prices', 1 );
function hide_single_product_prices(){
if( is_user_logged_in() ) return;
global $product;
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
if( ! $product->is_type('variable') ){
remove_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart', 30 );
} else {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 10);
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
}
}
Everything works fine as it hides the add to cart button for logged out users, but i still want the quantity selector to be available, because I am using a Request a quote plugin to add the product with the quantity to a quote cart. All products are variable products
At this stage the enquire now button shows, but when you click it to add it to the quote cart it doesn't add it because there's no quantity selected.
I have two templates, one for Panorama & Multi-Panel photographs, the other for Normal ones.
I'm trying to code a conditional statement that checks the product tag in order to select the relevant template to be displayed.
Panorama photos have the product tag 'panorama' or 'multi-panel'.
If the 'panorama' or 'multi-panel' tag is true >> perform some remove_action and add_action functionality on the Panorama product page...
...otherwise do nothing to the Normal product page...
This is what I have been dappling with to no affect!!
function robhemphill_panorama_layout () {
if ( has_tag( array( 'panorama', 'multi-panel'))) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_title', 5 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_excerpt', 10 );
} else {
// Do nothing
}
}
add_action( 'woocommerce_single_product_summary', 'robhemphill_panorama_layout' );
Any thoughts or ideas would be great, can't find anything online!
As you are targeting Woocommerce product tags (but not WordPress tags) try this instead:
add_action( 'woocommerce_single_product_summary', 'robhemphill_panorama_layout', 1 );
function robhemphill_panorama_layout () {
global $product;
if ( has_term( array( 'panorama', 'multi-panel' ), 'product_tag', $product->get_id() ) ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_title', 5 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_excerpt', 10 );
}
}
Code goes in function.php file of your active child theme (or theme).
Tested and works.
I am designing Woocommerce single product page,
I have designed product page for simple product
now I am designing for variable product for that I have created this function
to arrange "Choose variation" selection and "Add to cart" button
function filter_grouped_cart(){
global $post;
if( function_exists('get_product') ){
$product = get_product( $post->ID );
if( $product->is_type( 'variable' ) ){
remove_action( 'woocommerce_variable_add_to_cart',
'woocommerce_variable_add_to_cart', 30 );
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_variable_add_to_cart', 45 );
remove_action( 'woocommerce_single_variation',
'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_single_variation_add_to_cart_button', 60 );
}
}
}
I used conditional logic because without it my simple product page show nothing.
This code is working fine but I have these actions and their priority
remove_action( 'woocommerce_before_single_product_summary',
'woocommerce_show_product_images', 20 );
add_action( 'woocommerce_single_product_summary',
'woocommerce_show_product_images', 20 );
remove_action( 'woocommerce_single_product_summary',
'woocommerce_template_single_title', 5 );
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_template_single_title', 30 );
remove_action( 'woocommerce_after_single_product_summary',
'woocommerce_output_product_data_tabs', 10 );
add_action( 'woocommerce_before_single_product_summary',
'the_content', 40 );
remove_action( 'woocommerce_single_product_summary',
'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_template_single_price', 50 );
remove_action( 'woocommerce_single_product_summary',
'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_template_single_add_to_cart', 60 );
I want "Choose Option" selection before price i.e.
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_template_single_price', 50 );
Its priority is 50
so i have added priority 45 for
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_variable_add_to_cart', 45 );
But still woocommerce_variable_add_to_cart is showing below
woocommerce_template_single_price
In the above picture if I have 45-50-60 then 60 and 50 will come in single line, and 45 above 50, that is my requirement.
I tried you code but it broke the structure of the page for me here.
Then ultimately it lead me to research through the hooks and tried some permutation and combination which resulted in this code.
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_single_variation', 'woocommerce_template_single_price', 19 );
Hope that works for you. Have not added any conditional logic here to restrict it from getting applied on other types of products(I think that you can handle very well).
I'm working on a woocommerce theme and I'm required to hide the add to cart button for products that have 0 as price, as these products may only be inquired and not added to cart. I have successfully hidden the 'add to cart' button on the product page however, I am having a hard time doing so on the shop page/category page.
Below is my code for filtering the add to cart as well as changing the default 'Free!' message.
add_filter( 'woocommerce_variable_free_price_html', 'hide_free_price_notice' );
add_filter( 'woocommerce_free_price_html', 'hide_free_price_notice' );
add_filter( 'woocommerce_variation_free_price_html', 'hide_free_price_notice' );
/**
* Changes woocommerce default 'Free!' to return message
*/
function hide_free_price_notice( $price ) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 30 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
return 'Please inquire for pricing';
}
I have also tried filtering all add to cart buttons on loop, however this did not work either.
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}
Any suggestions? I'm looking to see if I can hide it with CSS...
This is what I use to change the 'Free!' message to 'POA' and also hide the cart.
Note: There seems to be an issue with this on WooCommerce version V2.1?
* Swop the 'Free!' price notice and hide the cart with 'POA' in WooCommerce
*/
add_filter( 'woocommerce_variable_free_price_html', 'hide_free_price_notice' );
add_filter( 'woocommerce_free_price_html', 'hide_free_price_notice' );
add_filter( 'woocommerce_variation_free_price_html', 'hide_free_price_notice' );
function hide_free_price_notice( $price ) {
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 'POA';
}