首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在特定类别的购物车和结账页面上显示最小重量和剩余重量消息- WooCommerce

在特定类别的购物车和结账页面上显示最小重量和剩余重量消息- WooCommerce
EN

Stack Overflow用户
提问于 2020-04-05 11:34:08
回答 1查看 210关注 0票数 1

受启发,我有以下代码

代码语言:javascript
复制
add_action( 'woocommerce_check_cart_items', 'checkout_required_min_weight_mood' );
function checkout_required_min_weight_mood () {
    
    // soltanto sulle pagine carrello e check out
    if( ! ( is_cart() || is_checkout() ) ) return;

    // qui il minimo del peso
    $minimum_weight = 20; // 20 kg

    // Get the Cart's content total weight
    $total_weight = WC()->cart->get_cart_contents_weight();

    // Se il peso totale è inferiore al minimo, evitiamo il pagamento e visualizziamo un avviso di errore
    if( $total_weight < $minimum_weight  ) {
        // Visualizza un avviso di errore dinamico
        wc_add_notice( sprintf(
            '<strong>Il minimo peso di %s è richiesto prima di acquistare.</strong>'
            . '<br />Il peso totale dei prodotti nel tuo carrello al momento è di %s',
            wc_format_weight($minimum_weight),
            wc_format_weight($total_weight)
        ), 'error' );
    }
}

我希望它应用到类别(弹格:特殊-框),并隐藏“继续签出按钮”,直到达到最低限度。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-05 12:04:09

下面的代码检查购物车中的产品是否属于该类别:专用框隐藏,如果没有达到最小重量,则隐藏“继续到结帐按钮”。

代码语言:javascript
复制
function checkout_required_min_weight_mood () {
    // Only on cart and check out pages
    if( ! ( is_cart() || is_checkout() ) ) return;

    // The minimum weight
    $minimum_weight = 20; // 20 kg

    // Get the Cart's content total weight
    $total_weight = WC()->cart->get_cart_contents_weight();

    // Set $cat_in_cart to false
    $cat_in_cart = false;

    // Loop through all products in the Cart        
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

        // If Cart has category "special-box", set $cat_in_cart to true
        if ( has_term( 'special-box', 'product_cat', $cart_item['product_id'] ) ) {
            $cat_in_cart = true;
            break;
        }
    }

    // If the total weight is less than the minimum, we avoid payment and display an error notice & category is in the Cart
    if( $total_weight < $minimum_weight && $cat_in_cart ) {
        // Displays a dynamic error warning
        wc_add_notice( sprintf(
            '<strong>Il minimo peso di %s è richiesto prima di acquistare.</strong>'
            . '<br />Il peso totale dei prodotti nel tuo carrello al momento è di %s',
            wc_format_weight($minimum_weight),
            wc_format_weight($total_weight)
        ), 'error' );

        // Removing the Proceed to checkout button from the Cart page
        remove_action( 'woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout', 20);
    }
}
add_action( 'woocommerce_check_cart_items', 'checkout_required_min_weight_mood' );
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61041903

复制
相关文章

相似问题

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