【问题标题】:Custom "Out of stock" text based on product category in WooCommerce基于 WooCommerce 中产品类别的自定义“缺货”文本
【发布时间】:2020-12-07 16:46:34
【问题描述】:

每个缺货的产品都说“缺货”

有很多 functions.php 脚本会覆盖文本,但我只是想覆盖特定于“A 类”的文本,或者如果我知道“id”类别的编号,这也可以。

我找到了这个脚本,但它只允许您修改每个产品 ID 的 txt。

add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 10, 2);       
function wcs_custom_get_availability( $availability, $_product ) { 
    // custom 
    if ( $_product->is_in_stock() && $_product->get_id() == '6498' ) {
        $availability['availability'] = sprintf( __('✔️ Available but low in stock | 30-day No Questions Asked Money-Back Guarantee Applies', 'woocommerce'), $_product->get_stock_quantity());
    }

    // Out of stock
    if ( ! $_product->is_in_stock() ) {
        $availability['availability'] = __('Sorry, All sold out!', 'woocommerce');
    }

    return $availability;
}

如何根据类别进一步调整此脚本?

【问题讨论】:

标签: php wordpress woocommerce product categories


【解决方案1】:

要检查产品类别,您可以使用has_term()

has_term( string|int|array $term = '', string $taxonomy = '', int|WP_Post $post = null )

检查当前帖子是否有任何给定的条款。


所以你得到:

function filter_woocommerce_get_availability( $availability, $product ) {
    // Specific categories
    $specific_categories = array( 'Categorie-A', 'categorie-1' );
    
    // Out of stock and has certain category     
    if ( ! $product->is_in_stock() && has_term( $specific_categories, 'product_cat', $product->get_id() ) ) {
        $availability['availability'] = __('My custom text', 'woocommerce' );
    }

    return $availability;
}
add_filter( 'woocommerce_get_availability', 'filter_woocommerce_get_availability', 10, 2 );

【讨论】:

  • 太棒了!它会在产品页面上更改它,但不会在目录视图上更改。那是不同的功能吗?
  • "但不在目录视图中"。如果“目录视图”是指产品存档/商店页面,则取决于您使用的主题。默认情况下,这在这些页面上不可见,您的主题将为此使用一些自定义代码。因此,不可能给出一个笼统的答案,因为有 1000 多个不同的主题,每个主题都有自己特定的属性和设置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-13
  • 2022-10-09
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多