Show Woocommerce product pages default description for empty descriptions - php

Is it possible to add Woocommerce Product Default Description for all new added product pages in the backend product description area as a default text for example ( free of charge ) or a default function which will generate a certain action but only for the products who doesn't have a product description value
All that is needed is to get the description have a default added value for the new added products in the description below
Can anyone help in this ?
I found this which do the magic but for the product short description, but i want it to the product description itself
add_action( 'woocommerce_single_product_summary', 'bbloomer_echo_short_desc_if_empty', 21 );
function bbloomer_echo_short_desc_if_empty() {
global $post;
if ( empty ( $post->post_excerpt ) ) {
$post_excerpt = '<p class="default-short-desc">';
$post_excerpt .= 'This is the default, global, short description.<br>It will show if <b>no short description has been entered!</b>';
$post_excerpt .= '</p>';
echo $post_excerpt;
}
}

function woocommerce_default_description($content) {
$empty = empty($content) || trim($content) === '';
if(is_product() && $empty) {
$content = 'default text content';
}
return $content;
}
add_filter( 'the_content', 'woocommerce_default_description' );
function rmg_woocommerce_default_product_tabs($tabs) {
if (empty($tabs['description'])) {
$tabs['description'] = array(
'title' => __( 'Description', 'woocommerce' ),
'priority' => 10,
'callback' => 'woocommerce_product_description_tab',
);
}
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'rmg_woocommerce_default_product_tabs' );
something like above should work.

Try this:
add_action( 'woocommerce_single_product_summary', 'bbloomer_echo_desc_if_empty', 21 );
function bbloomer_echo_desc_if_empty() {
global $post;
if ( empty ( $post->post_content ) ) {
$post_description = '<p class="default-content">';
$post_description .= 'This is the default, global, description.<br>It will show if <b>description has been entered!</b>';
$post_description .= '</p>';
echo $post_description;
}
}

Related

Woocommerce add_filter vs. add_action when asking about specific product meta

I'm struggling with a basic php function.
I implemented a backend checkbox in wordpress / woocommerce general tab which works fine:
// Add checkbox in Backend
function action_woocommerce_product_options_general_product_data() {
// Checkbox
woocommerce_wp_checkbox( array(
'id' => '_prevent_add_to_cart_button', // Required, it's the meta_key for storing the value (is checked or not)
'label' => __( 'Warenkorb Button', 'woocommerce' ), // Text in the editor label
'desc_tip' => false, // true or false, show description directly or as tooltip
'description' => __( 'Warenkorb Button ausblenden', 'woocommerce' ) // Provide something useful here
) );
}
add_action( 'woocommerce_product_options_general_product_data', 'action_woocommerce_product_options_general_product_data', 10, 0 );
// Save Field
function action_woocommerce_admin_process_product_object( $product ) {
// Isset, yes or no
$checkbox = isset( $_POST['_prevent_add_to_cart_button'] ) ? 'yes' : 'no';
// Update meta
$product->update_meta_data( '_prevent_add_to_cart_button', $checkbox );
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );
Now i want to ask for the value of that checkbox and do things based on it.
It works in this function as expected:
// Add function for category / listing page add to cart button hook
function hide_listingpage_button( $add_to_cart_html, $product, $args ){
$hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' );
if ( $hide_add_to_cart_button == 'yes' ) {
$before = '<span style="display:none;">';
$after = '</span>';
return $before . $add_to_cart_html . $after;
}
else {
return $add_to_cart_html;
}
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'hide_listingpage_button', 10, 3 );
but it does not work in this function(s):
// Add functions for singlepage before / after add to cart button hook
function beforehide_singlepage_button( $product ) {
$hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' );
if ( $hide_add_to_cart_button == 'yes' ) {
echo 'test after';
}
}
function afterhide_singlepage_button( $product ) {
$hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' );
if ( $hide_add_to_cart_button == 'yes' ) {
echo 'test after';
}
}
add_action( 'woocommerce_before_add_to_cart_button', 'beforehide_singlepage_button' );
add_action( 'woocommerce_after_add_to_cart_button', 'afterhide_singlepage_button' );
Im my opinion im doing basically the same things?
I give the $product object as argument in the function, ask then for yes / no boolean with
$hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' );
and then echo something with the if clause.
however, i get no output of the page from that line, and also no error message.
What am i missing?
thank you!

