Woocommerce - Disable Add to Cart Conditionally on Custom Field - php

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.

Related

Woocommerce - Change Add to Cart Button text depending on some conditions

I need help from an experienced programmer from here... I have a site that sells Montessori PDF's, Courses, a blog, and more - made from scratch by me. Can be checked at: https://insideoutmontessori.ro/
My issue is Add to Cart Button - I want to show different text depends on conditions - and is not :) Let me explain...
I use a snippet to modify Add to cart Button Text (CUMPARA - means Buy in english) - this is already done by the following code:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text' );
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text' );
// To change add to cart text on single product page
function woocommerce_custom_single_add_to_cart_text() {
if ( ! is_admin()) {
return __( 'Cumpără', 'woocommerce' );
}
}
// To change add to cart text on product archives(Collection) page
function woocommerce_custom_product_add_to_cart_text() {
if ( ! is_admin()) {
return __( 'Cumpără', 'woocommerce' );
}
}
I use to put a product without price, to announce next PDF - is like a teaser. Now is not active because o don't work for the moment to something new. For this i manage to alter price to be "In lucru" - means Work in progress in English - but i do not manage to change the button text from CUMPARA to VEZI DETALII (means MORE DETAILS in English). This product is not purchasable and I don't manage to use this condition to change the button text...
Also - for products already added to cart i want to change the Add to cart Button text to ADAUGAT IN COS - means Added to Cart in English - for the moment when the user came back to all products page and browse thru them. I manage before to do this (code below works but...) but i receive some errors (Critical ones) and after change the code i receive only something with JSON message in a red box, in Editor, when i try to save a page witch contains woocommerce shortcodes... I deactivate this snippet to lose the error.
The code is below:
add_filter('woocommerce_product_add_to_cart_text', 'wc_product_add_to_cart_text', 10, 2 );
add_filter('woocommerce_product_single_add_to_cart_text', 'wc_product_add_to_cart_text', 10, 2 );
function wc_product_add_to_cart_text( $text, $product ){
if ( ! is_admin()) {
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart ) {
$text = "Adăugat în coș";
}
return $text;
}
}
So - the bottom line is that I need a snippet that changes the Add to Cart Button text depending on these three conditions, simultaneously:
if the product is in the cart - the button will show: Adaugat in cos (Already in the cart)
if the product is without a price - the button will show: Vezi detalii (See more details)
otherwise - button will show: Cumpara (Buy)
I think this is the correct approach - i mean this needs to be done with one function that alters button text depends on that three conditions.
Thanks!

Conditional Add To Cart Button On Woocommerce Product Archive

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.

Show selected out of stock products In Woocommerce

I'm trying to find a solution to a client demand, without success. She's asking me how to show a selected number of out of stock products on her online store. By default, the Woocommerce setting is set to "hide out of stock products", but she wants to select some of her products and show them (even with 0 stock, because she wants to tell their customers that those few products will be available soon -there is a text for this-).
We have tried with a very simple snippet using the hook woocommerce_product_is_visible that we thought it would work, but there is something that we are missing...
This is de code:
// [WooCommerce] Show some out of stock products even the hide option is active
add_filter( 'woocommerce_product_is_visible', 'keep_showing_specific_out_of_stock_product_list', 10, 2 );
function keep_showing_specific_out_of_stock_product_list( $visible, $product_ID ){
$product_list = array( 18013, 18050 ); // Insert the products IDs that want to show
return in_array( $product_ID, $product_list )? true : $visible;
}
Any help is appreciated.
Why you don't simply use a Woocommerce shortcode like:
1) In the Wordpress text editor of a page or a post (or in a widget):
[products ids="18013,18050"]
2) In any PHP code file:
echo do_shortcode( "[products ids='18013,18050']" );
The products out of stock are displayed just like in this real example:

How can I remove the product/inventory count from the shop page on Woocommerce?

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

How can I replace the “Add to cart” button with an “Call to order” button and stop working on some product?

I am using woocommerce plugin in a wordpress site to sell some products. There are two types of products. some products will have add to cart button, but some will have call to order button instead of add to cart. But Both type of products will show the price and discounted price.
I am using some code to change the text
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );
function woo_custom_cart_button_text() {
return __( 'Call to order', 'woocommerce' );
}
that works. But I when I am using this code to change the url or say stop the button to add to cart that is not working.
add_filter( 'woocommerce_product_add_to_cart_url', 'woo_more_info_link' );
function woo_more_info_link( $link ) {
global $product; // switches link in all cases, i.e. in plugins
$link = get_permalink( $product->id );
return $link;
}
I actually want that the add to cart button should not working and should say only call to order.
Can anybody tell me how to stop add to cart from working.

Categories