首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WooCommerce cart.php显示后台消息

WooCommerce cart.php显示后台消息
EN

Stack Overflow用户
提问于 2016-12-08 11:22:54
回答 1查看 1.6K关注 0票数 1

我正试图在购物车中的产品下面显示一条信息。如果该产品处于待定状态,它将显示一个后台通知,因此我希望它在没有后台通知时显示另一条消息。

我试过的代码是:

代码语言:javascript
复制
// Backorder notification
if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) ) {
    echo '<p class="backorder_notification">' . esc_html__( 'Available in 3-5 Working Days', 'teencode' ) '</p>';
}
else; {
    echo '<p class="backorder_notification">' . esc_html__( 'Available Next Day', 'teencode' ) '</p>';
}

但是,当查看购物车时,它没有显示消息。

我哪里出问题了?

EN

回答 1

Stack Overflow用户

发布于 2016-12-08 13:02:04

可能您应该尝试基于代码(条件)在woocommerce_before_cart中挂钩的自定义函数来显示自定义通知。代码中有很多小错误。

这是代码:

代码语言:javascript
复制
add_action( 'woocommerce_before_cart', 'custom_backorders_notifications' );
function custom_backorders_notifications(){

    $backorders_notification = false;

    foreach( WC()->cart->get_cart() as $cart_item ):
        $product_id = $cart_item['product_id']; // get the product ID for the current cart item
        $item_qty = $cart_item['quantity']; // get the cart item quantity
        $product = wc_get_product($cart_item['product_id']); // get the $product OBJECT

        // Backorder notification with your existing (untested) condition
        if ( $product->backorders_require_notification() && $product->is_on_backorder( $item_qty ) ){
            $backorders_notification = true;
            break;
        }

    endforeach;

    if ( $backorders_notification )
        echo '<p class="backorder_notification">' . esc_html__( "Available in 3-5 Working Days", "teencode" ) . '</p>';
    else
        echo '<p class="backorder_notification">' . esc_html__( "Available Next Day", "teencode" ) . '</p>';
}

这段代码是经过测试和工作的。

代码在您的活动子主题(或主题)的function.php文件中。也可以在任何插件php文件中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41038280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档