gpt4 book ai didi

php - 根据 WooCommerce 中的用户元数据显示或隐藏送货方式

转载 作者:行者123 更新时间:2023-12-04 03:49:27 24 4
gpt4 key购买 nike

我有一个为非批发客户隐藏运费的代码,请帮我重做,我需要为批发客户隐藏运费选项。

/**
* Removes shipping methods for non-wholesale customers.
* Please be sure to clear your WooCommerce store's cache.
* Adjust 'flat_rate:2' to match that of your wholesale shipping method.
*/

function my_wcs_remove_shipping_non_wholesale( $rates, $package ){
global $current_user;

$is_wholesale = get_user_meta( $current_user->ID, 'wcs_wholesale_customer', true );

if ( ! $is_wholesale ) {
foreach( $rates as $method ) {
if ( $method->id == 'flat_rate:2' ) {
unset( $rates[$method->id] );
}
}
}

return $rates;
}
add_filter( 'woocommerce_package_rates', 'my_wcs_remove_shipping_non_wholesale', 10, 2 );

最佳答案

您不需要有 2 个函数,一个用于批发客户,另一个用于非批发客户……您可以将两者合并到同一个函数中,如下所示:

add_filter( 'woocommerce_package_rates', 'shipping_methods_based_on_wholesale_customer', 10, 2 );
function shipping_methods_based_on_wholesale_customer( $rates, $package ){
$is_wholesale = get_user_meta( get_current_user_id(), 'wcs_wholesale_customer', true );

// Set the shipping methods rate ids in the arrays:
if( $is_wholesale ) {
$shipping_rates_ids = array('flat_rate:1', 'flat_rate:4'); // To be removed for NON Wholesale users
} else {
$shipping_rates_ids = array('flat_rate:2'); // To be removed for Wholesale users
}

// Loop through shipping rates fro the current shipping package
foreach( $rates as $rate_key => $rate ) {
if ( in_array( $rate_key, $shipping_rates_ids) ) {
unset( $rates[$rate_key] );
}
}

return $rates;
}

代码进入事件子主题(或事件主题)的 functions.php 文件。它应该有效。

Don't forget to empty the cart after saving the code, to refresh cached shipping data

关于php - 根据 WooCommerce 中的用户元数据显示或隐藏送货方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64633909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com