Display minimum weight and remaining weight message on cart and checkout page for specific category - WooCommerce

你离开我真会死。 提交于 2020-06-28 06:10:20

问题


Inspired from Minimum cart amount for specific product categories in WooCommerce I have the following code , I would have liked it applied to the category (slug: special-box) and hide the "proceed to check out button" until the minimum is reached.

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

回答1:


The following code checks whether a product in the shopping cart belongs to the category: special-box and hides the "proceed to checkout button" if the minimum weight has not been reached.

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' );


来源:https://stackoverflow.com/questions/61041903/display-minimum-weight-and-remaining-weight-message-on-cart-and-checkout-page-fo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!