php - WooCommerce 根据产品类别替换购物车/结帐中的 "Available on backorder"

标签 php wordpress woocommerce hook-woocommerce taxonomy-terms

我编写了一些代码,用于在基于产品类别的产品详细信息页面上显示自定义延期交货消息。

function custom_backorder_message( $text, $product ){
    if ( $product->managing_stock() && $product->is_on_backorder( 1 ) ) {

        if( has_term( 'bridal-line', 'product_cat' ) ) {
            $text = __( 'Your piece will be handcrafted for you. Upon order we will manufacture your piece of eternity. Sadly, we can not give you a timeline, due to Covid 19, but are expecting 5-7 weeks', 'text-domain' );
        }else {
            $text = __( 'This product is currently out of stock, but upon order we will handcraft your piece. Sadly, we can not give you a timeline, due to Covid 19, but are expecting 6-8 week.', 'text-domain' );
        }
    }
    return $text;
}
add_filter( 'woocommerce_get_availability_text', 'custom_backorder_message', 10, 2 );

现在,在购物车页面上显示“可延期交货”。我怎样才能在那里显示正确的延期交货信息?

任何帮助表示赞赏!

最佳答案

使用: woocommerce_cart_item_backorder_notification

Note that the third parameter ($product_id) is specified by has_term, this is because default the current post(ID) is used. However, if there are several products in the shopping cart, there are multiple IDs...



// Change backorder notification - Single product page
function custom_availability_text( $text, $product ) {
    // Returns whether or not the product is stock managed.
    if ( $product->managing_stock() && $product->is_on_backorder( 1 ) ) {
        // Check if the current post has any of given terms.
        if( has_term( 'bridal-line', 'product_cat' ) ) {
            $text = __( 'My first text', 'woocommerce' );
        } else {
            $text = __( 'My second text', 'woocommerce' );
        }
    }
    return $text;
}
add_filter( 'woocommerce_get_availability_text', 'custom_availability_text', 10, 2 );

// Change backorder notification - Shop page
function custom_cart_item_backorder_notification( $html, $product_id ){
    // Check if the current post has any of given terms.
    if ( has_term( 'bridal-line', 'product_cat', $product_id ) ) {
        $html = '<p class="backorder_notification">' . esc_html__( 'My first text', 'woocommerce' ) . '</p>';
    } else {
        $html = '<p class="backorder_notification">' . esc_html__( 'My second text', 'woocommerce' ) . '</p>';
    }

    return $html;
}
add_filter( 'woocommerce_cart_item_backorder_notification', 'custom_cart_item_backorder_notification', 10, 2 );

关于php - WooCommerce 根据产品类别替换购物车/结帐中的 "Available on backorder",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61273620/

相关文章:

php - 当 mysql_num_rows 说有数据时,mysql_fetch_array 返回 false,为什么?

css - @font-face css 添加自定义字体

jquery - WordPress中ajax加载页面后如何触发jquery插件?

使用 GET 变量重写 WordPress URL

php - Woocommerce 我的帐户页面为新客户运行额外的循环

javascript - 如何从 WordPress 中的 ajax 处理程序调用我主题的 functions.php 中的方法?

php - 使用不显眼的 AJAX

php - WordPress:页面的自定义 URL

php - 限制购物车项目来自 WooCommerce 中的同一产品类别

css - 在 woocommerce 的移动 View 中每行显示 2 个产品