【问题标题】:Change order button text on checkout based on user role and cart total in WooCommerce根据 WooCommerce 中的用户角色和购物车总数更改结帐时的订单按钮文本
【发布时间】:2020-10-01 12:07:03
【问题描述】:

如果用户角色是客户,我们需要检查下面的函数。

如果订单总数为0,下面的函数改变下订单按钮的文本,我们需要它来检查用户角色是否也是客户并且总数为0。

我们目前使用的代码

function mishaa_custom_button_text($button_text) {
    global $woocommerce;
    $total = $woocommerce->cart->total;
    if ($total == 0 ) {
        $button_text = "Submit Registration";
    }
    return $button_text;
} 
add_filter( 'woocommerce_order_button_text', 'mishaa_custom_button_text' );

【问题讨论】:

    标签: php wordpress woocommerce checkout hook-woocommerce


    【解决方案1】:

    https://github.com/woocommerce/woocommerce/blob/4.1.0/includes/wc-template-functions.php#L2240

    • 在结账时输出付款方式。

    你可以使用wp_get_current_user();

    function filter_woocommerce_order_button_text( $button_text ) { 
        // Get cart total
        $cart_total = WC()->cart->get_cart_contents_total();
    
        // Get current user role
        $user = wp_get_current_user();
        $roles = ( array ) $user->roles;
    
        // Check
        if ( $cart_total == 0 && in_array( 'customer', $roles ) ) {
            $button_text = __('Submit Registration', 'woocommerce');
        }
    
        return $button_text;
    
    }
    add_filter( 'woocommerce_order_button_text', 'filter_woocommerce_order_button_text', 10, 1 );
    

    【讨论】:

    • @7ucf3r 非常感谢您的帮助和时间.. 我们已经测试了另一种解决方案,如果我们只是将 if ($total == 0 ) 更改为if ($total == 0 && current_user_can('customer')) .. 因为它也有效.. 这和你的答案有什么区别?
    • 1) 我没有使用全局 $woocommerce 变量,请参阅:Stop using global in PHP。 2) current_user_can( string $capability, mixed $args ) - 返回当前用户是否具有指定的能力
    • 所以使用你的解决方案比我的要好得多,对吗?还有你为什么在答案开头说:在结帐时输出付款方式。
    • 因为钩子在woocommerce_checkout_payment()函数中,如果你点击链接应该清楚了,另见:docs.woocommerce.com/wc-apidocs/function-wc_get_template.html
    • 是的,但是在代码中钩子 woocommerce_checkout_payment() 没有使用..对吗?
    猜你喜欢
    • 2016-03-20
    • 2020-12-07
    • 2021-08-04
    • 2019-01-21
    • 1970-01-01
    • 2018-02-28
    • 2020-06-03
    • 2017-08-14
    • 1970-01-01
    相关资源
    最近更新 更多