Show selected out of stock products In Woocommerce - php

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:

Related

Move selected variation price below product Add-ons form in Woocommerce

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.

Disable single product page for specific products in WooCommerce

How would we go about disabling the single product page for specific products?
For example, we have some products that have product variations. In this case we are using the single product page. But for the products that do not have variations we simply use an add to cart link on the landing pages and skip the single product page that just adds an extra step for the customer.
I found this post which outlines how to disable ALL single product pages. But I would like to target the pages that are disabled. Either by product number or perhaps product listing type ie. variable, non variable.
What is the best way to go about doing this, without breaking WooCommerce or causing SEO issues?
To clarify: by disabled I mean remove the link to the page from areas like the shopping cart etc.
In the following functions, you will have to define one or many product IDs inside the code.
This first hooked function will remove the product from product catalog:
add_filter( 'woocommerce_product_is_visible', 'filter_product_is_visible', 20, 2 );
function filter_product_is_visible( $is_visible, $product_id ){
// HERE define your products IDs (or variation IDs) to be set as not visible in the array
$targeted_ids = array(37, 43, 51);
if( in_array( $product_id, $targeted_ids ) )
$is_visible = false;
return $is_visible;
}
To remove the link from cart items in cart page, you can use the following
add_filter( 'woocommerce_cart_item_name', 'filter_cart_item_name', 20, 3 );
function filter_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
// HERE define your products IDs (or variation IDs) to be set as not visible in the array
$targeted_ids = array(37, 43, 51);
if( in_array( $cart_item['data']->get_id(), $targeted_ids ) && is_cart() )
return $cart_item['data']->get_name();
return $product_name;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Is also possible to redirect the target products pages to the main shop.
You can add if statement around return false. You can either check for product (page) id or I would add tag (or category or custom field) to these and then in if statement you can check for this tag and if its there you return false;

Woocommerce - Disable Add to Cart Conditionally on Custom Field

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.

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

trigger the update woocommerce product action by code

In my site registered users can upload downloadable products in frontend by WPUF,and they can select if the products are free, selecting the value from a dropdown list
I've added this snippet of code in functions.php:
//function to update product status to 'publish'**
// and set the price to 0 for 'free' products**
add_action('wpuf_add_post_after_insert', 'change_post_status');
function change_post_status($post_id){
$project_type = get_post_meta($post_id, 'project_type', true);
if ($project_type == 'Free')
{
update_post_meta( $post_id, '_regular_price', 0);
$current_post = get_post( $post_id, 'ARRAY_A' );
$current_post['post_status'] = 'publish';
wp_update_post($current_post);
}
}
it seems to work, I can see the product as live after submission, ready to be downloaded , BUT I can't see the products in the woocommerce shop page, and the price (in this case "Free") isn't set. In the product page appears an additional tab "Additional information" that displays the product_cat.
The only thing that I've noticed in the database is that the metakey product_count_product_cat of wp_woocommerce_termmeta, isn't correctly updated.
To view the product correctly, in the woocommerce page, and in its page, with the correct price (in this case "Free") I need to enter as admin in Products, /edit and click the "Update" button. This solves everything.
Anyone know what function calls the Publish/Update woocommerce Products button?
thank you

Categories