【问题标题】:Remove conditionally "proceed to checkout" button from minicart in Woocommerce从 Woocommerce 中的迷你购物车有条件地删除“继续结帐”按钮
【发布时间】:2018-10-11 06:35:07
【问题描述】:

在 woocommerce 中,如果满足两个条件,我目前正在寻求在我的主题的 functions.php 文件中添加一个函数。然后,使用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 美元。

【问题讨论】:

  • 我可以看到的主要问题(没有测试,因为我现在不在我的办公桌前)是if( 'woocommerce_widget_shopping_cart' ) { - 你实际上并没有在你的 if 语句中测试任何东西。您正在测试字符串“woocommerce_widget_shopping_cart”怎么样?它应该等于或不等于?

标签: php wordpress if-statement woocommerce cart


【解决方案1】:

请注意,您使用的挂钩仅适用于 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 文件中。它现在应该可以更好地工作了。

【讨论】:

  • 这样干净多了,非常感谢。但是,正如您在restaurantsonwheels.com/restaurants/rancho-viejo-mexican-food 找到的页面/帖子 232 中看到的那样,它不会激活“结帐”按钮,除非最低金额高于 150 美元,而不是此页面所需的 100 美元......谢谢你的帮助
  • @ThirsHuey 抱歉我没听懂?你需要确保 is_page([232]) 对你有用(没人能猜到)……
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 1970-01-01
相关资源
最近更新 更多