问题描述
我使用插件 **Checkout Field Editor (Checkout Manager) for WooCommerce 创建了一个自定义字段
**
我的字段名称和 ID 是 billing_delivery_location
我已将此字段添加为选择框。现在我想要的是,如果用户将其更改为值 eslamshahr
,则运费成本必须为 0
(确切地说,这意味着用户将亲自到货并交付),否则基于重量总购物车内容它将是动态的(小于 1KG = 28900,小于 2KG = 309700,大于 2KG = 320600)也基于状态(如果在所有三个重量中都等于 tehran
运费必须是16800),否则将是其他 3 个价格。
我所做的是在下面的代码中:
woocommerce_package_rates
add_filter('woocommerce_package_rates','nasha_shipping_based_on_weight',9999,2);
function nasha_shipping_based_on_weight($rates,$package)
{
// WC()->session->set('shipping_for_package_0',[ 'package_hash' => '' ]);
$weight = WC()->cart->get_cart_contents_weight();
$state = WC()->checkout->get_value('billing_state');
$city = WC()->checkout->get_value('billing_city');
// $location = WC()->checkout->get_value('billing_delivery_location');
$location = WC()->session->get('is_eslamshahr');
$isEslamshahr = ($location == '1') ? true : false;
$isTehran = ($state == 346) ? true : false;
$isLessthanOne = ($weight < 1000) ? true : false;
$isLessthanTwo = ($weight < 2000) ? true : false;
$isMoreThanTwo = ($weight >= 2000) ? true : false;
foreach ( $rates as $rate_key => $rate ) {
if ( $isEslamshahr ){
$rates[$rate_key]->label = "دریافت از انبار اسلامشهر";
$rates[ $rate_key ]->cost = 0;
$activeRate = $rates[$rate_key];
unset($rates);
$rates[] = $activeRate;
return $rates;
}
elseif ( $isTehran && ! $isEslamshahr ) {
$rates[ $rate_key ]->label = "ارسال به تهران";
$rates[ $rate_key ]->cost = 16800;
$activeRate = $rates[ $rate_key ];
unset($rates);
$rates[] = $activeRate;
return $rates;
}
elseif ( $isLessthanOne ) {
$rates[ $rate_key ]->label = "تا یک کیلوگرم";
$rates[ $rate_key ]->cost = 28900;
$activeRate = $rates[ $rate_key ];
unset($rates);
$rates[] = $activeRate;
return $rates;
}
elseif ( $isLessthanTwo ) {
$rates[ $rate_key ]->label = "تا دو کیلوگرم";
$rates[ $rate_key ]->cost = 309700;
$activeRate = $rates[ $rate_key ];
unset($rates);
$rates[] = $activeRate;
return $rates;
}
elseif ( $isMoreThanTwo ) {
$rates[ $rate_key ]->label = "بیش از دو کیلوگرم";
$rates[ $rate_key ]->cost = 320600;
$activeRate = $rates[ $rate_key ];
unset($rates);
$rates[] = $activeRate;
return $rates;
}
}
return $rates;
}
**ajax 设置 wc_session **
add_action('wp_ajax_woo_get_ajax_data','woo_get_ajax_data');
add_action('wp_ajax_nopriv_woo_get_ajax_data','woo_get_ajax_data');
function woo_get_ajax_data()
{
if ( $_POST[ 'billing_delivery_location' ] == 'eslamshahr' ){
WC()->session->set('is_eslamshahr','1');
}
else {
WC()->session->set('is_eslamshahr','0');
}
echo json_encode(WC()->session->get('is_eslamshahr'));
die();
}
WP 页脚操作
add_action('wp_footer','custom_checkout_script');
function custom_checkout_script()
{
?>
<script type="text/javascript">
jQuery(function ($) {
// update cart on delivery location checkBox option
$('#billing_delivery_location').change(function () {
// var checked = 0;
// if ($('#billing_ups_yn').is(':checked'))
// checked = 1;
$.ajax({
type: 'POST',url: wc_checkout_params.ajax_url,data: {
'action': 'woo_get_ajax_data','billing_delivery_location': $('#billing_delivery_location').val(),},success: function (result) {
$('body').trigger('update_checkout');
console.log('response: ' + result); // just for testing
},error: function (error) {
console.log(error); // just for testing
}
});
});
});
</script>
<?PHP
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)