Hide WooCommerce sale prices for logged-out user - php

I'm using the WooCommerce product table plugin by Barn2, this plugin allows you to add custom columns so I added a custom column to display "SALE PRICE" in a separate column using _sale_price custom field. The sale price gets displayed and I want to hide that sale price column data for non-logged-in customers, although regular prices are hidden for non-logged-in customers using the Wholesale pro plugin(By Barn2) Now, I'm struggling to find a solution to hide the sale price column for non-logged-in customers. I attempted this code:
add_filter( 'woocommerce_product_get_sale_price', function( $price ) {
if ( ! is_user_logged_in() ) {
return '';
}
return $price; // Return original price
} );

Related

Display product stock status on single product pages in WooCommerce when price field is empty

I'm adding a new stock status in WooCommerce products. It has been done by the help of How to add custom stock status to products in WooCommerce 4+ answer code.
But the problem is that when the price field of products is empty it doesn't show the status (front-end). but otherwise when the product has the price, it shows the status.
What can I do, to make the status be shown even if the price field is empty?
You will see that if the price is empty, besides not showing the product stock status, the price (the HTML code) for this will also not be displayed. Because the product is considered as not purchasable if the price is empty.
So in order to display the product stock status, you should make the product purchasable, if the price is empty. However, I do not believe this is your intention?
So instead of focusing on displaying the product stock status, it will be much simpler to display something else (HTML/text) where the price is normally displayed,
and that can be done by using the woocommerce_empty_price_html filter hook
So either you drop the whole idea of ​​the product stock status and simply use:
function filter_woocommerce_empty_price_html( $html, $product ) {
// NOT true on a single product page, RETURN
if ( ! is_product() ) return $html;
// Add HTML
$html = '<p>My text</p>';
return $html;
}
add_filter( 'woocommerce_empty_price_html', 'filter_woocommerce_empty_price_html', 10, 2 );
Or you build further on the product stock status and on that basis you get:
function filter_woocommerce_empty_price_html( $html, $product ) {
// NOT true on a single product page, RETURN
if ( ! is_product() ) return $html;
// Get stock status
$product_stock_status = $product->get_stock_status();
// Compare
if ( $product_stock_status == 'MY CUSTOM STATUS' ) {
// Add HTML
$html = '<p>My text based on product stock status</p>';
}
return $html;
}
add_filter( 'woocommerce_empty_price_html', 'filter_woocommerce_empty_price_html', 10, 2 );
The only downside to this solution(s) is that your new HTML/text will not be displayed where you normally see the product stock status, but where the price is normally displayed.

Change order item displayed meta key label in WooCommerce admin order pages

Following Display a product custom field only in WooCommerce Admin single orders answer to my previous question, which:
Adds a Custom SKU Field (ArticleID)
Saves the Custom SKU (ArticleID) as Hidden Order Item Meta Data
Saves the Custom SKU (ArticleID) as Hidden Order Item Meta Data for Manual Orders
How can I change the displayed meta key label _articleid on order line items section of the admin single order pages?
Right now it shows the "SKU", the "Variation ID" (for product variations) and the "_articleid".
I'd like to replace displayed "_articleid" with "Article ID" instead.
Any help?
You will use the following to replace displayed key label _articleid with for example 'Article ID' in order items on WooCommerce admin single orders:
add_filter('woocommerce_order_item_display_meta_key', 'filter_wc_order_item_display_meta_key', 20, 3 );
function filter_wc_order_item_display_meta_key( $display_key, $meta, $item ) {
// Change displayed label for specific order item meta key
if( is_admin() && $item->get_type() === 'line_item' && $meta->key === '_articleid' ) {
$display_key = __("Article ID", "woocommerce" );
}
return $display_key;
}
Code goes in functions.php file of the active child theme (or active theme). It should works.
Related:
Change order item custom meta data displayed label and value in WooCommerce Admin orders
Related to this thread:
How to show a product custom field (custom SKU) in WooCommerce orders
Display a product custom field only in WooCommerce Admin single orders for Manual Orders

Add specific product to cart if coupon is applied WooCommerce

I want to build the following:
A landingspage with a Apply Coupon field.
If the coupon is valid it needs to add a specific product to the cart.
So basically the coupon needs to be attached to a specific product. (in this case product_id 99.
I found this somewhere:
function wc_ninja_apply_coupon( $coupon_code ) {
if ( 'JLSDFO' === $coupon_code ) {
$product_id = 99;
WC()->cart->add_to_cart( $product_id );
}
}
add_action( 'woocommerce_applied_coupon', 'wc_ninja_apply_coupon' );
But this is for one coupon specific. I want to add for example 1000 coupons which are merged with this product. The customer can select this product on the back-end while adding this coupons. So when the user enters one of the 1000 coupons it understands to add for example product_id 99 to the cart.

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:

Woocommerce add free products with ACF Post Object field

I am trying to build a custom functionality in Woocommerce with Advanced Custom Fields (ACF) plugin.
I created some code already but it is not working properly.
I want to select (simple) products at a Woocommerce product with ACF post object field. That products must be added free into the Woocommerce cart when that product will be added to the cart. Also when I add two or more items the free products must be added together.
I got also some images how the situation should work.
This is the ACF field setup.
For example: this is the product we give away.
This is the product we give away selected at a Woocommerce single product.
This is my actual code:
add_action('woocommerce_check_cart_items', 'free_products' );
function free_products() {
if( ( is_cart() || is_checkout () ) {
$free_product = get_field('gratis_producten'); // Incentive product we are giving away
$cart_id = WC()->cart->generate_cart_id( $free_product );
$free_products_in_cart = WC()->cart->find_product_in_cart( $cart_id );
if( $free_product ) {
// Removing existing "free products" from the cart.
WC()->cart->remove_cart_item( $free_products_in_cart );
// Adding to cart 40 free products
WC()->cart->add_to_cart( $free_product, 40 );
}
}
}
Any help will be highly appreciated.

Categories