基于用户角色和小计金额的 WooCommerce 运输方式

gem*_*ita 6 php wordpress user-roles woocommerce shipping-method

我需要添加两种不同的运费,具体取决于购买用户的角色。我的角色是“wholesale_customer”,不应支付运费。但是,如果购物车小计等于或小于 \xe2\x82\xac90,则必须阻止购买并在结账时添加一条消息。(记住一定要达到\xe2\x82\xac90的数量)

\n

另一个选项是所有其他 WordPress 角色,如果他们的购物车小计为 \xe2\x82\xac40 或更少,则需要支付 \xe2\x82\xac5 运费。

\n

我已在 WooCommerce 中配置了送货,我创建了两种具有最低订单金额的免费送货方式,一种为 \xe2\x82\xac40,另一种为 \xe2\x82\xac90。

\n

问题是,具有“wholesale_customer”角色的用户在达到 \xe2\x82\xac40 的金额时,也启用了该免费送货方法,但这种情况不应该发生,因为对于此用户角色(\' Wholesale_customer \' ) 如果购买量超过 \xe2\x82\xac90 的最小值,则免运费。

\n

我已按以下方式在 WooCommerce 中配置发货,创建 2 种发货方式:\n- 其中一种在 \xe2\x82\xac90 处具有“所需的最小数量”

\n
    \n
  • 另一个在 \xe2\x82\xac40 处具有“所需的最小数量”

    \n
  • \n
  • 以及其中一份运费为 \xe2\x82\xac0。

    \n
  • \n
\n

我尝试添加以下功能来添加以获得我需要的内容,但所有运输方式始终处于启用状态,因此也为用户激活了“最小 \xe2\x82\xac40 免费送货”的零售运输方式具有批发客户角色的情况不应发生这种情况,因为具有此角色的用户将受益于不属于他们的好处。

\n

我正在展示一些我用来尝试做我正在寻找的事情的代码,因为我做了很多测试。只是我还没有办法添加我在演示文稿中提到的角色“wholesale_customer\”的文本\n显示 WooCommerce 设置的图像

\n
function custom_shipping_methods_visibility( $available_methods ) {\n     \n        $subtotal = WC()->cart->subtotal;\n        \n        $user = wp_get_current_user();\n        $user_roles = $user->roles;\n        \n        // Check user role and cart subtotal to show/hide shipping methods\n        if ( in_array( \'wholesale_customer\', $user_roles ) && $subtotal <= 70 ) {\n            unset( $available_methods[\'flat_rate:13\'] );\n        } elseif ( $subtotal <= 30 ) {\n            unset( $available_methods[\'flat_rate:9\'] );\n        }\n        \n        return $available_methods;\n    }\n    add_filter( \'woocommerce_package_rates\', \'custom_shipping_methods_visibility\', 10, 1 );\n
Run Code Online (Sandbox Code Playgroud)\n

////////////////////////////////////////

\n
// Adds a custom function to filter shipping methods based on cart role and subtotal\n    function custom_shipping_methods( $available_methods ) {\n        // Get cart subtotal\n        $subtotal = WC()->cart->subtotal;\n        \n        // Gets the role of the current user\n        $user = wp_get_current_user();\n        $user_roles = $user->roles;\n        \n        //Check user role and cart subtotal to adjust shipping methods\n        if ( in_array( \'wholesale_customer\', $user_roles ) && $subtotal < 70 ) {\n            foreach ( $available_methods as $method_id => $method ) {\n                // Hide the shipping methods if the user is a wholesaler and the subtotal is less than \xe2\x82\xac70\n                unset( $available_methods[ $method_id ] );\n            }\n            \n            // Show a warning message\n            wc_add_notice( \'Debes alcanzar un m\xc3\xadnimo de 70\xe2\x82\xac en tu carrito para realizar el pedido.\', \'error\' );\n        }\n        \n        return $available_methods;\n    }\n    add_filter( \'woocommerce_package_rates\', \'custom_shipping_methods\', 10, 1 );\n
Run Code Online (Sandbox Code Playgroud)\n

配置-1

\n

在此输入图像描述

\n

配置-2:

\n

在此输入图像描述

\n

配置-3:

\n

在此输入图像描述

\n

配置-4:\n在此输入图像描述

\n

Loi*_*tec 4

更新

\n

首先,为了防止“wholesale_customer”用户在购物车小计小于 \xe2\x82\xac90 时下订单,以下代码将:

\n
    \n
  • 在购物车和结帐页面上显示相关提醒通知,
  • \n
  • 在结账验证过程中显示错误通知,避免任何购买。
  • \n
\n
// Conditional function: Is user a Wholesale Customer\nfunction is_a_wholesale_customer() {\n    if ( ! is_user_logged_in() ) return false;\n\n    return (bool) in_array( \'wholesale_customer\', wp_get_current_user()->roles );\n}\n\n// Conditional function: Is user a (normal) Customer\nfunction is_a_normal_customer() {\n    if ( ! is_user_logged_in() ) return false;\n\n    return (bool) in_array( \'customer\', wp_get_current_user()->roles );\n}\n\n// Conditional function: Is "wholesale_customer" allowed to order\nfunction is_wholesale_customer_allowed_to_order() {\n    // Check "wholesale_customer" user role and required subtotal\n    return is_a_wholesale_customer() && WC()->cart->subtotal < 90 ? false : true;\n}\n\n// The notice text (for "wholesale_customer")\nfunction wholesale_customer_text_error_notice(){\n    return __(\'Please remember that you must reach an amount of \xe2\x82\xac90\', \'woocommerce\');\n}\n\n// Notice reminder based on required subtotal (for "wholesale_customer")\nadd_action( \'woocommerce_before_cart\', \'check_cart_subtotal_wholesale_customer\' ); // cart\nadd_action( \'woocommerce_before_checkout_form\', \'check_cart_subtotal_wholesale_customer\' ); // checkout\nfunction check_cart_subtotal_wholesale_customer() {\n    if ( ! is_wholesale_customer_allowed_to_order() ) {\n        wc_print_notice( wholesale_customer_text_error_notice() );\n    }\n}\n// Checkout validation based on required subtotal (for "wholesale_customer")\nadd_action( \'woocommerce_checkout_process\', \'wholesale_customer_checkout_validation\' );\nfunction wholesale_customer_checkout_validation() {\n    if ( ! is_wholesale_customer_allowed_to_order() ) {\n        wc_add_notice( wholesale_customer_text_error_notice(), \'error\' ); // Displays an error notice\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n
\n

对于运输方式,您的设置是正确的。

\n
\n

以下代码将根据您的用户角色(“wholesale_customer”和“customer”)显示/隐藏运输方式(也添加未登录的用户)以及免费送货的最低购物车小计金额显示/隐藏送货方式。

\n
\n

更新:代码现在适用于未登录的用户(就像“客户”用户角色)

\n
\n

对于“wholesale_customer”,所有运输方式都将被禁用,直到购物车小计达到 \xe2\x82\xac90。

\n

当免费送货可用时,所有其他送货方式将被隐藏。

\n

对于 2 种免费送货方式中的每一种,您都需要获取正确的运费 ID。
\n如果购物车小计高于 \xe2\x82\xac90,请在结账页面的送货部分检查每个免费送货选项的单选按钮(请参阅下面的屏幕截图):

\n

在此输入图像描述

\n

现在,您可以在下面的代码中设置两种免费送货方式的费率 ID:

\n
// showing / Hidding shipping methods\nadd_filter( \'woocommerce_package_rates\', \'filter_shipping_package_rates\', 10, 2 );\nfunction filter_shipping_package_rates( $rates, $package ) {\n    // Settings\n    $free_rate_ids  = array( \n        \'wholesale_customer\'   => \'free_shipping:2\', // Here set the free shipping rate ID for wholesale user\n        \'customer_or_unlogged\' => \'free_shipping:3\', // Here set the free shipping rate ID for customer or unlogged user\n    );\n\n    $error_data[\'all_rates\'] = array_keys($rates);\n    \n    // "Wholesale" user role\n    if ( is_a_wholesale_customer() ) {\n        $key = \'wholesale_customer\';\n        // show only "Wholesale" free shipping when available\n        if( isset($rates[$free_rate_ids[$key]]) ) {\n            return array( $free_rate_ids[$key] => $rates[$free_rate_ids[$key]] );\n        } \n        // Hide all shipping methods (no free shipping available)\n        else {\n            return array();\n        }\n    } \n    // "Customer" user role or unlogged users\n    else {\n        $key = \'customer_or_unlogged\';\n        // show only "Normal" free shipping when available\n        if( isset($rates[$free_rate_ids[$key]]) ) {\n            return array( $free_rate_ids[$key] => $rates[$free_rate_ids[$key]] );\n        } \n    } \n    return $rates;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。

\n
\n

保存代码后,要刷新送货方式缓存数据,请不要忘记清空您的购物车(或在 Woocommerce 送货设置中禁用、保存和启用、保存当前送货区域的相关送货方式)

\n
\n