简体   繁体   English

WooCommerce 自定义缺货文本 + 特定产品 ID

[英]WooCommerce custom out of stock text + specific product id

Here is what I am trying to do这是我想要做的

  1. Detect if a specific product is in stock检测特定产品是否有库存
  2. If yes, edit the custom stock message如果是,请编辑自定义股票消息
  3. display a custom message above the A2C button (where stock quantity notification is)在 A2C 按钮上方显示自定义消息(库存数量通知所在的位置)

Problem: ALL products are at the moment being edited.问题:目前正在编辑所有产品。 I am not successfully applying it do specific product IDs.我没有成功地应用它做特定的产品 ID。

This is what I tried so far这是我到目前为止尝试过的

add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
    global $product;

    // custom 
    if ( $_product->is_in_stock() && $product_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;

} }

Thanks!谢谢!

Try this code试试这个代码

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据 WooCommerce 中的产品类别自定义“缺货”文本 - Custom “Out of stock” text based on product category in WooCommerce 更改 WooCommerce 产品可用性 库存文本由自定义文本 - Change WooCommerce product availability In stock text by a custom text 显示用于 woocommerce 缺货产品变体的自定义 div 块 - Display a custom div block for woocommerce out of stock product variations 当 WooCommerce 档案中的产品缺货时删除自定义数量字段 - Remove custom quantity field when product out of stock on WooCommerce archives 在 WooCommerce 中产品缺货时更改单个添加到购物车文本 - Change single add to cart text when product is out of stock in WooCommerce 如何在 Woocommerce 缺货产品文本下方添加单个产品的 Woocommerce 产品价格 - How can I add below of Woocommerce out of stock product text the Woocommerce product price on a single product 更改自定义产品类型的 WooCommerce“库存”文本(预购) - Change WooCommerce "In Stock" text for custom product type (pre-order) 通过 WooCommerce 中的自定义库存数量减少来灰显“缺货”产品变化 - Greying out “out-of-stock” product variations with custom stock quantity reduction in WooCommerce 在 Woocommerce 中的产品变体旁边显示“缺货” - Show “Out Of Stock” next to product variation in Woocommerce 如何将缺货产品转移到垃圾 Woocommerce? - How to move out of stock product to trash Woocommerce?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM