如何在库存WooCommerce中添加下一个可用日期

发布于 2025-01-29 12:53:59 字数 1657 浏览 0 评论 0原文

我有这个代码,该代码应该在剩余的库存时添加下一个可用日期。我使用自定义字段添加了字段。

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;
}

自定义字段

有人可以帮助我吗?

最终目标是在产品中拥有一个字段,当达到库存时,需要用现场值打印该行 但是,如果我这样将其放置:

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;
}

那么它起作用,但由于某种原因它重复出现,因此: 重复

,如果启用了,尽管已禁用了每个产品的库存标签,但仍将

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

[浮城] 2025-02-05 12:54:00

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.

以下内容应起作用,因为它在店面主题上起作用:

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);

但是,如果上述失败,则可以使用动作钩输出。就您而言,由于Bacola开发人员选择使用其代码,因此过滤器可能会失败。这应该输出不管:

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 class="product-meta date-of-avail"><p>Available from: '. $date_of_availability .'</p></div>';
  }

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

添加到您的CSS文件:

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

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 class="product-meta date-of-avail"><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;
}
骷髅 2025-02-05 12:54:00

您需要评论:

echo $availability

代码段:

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;
}

在此处输入图像描述

另外,还有一个内置的Inn-Inin在WooCommerce&gt;设置&gt;产品&GT;库存 - 您可以在应视为“低库存”时设置,然后显示一条消息:

输入图像描述这里

您可以使用免费的翻译插件(例如Loco Translate)更改消息的措辞( https://wordpress.org/plugins/loco-translate/ )。

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/ ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文