Hide WooCommerce Add To Cart button when price is zero - woocommerce

I need to hide my add to cart button on the condition where the price = 0.00. If the price is greater than 0.00 i would like the button to reappear.
However i also use the WooTheme Wish List extension and i need the add to wish list button to remain. I would also like to change the text from Free to "Subscription Product" when the value is 0.00.
This is only needed on the single product page as we do not show the add to cart anywhere else.
Currently if the price is set to 0.00 i get the text at the top of the single product page saying "free" and the add to cart button remains as does the add to wish list button. If i remove the price all together the buttons also disappear hence why i need to "hide" the button to retain the wish list one.
I have tried the following Code inserted into my functions.php file
<?php
/*
* 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';
}
?>
However, although it did work in that it changed the "Free" text to "POA" and also hid the "Add to Cart button" the "Add to Wishlist" button also disappeared.
If i changed the value to 0.01 everything appeared again so it nearly there... i just need the Wish List button to stay.
Any help or pointers would be greatly appreciated. Many Thanks...
Just for info, the reason i need to do this is because we run a subscription service, so products that are included in a subscription service do not need a price but do need to be shown so they can be added to a wish list. However sometimes we sell the products off so when they have a value i need the button to reappear so they can be added to the cart.
Simply having zero doesn't work because believe it or not people think they are free and try to add them to the cart to purchase !

Please add below code in your active theme functions.php and check it.
function wpcustom_is_purchasable( $purchasable, $product ){
if( $product->get_price() == 0 )
$purchasable = false;
return $purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'wpcustom_is_purchasable', 10, 2 );

Related

How to remove quantity from cart page in WooCommerce for downloadable items

Thanks in advance for anyone who can help with this. As I have many virtual/ downloadable items in my Woocommerce shopping page, is there a way to disable, or make the quantity field disappear, entirely but only for those types of items? I have had several occurrences where customers bought multiple of something that was digital, and since that didnt make much sense because they could download it multiple times, I wound up manually refunding the extra. To avoid this extra work, can qty get turned off if the downloadable checkbox is ticked on the product page, or something like that? And it just keeps the default qty of 1 during checkout.
Give this a try
function hide_downloadable_quantity_field_cart( $product_quantity, $cart_item_key, $cart_item ) {
if ( isset( $cart_item['data'] ) && $cart_item['data']->is_downloadable() ) {
$product_quantity = '';
}
return $product_quantity;
}
add_filter( 'woocommerce_cart_item_quantity', 'hide_downloadable_quantity_field_cart', 10, 3 );
It should remove the quantity field completely for downloadable items in the cart.

How do I add product attributes to the product page in WooCommerce?

I'm using WooCommerce and Elementor Pro. Is there any way to add specific product attributes (e.g. gross and net weight) below the Add to cart button?
It seems like an obvious thing but I haven't found options or snippets for it.
First you add attributes to product. Then you use this snippet in functions.php:
// maybe remove tab with attributes
add_filter( 'woocommerce_product_tabs', 'remove_info', 100, 1 );
function remove_info( $tabs ) {
unset($tabs['additional_information']);
return $tabs;
}
// show attributes
add_action( 'woocommerce_single_product_summary', 'summary_attributes', 35 );
function summary_attributes() {
global $product;
if ( $product->has_attributes() ) {
wc_display_product_attributes( $product );
}
}
Setting Up WooCommerce Attributes
go to Products > Attributes
Add the attribute name
Slug will automatically be created, and we can leave the rest of these options untouched.
Click “Add attribute” button and your attribute will be added.
Configure The Attributes
After creating attribute, now we can add different variation on your product. Click on the Configure Terms option.
Enter the variation
Add some short description
Click on the add new tab
To understand it in a better way you can follow this tutorial

How to add Stripe ApplePay to woocommerce minicart

I was able to hide Apple Pay button from single product page following way
// in functions.php
add_filter( 'wc_stripe_hide_payment_request_on_product_page', '__return_true' );
but now I want to add this button to minicart. Is there any hook or shortcode that allows placing Apple Pay button on minicart?
I am using this plugin https://wordpress.org/plugins/woocommerce-gateway-stripe/
add_action( 'woocommerce_widget_shopping_cart_buttons', 'mini_cart_stripe_button', 20 );
function mini_cart_stripe_button() {
if( wp_is_mobile() ){
//I'd like to add button here
}
}
Not sure if this helps but the snippet below makes the button appear on checkout.
add_filter( 'wc_stripe_show_payment_request_on_checkout', '__return_true' );
Also, here are some of the ApplyPay elements:
#wc-stripe-payment-request-wrapper
#wc-stripe-payment-request-button
#wc-stripe-payment-request-button-separator
Please let me know if you figure this one out. I would love to add this to the mini cart as well! Cheers.

Woocommerce product price duplicating

I want to add two prices on single product, first simple price was showing but when clicking on cariation size, price was duplicating, simple and variation both price showing, I want to show one price at a time.
Fix 2x price
GoTo: www.YOURDOMAIN/wp-admin/admin.php?page=wc-settings&tab=tax
then remove {price_excluding_tax} from "Price display suffix" input field
You Can try with jquery onchange function and get above product price and show at bottom
You can try to use this code. Hope it will work.
add_action( 'init', 'wp1232_remove_price_for_variations', 99 );
function wp1232_remove_price_for_variations() {
global $product;
if ( 'variable' == $product->get_type() ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
}
}

Woocommerce Add Product To Cart Multiple Times But As Different items

I have some custom code that adds an item to the cart, a variable product. If i add the same product twice it just increases the quantity. I would like it to add the product again as a different item in the cart.
How would i go about doing that? The item is being added to the cart using a link like domain.com?add-to-cart=3434&variation_id=4434 and so on.
The system i have developed is a product designer. So i may want to pick the same product but design it in different ways, and then add the same variation to the cart.
Is there also a way to send a unique identifier in the url to split these items up?
I want to do it by using add_to_cart but every time i do that with the variation attributes, the shipping ends up with a bug, it basically can't seem to find shipping methods:
$woocommerce->cart->add_to_cart(522,1, 523,array("attribute_colour" => "colour","attribute_size" => "a3", "attribute_invoice-numbering" => "yes", "attribute_quantity-column" => "yes", "attribute_cc-type" => "duplicate"));
While that code adds the item to the cart, it causes the following:
Warning: Invalid argument supplied for foreach() in /home/****/public_html/wp-content/plugins/woocommerce/includes/class-wc-shipping.php on line 291
There doesn‘t seem to be any available shipping methods. Please double check your address, or contact us if you need any help.
It doesn't make any sense, because the standard add to cart code in woocommerce uses add_to_cart and has none of these issues. The same if i use a URL, it works fine!
Try this !
/*
* #desc Force individual cart item
*/
function force_individual_cart_items( $cart_item_data, $product_id ){
$unique_cart_item_key = md5( microtime().rand() );
$cart_item_data['unique_key'] = $unique_cart_item_key;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data','force_individual_cart_items', 10, 2 );
/*
* #desc Remove quantity selector in all product type
*/
function remove_all_quantity_fields( $return, $product ) {
return true;
}
add_filter( 'woocommerce_is_sold_individually', 'remove_all_quantity_fields', 10, 2 );
I can't tell yet if the shipping array issue is related. But, if you take a look at the beginning of the add_to_cart() method in the cart class:
// Generate a ID based on product ID, variation ID, variation data, and other cart item data
$cart_id = $this->generate_cart_id( $product_id, $variation_id, $variation, $cart_item_data );
// See if this product and its options is already in the cart
$cart_item_key = $this->find_product_in_cart( $cart_id );
Basically, what we're seeing here is that the item must be completely unique in order to add it to the cart again. Otherwise, it will just up the quantity of the item already in the cart. So, to get around this, you will want to make the add_to_cart() parameters unique... probably via the final, $cart_item_data array.

Resources