I'm trying to replace the default woocommerce product archive add to cart button based on a condition.
For example
Product A - Checkbox Active --> Display Find A Dealer Button
Product B - Checkbox inactive -- > Display default add to cart button
I have managed to successfully write the code to add the checkbox and the condition to replace the button if the product has a custom checkbox active. The button for product A works fine and diaplyas as intended in the shop archives.
I am however not sure how to retain the woocommerce default add to cart button if for products that do no have this checkbox activate. I thought adding the action would work however I am stumped. Any help would be greatly appreciated. Thank you in advance.
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button' );
function replace_default_button(){
global $product;
if ($product->get_meta('_checkbox_active') === 'yes' ){
return '<button>Finda Dealer</button>';}
else {add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );}
You just forgot the hooked function variables arguments. Try the following instead:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_default_button', 10, 2 );
function replace_default_button( $button, $product ){
if ( $product->get_meta('_checkbox_active') === 'yes' ){
$button = '' . __( "Find a dealer", "woocommerce" ) . '';
}
return $button;
}
Code goes in functions.php file of your active child theme (or active theme). It should work.
Related
I'm trying to remove the add to cart button from the categories page for products that are not purchasable.
This is my current code, which I have placed in the functions.php file of my child theme;
function buy_filter()
{
if ( ! is_product() ) return;
$product_id=get_the_ID();
$product = wc_get_product($product_id);
if ($product->is_virtual('yes'))
if ($product->is_virtual('yes'))
{
add_filter( 'woocommerce_is_purchasable', '__return_false');
}
}
add_action ('wp', 'buy_filter');
I've managed to remove the button from the individual product pages, however I still can't get it to be removed from the categories page. Now when I inspect the button shows the following code:
<a href="?add-to-cart=16972" data-quantity="1" class="button product_type_simple add_to_cart_button
ajax_add_to_cart" data-product_id="16972" data-product_sku="604544617405" aria-label=
"Add “Federal - 9MM Syntech, 115gr” to your cart" rel="nofollow">Add to cart</a>
Does this button need to be disabled in another way?
I really need this button to be removed as its a product that we only want to sell in our store, but we want people to know that we do stock that product as well.
Below is an image of what it currently looks like, and what I want to go. Ideally, I would like the button gone completely, but I will definitely settle for a replacement to the read more button if that is easier.
To remove it from everywhere (on single pages and category pages) use directly the filter instead, replacing your code with:
add_filter( 'woocommerce_variation_is_purchasable', 'filter_virtual_products', 10, 2 );
add_filter( 'woocommerce_is_purchasable', 'filter_virtual_products', 10, 2 );
function filter_virtual_products( $is_purchasable, $product )
{
if ( $product->is_virtual('yes') ) {
$is_purchasable = false;
}
return $is_purchasable;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Please try this code inside your functions.php file
add_filter('woocommerce_is_purchasable', 'woocommerce_cloudways_purchasable');
function woocommerce_cloudways_purchasable($cloudways_purchasable, $product) {
return ($product->id == your_specific_product_id (like 22) ? false : $cloudways_purchasable);
}
Source: https://www.cloudways.com/blog/how-to-remove-hide-or-disable-add-to-cart-button-in-woocommerce/
or Use this plugin will solve your issue without any coding.
https://wordpress.org/plugins/non-purchasable-woocommerce-products/
Currently in woocommerce, add to cart button disabled, if the stock status is out of stock. I add new stock status with the label Discontinued product by using woocommerce_product_stock_status_options, now I am looking for a way to treat this product like it is out of stock.
Since I believe it is better to separate between a product that is not produced anymore and a product that produced and available in another store but it's not in stock.
You can use the following to disable add to cart button based on a custom stock status (where you will replace custom_status_slug by your custom status slug):
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
add_filter('woocommerce_variation_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
function filter_is_purchasable_callback( $purchasable, $product ) {
if ( $product->get_stock_status() === 'custom_status_slug' ) {
return false;
}
return $purchasable;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
I propose a solution that displays the stock status on the product page but not the 'add to cart' button
add_filter('woocommerce_product_is_in_stock', 'filter_is_in_stock_callback', 10, 2 );
function filter_is_in_stock_callback( $stock, $product ) {
if ( $product->get_stock_status() === 'custom_status_slug' ) {
return false;
}
return $stock;
}
Thanks for the first answer :)
I have the official WooCommerce Product Addon's plugin and I am using it on a variable product. however I would like the price to show above the add to cart button instead of below the variation select list as it doesn't now.
I have looked everywhere and I have moved the price for single products to below the short description but can not work out out to do this for variable products with product add-ons.
Screenshot showing current location and desired location:
Any help is greatly appreciated.
WooCommerce Product Addon's plugin displays already a custom pricing table just before add to cart buttons, where the selected variation price is displayed, with the selected option pricing with the subtotal:
You can't only move the displayed selected variation price alone, as it's driven by javascript on live selection event and grouped with variation availability and variation description (if any).
So what you can do is:
1) To move the variation price with its availability and description before add to cart button using:
add_action( 'woocommerce_before_variations_form', 'reposition_display_for_variable_products', 10 );
function reposition_display_for_variable_products() {
global $product;
if( $product_addons = $product->get_meta('_product_addons') ) {
if( sizeof($product_addons) > 0 ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 10 );
add_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 16 );
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
But it will be displayed after the custom "Addon's" pricing table:
2) hide the Woocommerce variation price (as it's already displayed by the custom "Addon's" pricing table)
add_filter( 'woocommerce_available_variation', 'hide_variation_selected_price', 10, 3 );
function hide_variation_selected_price( $data, $product, $variation ) {
if( $product_addons = $product->get_meta('_product_addons') ) {
if( sizeof($product_addons) > 0 ) {
$data['price_html'] = '';
}
}
return $data;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
3) Use both together, moving the variation availability and variation description after "Addon's" pricing table and before add to cart button:
jQuery('.price').insertAfter('.sauce')
a front-end solution (just put the right selectors)
To move the Product Add-Ons Totals placement you can add this to your functions.php file:
if( isset($GLOBALS['Product_Addon_Display']) ){
remove_action( 'woocommerce_product_addons_end', array( $GLOBALS['Product_Addon_Display'], 'totals' ), 10 );
}
And if you want to add it back:
if( isset($GLOBALS['Product_Addon_Display']) ){
add_action( 'some_action_hook', array( $GLOBALS['Product_Addon_Display'], 'totals' ), 10 );
}
You have to make sure of two things though:
You have to pass the id (or some id) as parameter to your new hook.
The JS that generates the totals looks for the #product-addons-total inside some .cart element.
Everything I can find on removing the WooComm Add To Cart button will remove not just the add to cart button but also the pricing/variations, aka the whole add to cart area.
My goal is to enable/disable the ability to purchase a product with a checkbox/selector on the product info page. BUT I still have to be able to see the product variation pricing and the variation drop down menu.
This is important. The pricing shown under the product title, for a variation, will be something like $20.00 - $40.00 and not until you select the variation choice will it show the price next to the add to cart button.
So far I have things working wherein I can remove the add to cart area — variations and all — conditionally on my custom field, but I have no idea how to hide/disable click/remove just the add to cart button and allow variations to still be chosen with the variation price displayed.
function remove_add_to_cart(){
if(get_post_meta(get_the_ID(), 'woo_callforinfo', true)) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
} add_action('woocommerce_single_product_summary','remove_add_to_cart');
Here's what I did. The conditional IF statement is because I have a RETAIL shop with variable products that I don't want affected.
function remove_add_to_cart(){
if ( has_term( 'wholesale', 'product_tag' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
}
}
add_action('woocommerce_single_variation','remove_add_to_cart');
Added this to get rid of the 'Sorry..' message if price is not set.
add_filter( 'gettext', 'customizing_product_variation_message', 10, 3 );
function customizing_product_variation_message( $translated_text,
$untranslated_text, $domain )
{
if ($untranslated_text == 'Sorry, this product is unavailable. Please choose a different combination.') {
$translated_text = __( '-type anything you want here, or leave a space- ', $domain );
}
return $translated_text;
}
Just add the following code in your functions.php and you will find button hidden
I don't know whether my solution is perfect. But it works. Normally if is_purchasable is returned to the filter woocommerce_is_purchasable, the ‘Add to Cart’ button is displayed, and if false is returned the button is hidden.
So, you just need to add the following:
add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);
function my_woocommerce_is_purchasable($is_purchasable, $product) {
// Write code to access custom field value in this function
// let $custom_value be the value from checkbox
return ($custom_value == false ? false : $is_purchasable);
}
No incompatibility issues would creep up.
Do anyone know how I can remove the number of current available stock that is shown on my Woocommerce product page next the the title of the product? I guess this has changed since the recent Woocommerce update because adding the code snippet
add_filter( 'woocommerce_subcategory_count_html', 'woo_remove_category_products_count' );
function woo_remove_category_products_count() {
return;
}
no longer works. Does anyone have a solution to this? All suggestions are very much appreciated and thank you in advance!
Navigate to WooCommerce > Settings > Product > Inventory. There is a setting "Stock Display Format". Select the "Never Show Stock Amount" from the drop down.
by CSS .stock { display:none; }
The proper way to do it. Copy and paste this code into you functions.php file in your child theme. Code is tested and working.
/* Remove "in stock" text form single products */
function remove_in_stock_text_form_single_products( $html, $text, $product ) {
$availability = $product->get_availability();
if ( isset( $availability['class'] ) && 'in-stock' === $availability['class'] ) {
return '';
}
return $html;
}
add_filter( 'woocommerce_stock_html', 'remove_in_stock_text_form_single_products', 10, 3 );
Accepted answer will not show the stock count, but stock status will appear if css isn't added. woocommerce_get_stock_html can be used to remove it completely.
Complete snippet for this:
add_filter( 'woocommerce_get_stock_html', function ( $html, $product ) {
return '';
}, 10, 2);
This is standard woocommerce functionality, it shows “out of stock” products on the shop page, in the woocommerce widgets, everywhere.
Only on the product single is “Out of stock” shown instead of an “Add to cart”.
Go to Woocommerce → Settings and click the Products tab
Click the Inventory link at the top
Check the Out Of Stock Visibility option to hide out of stock items