WooCommerce: Change add to cart button text for multiple products with a redirection to checkout

In WooCommerce, I need to change some things for two products:
Add to cart button (different for both products)
Skip cart to checkout (same for two products)
I've found this code, that works. But just for one product id:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_cart_button_text' );
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_cart_button_text' );
function custom_cart_button_text($text) {
global $woocommerce;
global $post;
$post_id = $post->ID;
if($post_id == 11359){
$text = __( 'Ja ik word kompaan', 'woocommerce' );
}
return $text;
}
add_filter ('woocommerce_add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout($url) {
global $woocommerce;
if ( isset( $_POST['add-to-cart'] ) ) {
$product_id = (int)$_POST['add-to-cart'];
if($product_id == 11359){
$url = $woocommerce->cart->get_checkout_url();
}
}
return $url;
}
How would I make the first part of the snippet (renaming the add to cart text) so that I can use it two times; every product id needs a different add to cart text.
And how can I add a second product id to the second part of the snippet (so it skips the cart for both product id's)
Thanks!
You can use the following for 2 different products button texts and a redirection to checkout for both (where you will define your 2 product ids):
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_addtocart_button_text', 10, 2 );
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_addtocart_button_text', 10, 2 );
function custom_addtocart_button_text( $button_text, $product ) {
// 1st product
if ( $product->get_id() == 11359 ) {
$button_text = __( 'Ja ik word kompaan', 'woocommerce' );
}
// 2nd product
elseif ( $product->get_id() == 11362 ) {
$button_text = __( 'Something else', 'woocommerce' );
}
return $button_text;
}
add_filter ( 'woocommerce_add_to_cart_redirect', 'custom_redirect_to_checkout' );
function custom_redirect_to_checkout( $url ) {
if ( isset( $_POST['add-to-cart'] ) && $_POST['add-to-cart'] > 0 ) {
$product_id = intval( $_POST['add-to-cart'] );
if( in_array( $product_id, array( 11359, 11362 ) ) ){
$url = wc_get_checkout_url();
}
}
return $url;
}
Code goes in functions.php file of the active child theme (or active theme). It should works.

Hide all products with a specific stock status from WooCommerce catalog

I'm using Flatsome template on Wordpress and Woocommerce site. I also create custom stock status (example noproduzione, used when a product is not created anymore from manufacturer). But I don't want to show this products (noproduzione stock status) on my site (only in admin pages).
I'm using this code that I write here (I put it in my functions.php file), but it seems that on flatsome does not works. How to apply it on this template?
add_action( 'woocommerce_product_query', 'qc_action_product_query', 10, 2 );
function qc_action_product_query( $q, $query ) {
// Get any existing meta query
$meta_query = $q->get( 'meta_query');
// Define an additional meta query
$q->set( 'meta_query', array( array(
'key' => '_stock_status',
'value' => 'noproduzione',
'compare' => 'NOT LIKE',
) ) );
// Set the new merged meta query
$q->set( 'meta_query', $meta_query );
}
To create custom code I used some of codes found on StackOverflow
// Add new stock status options
add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );
function filter_woocommerce_product_stock_status_options( $status ) {
// Add new statuses
$status['10days'] = __( 'Disponibile entro 10 giorni', 'woocommerce' );
$status['inarrivo'] = __( 'In arrivo', 'woocommerce' );
$status['noproduzione'] = __( 'Fuori produzione', 'woocommerce' );
return $status;
}
// Availability text
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );
function filter_woocommerce_get_availability_text( $availability, $product) {
switch( $product->get_stock_status() ) {
case '10days':
$availability = __( 'Disponibile entro 10 giorni', 'woocommerce' );
break;
case 'inarrivo':
$availability = __( 'In arrivo', 'woocommerce' );
break;
case 'noproduzione':
$availability = __( 'Fuori produzione', 'woocommerce' );
break;
}
return $availability;
}
I added code to show the text of availability, and also to hide the cart button if the product is noproduzione stock status
// Display the stock status on other page
add_action( 'woocommerce_after_shop_loop_item_title', 'wcs_stock_text_shop_page', 25 );
function wcs_stock_text_shop_page() {
//returns an array with 2 items availability and class for CSS
global $product;
$availability = $product->get_stock_status();
//check if availability in the array = string 'noproduzione'
//if so display on page.//if you want to display the 'in stock' messages as well just leave out this, == 'Out of stock'
if ( $product->get_stock_status() === 'noproduzione') {
echo '<span style="color:#b20000;">Fuori produzione!</span>';
}
else if ( $product->get_stock_status() === 'onbackorder') {
echo '<span style="color:#13b200;">Disponibile su ordinazione</span>';
}
else if ( $product->get_stock_status() === '10days') {
echo '<span style="color:#13b200;">Disponibile in 10 giorni</span>';
}
else if ( $product->get_stock_status() === 'inarrivo') {
echo '<span style="color:#e0c81d;">In arrivo</span>';
}
else if ( $product->get_stock_status() === 'outofstock') {
echo '<span style="color:#b20000;">Terminato!</span>';
}
else {
echo '<span style="color:#53af00;">Disponibile!</span>';
}
}
// Hide the cart button if stock status is 'noproduzione'
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
add_filter('woocommerce_variation_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
function filter_is_purchasable_callback( $purchasable, $product ) {
if ( $product->get_stock_status() === 'noproduzione' ) {
return false;
}
return $purchasable;
}
Last, I also added this code, is useful because order the RELATED PRODUCT by "instock" status, and for me is good because do not show the other custom stock status. But I want to apply this to all of my frontend site.
//show, in the related products, only instock products!
add_filter( 'woocommerce_related_products', 'filter_woocommerce_related_products', 10, 3 );
function filter_woocommerce_related_products( $related_posts, $product_id, $args ) {
foreach( $related_posts as $key => $related_post ) {
// Get product
$related_product = wc_get_product( $related_post );
// Is a WC product
if ( is_a( $related_product, 'WC_Product' ) ) {
// Stock status
$stock_status = $related_product->get_stock_status();
// NOT instock
if ( $stock_status != 'instock' ) {
unset( $related_posts[$key] );
}
}
}
return $related_posts;
}
So, the question is: how to hide this products (noproduzione stock status) on my site (and show only in admin pages)? Noproduzione is different from OUTOFSTOCK!
You should better use dedicated woocommerce_product_query_meta_query filter hook as follows, to hide, from your store, all products that have a specific stock status (works with custom stock status):
add_action( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 1000 );
function custom_product_query_meta_query( $meta_query ) {
if ( ! is_admin() ) {
$meta_query[] = array(
'key' => '_stock_status',
'value' => 'noproduzione',
'compare' => '!=',
);
}
return $meta_query;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works on all versions since WooCommerce 3.

Add Text After the Price in WooCommerce Product Per Product ID/WP

I have the following code in my child theme's functions.php file, and it works great.
function themeprefix_custom_price_message( $price ) {
global $post;
$product_id = $post->ID;
$my_product_array = array( 270,373,378,506,1306,1311,1312,1444,1445,1447,1449,1930,1932,1933,1934,1935,1963,4146,4152,4153,4154 );//add in product IDs
if ( in_array( $product_id, $my_product_array )) {
$textafter = ' Each/Min 12'; //add your text
return $price . '<span class="price-description">' . $textafter . '</span>';
}
else
{
return $price;
}
}
add_filter( 'woocommerce_get_price_html', 'themeprefix_custom_price_message' );
However, now I need to add another group of products and enable different text after the price, i.e., Each/Min 8. I have tried various changes to the above and have brought the site down each time. How do I adjust the above to add another (and then possibly another) group of product IDs with different text after the price? Thanks in advance!
This answer is for the question which was asked here: Add custom text after the price in WooCommerce
However, it can also be used in the above question.
You can try something like that if you want dynamic text after every product.
/* add custom field in single product */
add_action('woocommerce_product_options_general_product_data', function() {
woocommerce_wp_text_input([
'id' => '_custom_text_after_price',
'label' => __('Custom Text after Price', 'themedomain'),
]);
});
You can see the above changes in your product.
Now add the below code to save meta in your product
/* save meta field */
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields_save($post_id)
{
$woocommerce_custom_product_text_field = $_POST['_custom_text_after_price'];
if (!empty($woocommerce_custom_product_text_field))
update_post_meta($post_id, '_custom_text_after_price', esc_attr($woocommerce_custom_product_text_field));
}
And now to display custom code on the front end you need to use 2 hooks.
/* show custom text on front end */
function cw_change_product_price_display( $price ) {
global $post, $product;
if(get_post_meta( $post->ID, '_custom_text_after_price', true )){
$text = get_post_meta( $post->ID, '_custom_text_after_price', true );
}
$price .= ' '.$text;
return $price;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
In the front end, it will look like this.
You could leave the existing array as it is. Simplify the code by removing the else from the if and then start declaring & checking new product arrays before returning the default price-only text...
function themeprefix_custom_price_message( $price ) {
global $post;
$product_id = $post->ID;
$my_product_array = array( 270,373,378,506,1306,1311,1312,1444,1445,1447,1449,1930,1932,1933,1934,1935,1963,4146,4152,4153,4154 );//add in product IDs
if ( in_array( $product_id, $my_product_array )) {
$textafter = ' Each/Min 12'; //add your text
return $price . '<span class="price-description">' . $textafter . '</span>';
}
$my_product_array2 = array( 9990,9991,9992 );
if ( in_array( $product_id, $my_product_array2 )) {
$textafter = ' Each/Min 8'; //add your text
return $price . '<span class="price-description">' . $textafter . '</span>';
}
$my_product_array3 = array( 9993,9994,9995 );
if ( in_array( $product_id, $my_product_array3 )) {
$textafter = ' Each/Min 4'; //add your text
return $price . '<span class="price-description">' . $textafter . '</span>';
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'themeprefix_custom_price_message' );

Woocommerce Conditional Product Tabs

Okay, I'm trying to show certain tab content depending on what category a product is listed in.
I have created a custom "Size Chart" tab and want to show for example:
Footwear size chart when product is in "Footwear" category
Clothing size chart when product is in "Clothing" category
No size chart when product is in "Accessories" category.
I've created a custom tab like this:
add_filter( 'woocommerce_product_tabs', 'woo_rename_tabs', 98 );
function woo_rename_tabs( $tabs ) {
$tabs['new_tab'] = array(
'title' => __( 'Size Chart', 'woocommerce' ),
'priority' => 55,
'callback' => 'woo_tab_content'
);
return $tabs;
}
Then added the custom tab content like this:
function woo_tab_content() {
echo 'This is the size chart';
}
Does anyone know how to add a conditional statement to this code to allow me to specify what content is displayed depending on what category a product is placed in i.e.
is_product_category( 'footwear' )
Thanks!
in you woo_tab_content function simple add what you have done above so it would like this this:
function woo_tab_content() {
if (is_product_category( 'footwear' )){
echo 'This is the size chart';
} else {
//do something else
}
}
Okay, I managed to fix it using the following code:
function woo_tab_content() {
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( 'clothing', $categories ) ) {
echo 'this is the clothing tab content!';
} elseif ( in_array( 'shoes', $categories ) ) {
echo 'this is the shoes tab content!';
} else {
echo 'all other tabs';
}
}
It turns out that is_product_category does not work for the single product pages. Only for the category page itself.

Categories