Disallow purchases from virtual products in WooCommerce - php

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/

Related

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.

Remove WooCommerce admin order action from order preview

I want to remove, for shop-managers, the ability to mark an order as completed. To do so, I used the following based on "Hide a specific action button conditionally in Woocommerce admin Orders list" answer in my theme's functions.php file:
add_filter( 'woocommerce_admin_order_actions', 'custom_admin_order_actions', 900, 2 );
function custom_admin_order_actions( $actions, $the_order ){
if(isset(wp_get_current_user()->roles[0]) && wp_get_current_user()->roles[0] == 'shop-manager')
unset($actions['complete']);
return $actions;
}
By this, I succesfully removed the complete button from the shop_order page. However, the shop-manager is still able to complete the order using the Complete button that appears in the order preview. To avoid this, I tried the next action after the previous one:
add_action( 'woocommerce_admin_order_preview_start', 'custom_display_order_data_in_admin' );
function custom_display_order_data_in_admin(){
// Call the stored value and display it
echo '<div>Class = "button hidden wc-action-button wc-action-button-complete complete"</div><br>';
}
However, this does not remove the button from the preview window because it does not substitute the line in the code.
Is there a way to remove this ability from the shop_order page and the order preview at once? If not, how can I hide this button from the preview window?
To remove the "complete" update order status button from admin order preview for "Shop manager" user role, use the following:
add_filter( 'woocommerce_admin_order_preview_actions', 'filter_admin_order_preview_actions', 10, 2 );
function filter_admin_order_preview_actions( $actions, $order ) {
if( current_user_can('shop-manager') && isset($actions['status']['actions']['complete']) ) {
unset($actions['status']['actions']['complete']);
}
return $actions;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Add a text under specific Woocommerce product category archive page

I would like to show text under Shop page main content. Different text for different categories.
I am aware of showing category description on the Shop page for each category but this is already in use.
Right now I am this far but it does not seem to work in the Child theme functions.php
if ( is_category( 'category1' ) ) {
function add_my_text() {
print '<p>This is my extra text.</p>';
}
add_action( 'woocommerce_after_main_content', 'add_my_text' );
}
Will be thankful if anyone knows how to improve this function
Thrilled I found this answer. Was struggling mightily to add content based on a custom taxonomy created with Custom Post Type UI. In case anyone else needs to know, you don't have to change permalinks or anything silly like that. Simple modification of LoicTheAztec's answer above does the trick.
add_action( 'woocommerce_product_meta_end', 'add_my_text' );
function add_my_text() {
if ( wc_get_product_class( $class = 'taxonomy-term' ) ) {
echo '<h2>IT WORKS!!!</h2>';
}
}
If you want to insert content into a different place on the page, choose a different hook from this very helpful WooCommerce Visual Hook Guide.
Hope this saves somebody else 9 hours :)
The Wordpress conditional function is_category() doesn't work with Woocommerce product categories which are a custom taxonomy. Instead use Woocommerce conditional tag is_product_category() inside your hooked function like:
add_action( 'woocommerce_after_main_content', 'add_my_text' );
function add_my_text() {
if ( is_product_category( 'category1' ) ) {
echo '<p>This is my extra text.</p>';
}
}
Code goes on function.php file of your active child theme (or active theme). It should works.

To remove the Add to Cart button from a specific product and replace a link instead

I searched about removing the Add to Cart button from a specific product and I used this code in functions.php.
add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);
function my_woocommerce_is_purchasable($is_purchasable, $product) {
return ($product->id == 305 ? false : $is_purchasable);
}
Now I want to replace a direct download link instead of the Add to Cart button.
I have free products and I want direct download link instead of the Add to Cart button.
EDITED:
I used this code but I saw direct download link in all single-product page. I want to see direct download link just in pages which have free product.
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_startsinfo', 11 );
function woocommerce_template_single_startsinfo() {
echo '<div class="starsinfo">
<p>direct download
</div>';
}
The add to cart button is really an HTML a element. You can change the href of this HTML a element by using the filter 'woocommerce_product_add_to_cart_url'.
add_filter( 'woocommerce_product_add_to_cart_url', function( $url, $product ) {
return $your_url;
}, 10, 2 );
Of course, you should use the $product which is a subclass of WC_Product to determine $your_url.

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

Categories