Home > Software design >  How to add next available date in stock WooCommerce
How to add next available date in stock WooCommerce

Time:05-17

I have this code that's suppose to add a next available date in stock when out of stock. I added the field using custom fields.

add_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2);
function filter_product_availability_text( $availability, $product ) {
    $date_of_availability = get_post_meta( get_the_ID(), 'date_of_availability', true );

    if ( ! $product->is_in_stock() && ! empty($date_of_availability) ) {
        $availability .= '<span style="color:#e2401c;"><strong>- (' . __('Available from:', 'flatsome') . ' </strong>' . get_post_meta( get_the_ID(), 'date_of_availability', true ) . '!important' . ')</span>';
    }
    return $availability;
}

custom fields

Could someone help me ?

The end goal is to have a field in the product with a date, when out of stock is reached it needs to print that line out with the field value If I however place it like this:

add_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2);
function filter_product_availability_text( $availability, $product ) {
    $date_of_availability = get_post_meta( get_the_ID(), 'date_of_availability', true );

    if ( ! $product->is_in_stock() && ! empty($date_of_availability) ) {
        $availability .= '<span style="color:#e2401c;"><strong>- (' . __('Available from:', 'flatsome') . ' </strong>' . get_post_meta( get_the_ID(), 'date_of_availability', true ) . ')</span>';
echo $availability
    }
   // return $availability;
}

Then it works however for some reason it repeats, like this: repeating

Also if enabled it leaves an In stock label on every product despite being disabled

CodePudding user response:

The reason your function appears to output twice is that you are echoing the $availability value without modifying or ceasing return output of woocommerce_get_availability text.

The following should work, as it works on Storefront theme:

function filter_product_availability_text( $availability, $product ) {

  $date_of_availability = $product->get_meta( 'date_of_availability' );

  if ( ! $product->is_in_stock() && $date_of_availability ) {
    $availability .= '- Available from: ';
    $availability .= $date_of_availability ;
  }

  return $availability;
}
add_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2);

However, if the above fails, you can output with an action hook. In your case, the filter may be failing because of something Bacola developers chose to do with their code. This should output regardless:

function filter_product_availability_text() {
  global $product;

  $availability = '';
  $date_of_availability = $product->get_meta( 'date_of_availability' );

  if ( ! $product->is_in_stock() && $date_of_availability ) {
    $availability = '<div ><p>Available from: '. $date_of_availability .'</p></div>';
  }

  echo $availability;
}
add_action( 'woocommerce_single_product_summary', 'filter_product_availability_text' );

Add to your CSS file:

.product-meta p {
  color: #F00;
}

CodePudding user response:

You need to comment:

echo $availability

Code snippets:

add_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2);
function filter_product_availability_text( $availability, $product ) {
    $date_of_availability = '17-05-2022'; // Your custom data

    if ( ! $product->is_in_stock() && ! empty($date_of_availability) ) {
        $availability .= '<span style="color:#e2401c;"><strong>- (' . __('Available from:', 'flatsome') . ' </strong>' . $date_of_availability . ')</span>';
        //echo $availability
    }
    return $availability;
}

enter image description here

Alternatively, there’s also a built-in setting for this under WooCommerce > Settings > Products > Inventory — you can set when it should be considered “low stock”, and then display a message:

enter image description here

You could change the wording of the message by using a free translation plugin like Loco Translate ( https://wordpress.org/plugins/loco-translate/ ).

  • Related