在“运送到其他地址?”时为特定用户角色添加费用?已在 WooCommerce 结帐时检查
Posted
技术标签:
【中文标题】在“运送到其他地址?”时为特定用户角色添加费用?已在 WooCommerce 结帐时检查【英文标题】:Add a fee for specific user roles when "Ship to a different address?" has been checked on WooCommerce checkout 【发布时间】:2022-01-07 13:57:34 【问题描述】:我有一个具有不同用户角色的批发网站,并希望允许员工也从这里开始在线订购。
如果用户角色“团队”和“团队 2”选择运送到不同的地址(如果发送到他们的帐单邮寄地址,他们可以免费送货),我只想添加 5 美元的费用。
如果选择运送到其他地址,其他用户角色不应看到费用。
这是我发现的最接近的解决方案,但需要帮助配置此代码以仅适用于这两个用户角色,而不适用于其他用户角色。
// send as gift for Team & Team2 role add $5 fee
add_filter( 'woocommerce_form_field' , 'remove_order_comments_optional_fields_label', 10, 4 );
function remove_order_comments_optional_fields_label( $field, $key, $args, $value )
// Only on checkout page for Order notes field
if( 'ship_to_different_address' === $key && is_checkout() )
$optional = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
$field = str_replace( $optional, '', $field );
return $field;
// Ajax / jQuery script
add_action( 'wp_footer', 'ship_to_different_address_script' );
function ship_to_different_address_script()
// On checkoutpage
if( ( is_checkout() && ! is_wc_endpoint_url() ) ) :
?>
<script type="text/javascript">
jQuery( function($)
if (typeof woocommerce_params === 'undefined')
return false;
console.log('defined');
$('input[name=ship_to_different_address]').click( function()
var fee = $(this).prop('checked') === true ? '1' : '';
$.ajax(
type: 'POST',
url: woocommerce_params.ajax_url,
data:
'action': 'ship_to_different_address',
'ship_to_different_address': fee,
,
success: function (result)
$('body').trigger('update_checkout');
console.log(result);
,
);
);
);
</script>
<?php
endif;
// Get the ajax request and set value to WC session
add_action( 'wp_ajax_ship_to_different_address', 'get_ajax_ship_to_different_address' );
add_action( 'wp_ajax_nopriv_ship_to_different_address', 'get_ajax_ship_to_different_address' );
function get_ajax_ship_to_different_address()
if ( isset($_POST['ship_to_different_address']) )
WC()->session->set('ship_to_different_address', ($_POST['ship_to_different_address'] ? '1' : '0') );
echo WC()->session->get('ship_to_different_address');
die();
// Add / Remove a custom fee
add_action( 'woocommerce_cart_calculate_fees', 'add_remove_ship_to_different_address', 10, 1 );
function add_remove_ship_to_different_address( $cart )
// Only on checkout
if ((is_admin() && !defined('DOING_AJAX')) || is_cart())
return;
$fee_amount = 5.00;
if (WC()->session->get('ship_to_different_address'))
$cart->add_fee(__('Shipping fee', 'woocommerce'), $fee_amount);
【问题讨论】:
【参考方案1】:您的代码包含一些错误、缺失的部分和不必要的步骤。
此答案显示(没有任何用户限制)在“运送到其他地址?”时如何添加费用?已检查:
// The jQuery Ajax request
function action_wp_footer()
// Only checkout page
if ( is_checkout() && ! is_wc_endpoint_url() ) :
// Remove "ship_different" custom WC session on load
if ( WC()->session->get( 'ship_different' ) )
WC()->session->__unset( 'ship_different' );
// jQuery Ajax code below
?>
<script type="text/javascript">
jQuery( function($)
if ( typeof wc_checkout_params === 'undefined' )
return false;
var a = '#ship-to-different-address-checkbox', b = '';
// Ajax function
function triggerSTDA( value )
$.ajax(
type: 'POST',
url: wc_checkout_params.ajax_url,
data:
'action': 'ship_different_address',
'ship_different': value,
,
success: function (result)
$( 'body' ).trigger( 'update_checkout' );
// console.log( result ); // For testing (to be removed)
);
$( a ).on( 'change', function()
b = $( this ).prop('checked') === true ? 'yes' : 'no';
triggerSTDA( b );
);
);
</script>
<?php
endif;
add_action( 'wp_footer', 'action_wp_footer' );
// The Wordpress Ajax PHP receiver
function get_ajax_ship_different_address()
if ( isset( $_POST['ship_different'] ) )
WC()->session->set( 'ship_different', esc_attr( $_POST['ship_different'] ) );
echo $_POST['ship_different'];
die();
add_action( 'wp_ajax_ship_different_address', 'get_ajax_ship_different_address' );
add_action( 'wp_ajax_nopriv_ship_different_address', 'get_ajax_ship_different_address' );
// Add / remove a custom fee
function action_woocommerce_cart_calculate_fees( $cart )
// Only on checkout
if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || is_cart() )
return;
$fee_amount = 5;
if ( WC()->session->get( 'ship_different' ) == 'yes' )
$cart->add_fee( __( 'Shipping fee', 'woocommerce'), $fee_amount );
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
但是,您的问题是仅基于某些用户角色应用此功能:
所以你得到:
// The jQuery Ajax request
function action_wp_footer()
// Only for logged in users
if ( ! is_user_logged_in() )
return;
// Only checkout page
if ( is_checkout() && ! is_wc_endpoint_url() ) :
// Remove "ship_different" custom WC session on load
if ( WC()->session->get( 'ship_different' ) )
WC()->session->__unset( 'ship_different' );
// jQuery Ajax code below
?>
<script type="text/javascript">
jQuery( function($)
if ( typeof wc_checkout_params === 'undefined' )
return false;
var a = '#ship-to-different-address-checkbox', b = '';
// Ajax function
function triggerSTDA( value )
$.ajax(
type: 'POST',
url: wc_checkout_params.ajax_url,
data:
'action': 'ship_different_address',
'ship_different': value,
,
success: function (result)
$( 'body' ).trigger( 'update_checkout' );
// console.log( result ); // For testing (to be removed)
);
$( a ).on( 'change', function()
b = $( this ).prop('checked') === true ? 'yes' : 'no';
triggerSTDA( b );
);
);
</script>
<?php
endif;
add_action( 'wp_footer', 'action_wp_footer' );
// The Wordpress Ajax PHP receiver
function get_ajax_ship_different_address()
if ( isset( $_POST['ship_different'] ) )
WC()->session->set( 'ship_different', esc_attr( $_POST['ship_different'] ) );
echo $_POST['ship_different'];
die();
add_action( 'wp_ajax_ship_different_address', 'get_ajax_ship_different_address' );
add_action( 'wp_ajax_nopriv_ship_different_address', 'get_ajax_ship_different_address' );
// Add / remove a custom fee
function action_woocommerce_cart_calculate_fees( $cart )
// Only on checkout
if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || is_cart() )
return;
// Only for logged in users
if ( ! is_user_logged_in() )
return;
// Get current WP_User Object
$user = wp_get_current_user();
// Roles to check, adapt to your specific needes
$roles_to_check = array( 'team', 'team2', 'administrator' );
// User roles
$roles = ( array ) $user->roles;
// Compare
$compare = array_diff( $roles, $roles_to_check );
// Result is empty
if ( empty ( $compare ) )
// Amount
$fee_amount = 5;
if ( WC()->session->get( 'ship_different' ) == 'yes' )
$cart->add_fee( __( 'Shipping fee', 'woocommerce'), $fee_amount );
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
在这个答案中部分使用
Show hide payment gateways based on checkout fields in Woocommerce Checkbox field that add / remove a custom fee in WooCommerce Check WooCommerce User Role and Payment Gateway and if they match - Apply fee【讨论】:
以上是关于在“运送到其他地址?”时为特定用户角色添加费用?已在 WooCommerce 结帐时检查的主要内容,如果未能解决你的问题,请参考以下文章
在 WooCommerce 中为特定用户角色创建订单时添加额外的客户注释