Remove shipping cost if custom checkbox is checked in WooCommerce Checkout(如果在 WooCommerce Checkout 中选中自定义复选框,则删除运费)
问题描述
如果选中结帐字段中的复选框,我将尝试将运费价格设置为 0.00 美元.
I am trying to set the price of shipping rates to $0.00 if a checkbox in the checkout fields is checked.
当前尝试:
function no_shipping_for_own_ups($rates,$package) {
foreach ($rates as $rate) {
//Set the price
$rate->cost = 0;
}
return $rates;
}
function woo_add_cart_ups_y_n_fee( $cart ){
if ( ! $_POST || ( is_admin() && ! is_ajax() ) ) {
return;
}
if ( isset( $_POST['post_data'] ) ) {
parse_str( $_POST['post_data'], $post_data );
} else {
$post_data = $_POST;
}
if (isset($post_data['billing_ups_yn'])) {
add_filter( 'woocommerce_package_rates','no_shipping_for_own_ups', 99, 2 );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_ups_y_n_fee', 43, 1 );
As $post_data['billing_ups_yn']
是我设置的复选框,用于在触发时更新结帐.
As $post_data['billing_ups_yn']
being the checkbox that I set to update the checkout when triggered.
只需选中复选框,然后应用过滤器将运费设置为 0.
但是,这不起作用.
Simply if the checkbox is checked, then apply the filter to set shipping rates to 0.
This, however, isn't working.
推荐答案
通过 Ajax 在结帐页面中的自定义 javascript 事件上更改运输方式成本比有条件地更改费用或其他任何事情要复杂得多,如下所示.
To alter shipping methods cost via Ajax on a custom javascript event in checkout page is quiet more complicated than conditionally altering fees or anything else as you will see below.
为了存储 Ajax 数据,我使用 WC_Sessions 并改变运输方式成本只有在您操纵运输会话数据
To store the Ajax data I use WC_Sessions and to alter Shipping methods costs will only work if you manipulate the shipping sessions data
完整的工作代码:
// Add a Custom checkbox field for shipping options (just for testing)
add_action( 'woocommerce_after_checkout_billing_form', 'custom_billing_checkbox_for_testing', 10, 1 );
function custom_billing_checkbox_for_testing( $checkout ) {
$field_id = 'billing_ups_yn';
// Get the checked state if exist
$billing_ups = WC()->session->get('billing_ups' );
if(empty($billing_ups))
$billing_ups = $checkout->get_value( $field_id );
// Add the custom checkout field (checkbox)
woocommerce_form_field( $field_id, array(
'type' => 'checkbox',
'class' => array( 'form-row-wide' ),
'label' => __('Billing UPS'),
), $billing_ups );
}
// function that gets the Ajax data
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_ups'] == '1' ){
WC()->session->set('billing_ups', '1' );
} else {
WC()->session->set('billing_ups', '0' );
}
echo json_encode( WC()->session->get('billing_ups' ) );
die(); // Alway at the end (to avoid server error 500)
}
// Conditionally changing the shipping methods costs
add_filter( 'woocommerce_package_rates','conditional_custom_shipping_cost', 90, 2 );
function conditional_custom_shipping_cost( $rates, $package ) {
if ( WC()->session->get('billing_ups' ) == '1' ){
foreach ( $rates as $rate_key => $rate_values ) {
// Not for "Free Shipping method" (all others only)
if ( 'free_shipping' !== $rate_values->method_id ) {
// Set the rate cost
$rates[$rate_key]->cost = 0;
// Set taxes rate cost (if enabled)
$taxes = array();
foreach ($rates[$rate_key]->taxes as $key => $tax)
if( $rates[$rate_key]->taxes[$key] > 0 ) // set the new tax cost
$taxes[$key] = 0;
$rates[$rate_key]->taxes = $taxes;
}
}
}
return $rates;
}
// Enabling, disabling and refreshing session shipping methods data
add_action( 'woocommerce_checkout_update_order_review', 'refresh_shipping_methods', 10, 1 );
function refresh_shipping_methods( $post_data ){
$bool = true;
if ( WC()->session->get('billing_ups' ) == '1' ) $bool = false;
// Mandatory to make it work with shipping methods
foreach ( WC()->cart->get_shipping_packages() as $package_key => $package ){
WC()->session->set( 'shipping_for_package_' . $package_key, $bool );
}
WC()->cart->calculate_shipping();
}
// The Jquery script
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_ups_yn_field input').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_ups': checked,
},
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
}
代码位于活动子主题(或活动主题)的 function.php 文件中.
经过测试并有效.
与您的评论相关的更新:
如果您希望在加载时始终默认取消选中复选框,您将用这个函数替换第一个函数:
If you wish to have always the checkbox unchecked by default at load you will replace the first function by this one:
// Add a Custom checkbox field for shipping options (just for testing)
add_action( 'woocommerce_after_checkout_billing_form', 'custom_billing_checkbox_for_testing', 10, 1 );
function custom_billing_checkbox_for_testing( $checkout ) {
$field_id = 'billing_ups_yn';
// Add the custom checkout field (checkbox)
woocommerce_form_field( $field_id, array(
'type' => 'checkbox',
'class' => array( 'form-row-wide' ),
'label' => __('Billing UPS'),
), '' );
}
这篇关于如果在 WooCommerce Checkout 中选中自定义复选框,则删除运费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果在 WooCommerce Checkout 中选中自定义复选框,则
基础教程推荐
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01