首页 > 解决方案 > 从 Woocommerce 中的迷你购物车有条件地删除“继续结帐”按钮

问题描述

functions.php在 woocommerce 中,如果满足两个条件,我目前正在寻求在我的主题文件中添加一个函数。然后,elseif()如果仅满足一个条件,则使用 部署该功能。

代码如下:

add_action( 'woocommerce_widget_shopping_cart_before_buttons' , 'wc_minimum_order_amount' );

function wc_minimum_order_amount() {
    $minimum = 150;
    $minimum2 = 100;

    if ( is_page([232]) && WC()->cart->subtotal < $minimum2 ) {
        if( 'woocommerce_widget_shopping_cart' ) {
            wc_print_notice(
                sprintf( 'Your current order total does not meet the %s minimum' , 
                    wc_price( $minimum2 )
                ), 'error' 
            );
            remove_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_proceed_to_checkout', 20 );

        } 
        else {
            wc_add_notice( 
                sprintf( 'Your current order total does not meet the %s minimum' , 
                    wc_price( $minimum2 )
                ), 'error' 
            );
        }

    }
    elseif ( WC()->cart->subtotal < $minimum ) {
        if( 'woocommerce_widget_shopping_cart' ) {
            wc_print_notice(
                sprintf( 'Your current order total does not meet the %s minimum', 
                    wc_price( $minimum )
                ), 'error' 
            );
            remove_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_proceed_to_checkout', 20 );

        } 
        else {
            wc_add_notice( 
                sprintf( 'Your current order total does not meet the %s minimum' , 
                    wc_price( $minimum )
                ), 'error' 
            );
        }
    }
}

如果未达到最低订单金额,我想要做的是隐藏 woocommerce 小部件的结帐按钮。但是,不同的页面有不同的最小值。

如果购物车不等于 150 美元,我会尝试隐藏结帐按钮。但特别是对于一页,我只希望购物车至少有 100 美元。

标签: phpwordpressif-statementwoocommercecart

解决方案


请注意,您使用的钩子仅适用于 minicart 小部件,因此您无需在IF语句中对其进行测试。

您正在使应该变得更加复杂。请尝试以下重新访问的代码:

add_action( 'woocommerce_widget_shopping_cart_before_buttons' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
    $min_amount = is_page([232]) ? 100 : 150;

    if( WC()->cart->subtotal < $min_amount ) {
        remove_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_proceed_to_checkout', 20 );

        wc_add_notice( 
            sprintf( 'Your current order total does not meet the %s minimum' , 
                wc_price( $min_amount )
            ), 'error' 
        );
    } 
}

代码位于您的活动子主题(或活动主题)的 function.php 文件中。它现在应该更好地工作。


推荐阅读