在我的项目,我定制了一些WooCommerce特点。
我的“航运法”是:
1.交货
2.带走
我在结账页面是还添加了一个自定义字段<select>
填充了有效时间用于递送(“1”)或用于带走(情况“2”)。
它可能发生,用户选择2. take away
的车页面,然后选择一个时间有效期为“2”,但后来变为1. delivery
和选择的时间可能不再有效,无论是选择列表和自定义字段标签。
当然,我使用的是woocommerce_checkout_process
钩警告通过用户wc_add_notice()
但即使woocommerce_checkout_fields
钩被触发(这就是我创建的选择列表),在<select>
未更新的值。
我认为有,虽然是只涉及到运输方式,不更新其他结账领域,AJAX调用woocommerce_checkout_fields
钩被触发。
如何更新自定义字段? 我需要一些JS / jQuery的/ AJAX?
或者:可以自定义字段相关的运输方式(并通过AJAX与它得到更新)? 怎么样?
编辑
自定义字段代码:
add_filter( 'woocommerce_checkout_fields', 'fty_filter_checkout_fields' );
function my_filter_checkout_fields($fields) {
$must_deliver = WC()->cart->shipping_total > 0.0; // true=deliver, false=take away
// some complex code to calculate time lists omitted, samples array instead:
$delivery_time_list = array(
"deliver 10:00",
"deliver 11:00",
"deliver 12:00",
"deliver 13:00"
);
$takeaway_time_list = array(
"takeaway 10:00",
"takeaway 10:30",
"takeaway 11:00",
"takeaway 11:30",
"takeaway 12:00",
"takeaway 12:30",
"takeaway 13:00",
"takeaway 13:30"
);
// add the new conditional field
if($must_deliver) {
$fields['my_delivery_datetime'] = array(
'my_delivery_time' => array(
'type' => 'select',
'options' => $delivery_time_list,
'required' => true,
'label' => __('Delivery time')
)
);
} else {
$fields['my_delivery_time'] = array(
'my_delivery_time' => array(
'type' => 'select',
'options' => $takeaway_time_list,
'required' => true,
'label' => __('Take away time')
)
);
}
return $fields;
}
验证码的一个想法:
add_action('woocommerce_checkout_process', 'my_checkout_date_time_validation', 30, 1);
function my_checkout_date_time_validation($doh) {
$time = filter_input(INPUT_POST, 'my_delivery_time');
$shipping = filter_input(INPUT_POST, 'shipping_method', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
if(strpos($time, "deliver")!==FALSE && strpos($shipping[0], "local_pickup")!==FALSE) {
wc_add_notice('Please re-select take away time', 'error');
} else if(strpos($time, "takeaway")!==FALSE && strpos($shipping[0], "distance_based_rate")!==FALSE) {
wc_add_notice('Please re-select delivery time', 'error');
}
}
这里是关于运输方法;
add_action( 'woocommerce_flat_rate_shipping_add_rate', 'add_distance_based_delivery_rate', 10, 2 );
function add_distance_based_delivery_rate( $method, $rate ) {
$new_rate = $rate;
$new_rate['id'] .= ':' . 'distance_based_rate';
$new_rate['label'] = 'delivery'; // Rename to 'Rushed Shipping'.
// incredibly complex code used to calculate delivery costs omitted
$dist_cost = 1000;
$new_rate['cost'] += $dist_cost;
$method->add_rate( $new_rate );
}
感谢名单!
提供的代码主要是未有用......我做了很大的改变和优化。 我的所有代码都评价说,这是对WooCommerce 3+测试和完美的作品。
你将不得不添加 “用来计算省略快递费用极其复杂的代码” ...
1)对于条件CHECKOUT JAVASCRIPT FIELDS实况事件
让客户现场活动(浏览器端)的手的唯一方式是使用Javascript / jQuery的。 因此,这是不容易的,因为WooCommerce已经用了很多的JavaScript / jQuery的/阿贾克斯结帐页面上...
我已经包含的JavaScript代码到挂钩函数,但你应该把它保存在一个单独的文件和注册与经典的WordPress在这个线程注册脚本功能,像这样的脚本文件:
结帐字段:隐藏和显示现有油田
2)利用现有可用的送货方法 (动态价格计算):
你并不需要创建任何运费率。 您可以使用:
- local_pickup可用的方法为你的“TAKE WAY”
- 为您的“交货” flat_rate可用的方法(动态价格计算)
对于每一个航运区,启用设置,并重新命名(标签名称)Woocommerce 2种方法>设置>航运:
对于扁平率可以设定任何最低量(即会被你的计算被覆盖)...
如果更改需要刷新航运缓存数据:禁用,保存并启用,保存当前航运区的方法。
3)节省运输时间订购META DATA:
我添加一些代码,这个和它的保存在2元的自定义字段:
4)在显示器中METABOX所选运输类型和时间(按顺序编辑网页):
我还添加一些代码为。
FINALY这里是代码:
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
// The 2 Options arrays in imput select
$delivery_time_list[''] = $takeaway_time_list[''] = __('Select an hour');
for($h = 10, $i = 0; $i < 8; $i++ ){
if( $i % 2 == 0 ){
$time = $h.':00';
$delivery_time_list[$time] = 'deliver '.$time;
} else {
$time = $h.':30';
$h++;
}
$takeaway_time_list[$time] = 'takeaway '.$time;
}
echo '<div id="delivery_checkout_fields"><h3>' . __('Shipping time options') . '</h3>';
woocommerce_form_field( 'delivery_time', array(
'type' => 'select',
'class' => array('delivery-time form-row-wide'),
'label' => __('Delivery time'),
'options' => $delivery_time_list,
), $checkout->get_value( 'delivery_time' ) );
woocommerce_form_field( 'takeaway_time', array(
'type' => 'select',
'class' => array('takeaway-time form-row-wide'),
'label' => __('Take away time'),
'options' => $takeaway_time_list,
), $checkout->get_value( 'takeaway_time' ) );
echo '</div>';
$required = esc_attr__( 'required', 'woocommerce' );
?>
<script>
jQuery(function($){
var choosenShipMethod = $('input[name^="shipping_method"]:checked').val().split(':')[0], // Choosen shipping method slug
required = '<abbr class="required" title="<?php echo $required; ?>">*</abbr>'; // Required html
// TESTING: displaying in console the choosen shipping
console.log('Chosen shipping: '+choosenShipMethod);
// Function that shows or hide imput select fields
function showHide( actionToDo='show', selector='' ){
if( actionToDo == 'show' )
$(selector).show(function(){
$(this).addClass("validate-required");
$(this).removeClass("woocommerce-validated");
$(this).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
$(selector+' label').append(required);
//console.log('Selector (show): '+selector);
});
else
$(selector).hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$(this).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
$(selector+' label > .required').remove();
//console.log('Selector (hide): '+selector);
});
}
// Initialising at start (Based on the choosen shipping method)
if( choosenShipMethod == 'flat_rate' ) // Choosen "Delivery" (Hidding "Take away")
{
showHide('show','#delivery_time_field' );
showHide('hide','#takeaway_time_field' );
}
else if( choosenShipMethod == 'local_pickup' ) // Choosen "Take away" (Hidding "Delivery")
{
showHide('show','#takeaway_time_field' );
showHide('hide','#delivery_time_field' );
}
else // No shipping choosen yet (Hidding BOTH shipping dropdown hour selectors
{
showHide('hide','#delivery_time_field' );
showHide('hide','#takeaway_time_field' );
$('#delivery_checkout_fields').hide();
}
// When shipping method is changed (Live event)
$( 'form.checkout' ).on( 'change', 'input[name^="shipping_method"]', function() {
var changedShipMethod = $('input[name^="shipping_method"]:checked').val().split(':')[0];
if( changedShipMethod == 'flat_rate' )
{
// Choose "Delivery" | Show "Delivery" and Hide "Take away"
$('#delivery_checkout_fields').show();
showHide('show','#delivery_time_field' );
showHide('hide','#takeaway_time_field' );
}
else if( changedShipMethod == 'local_pickup' )
{
// Choose "Take away" | Show "Take away" and Hide "Delivery"
$('#delivery_checkout_fields').show();
showHide('show','#takeaway_time_field' );
showHide('hide','#delivery_time_field' );
}
console.log("Chosen shipping: "+changedShipMethod);
});
// When an hour is selected (LIVE event)
$('#delivery_checkout_fields select').change( function(){
if( $(this).val() != '')
$(this).parent().removeClass("validate-required");
else
$(this).parent().addClass("validate-required");
console.log("Selector value: "+$(this).val());
});
// "select.shipping_method, input[name^="shipping_method"], #ship-to-different-address input, .update_totals_on_change select, .update_totals_on_change input[type="radio"], .update_totals_on_change input[type="checkbox"]"
//"function (){t.reset_update_checkout_timer(),t.dirtyInput=!1,e(document.body).trigger("update_checkout")}"
});
</script>
<?php
}
// Process the checkout (Checking if required fields are not empty)
add_action('woocommerce_checkout_process', 'ba_custom_checkout_field_process');
function ba_custom_checkout_field_process() {
$delivery_time = $takeaway_time = 0;
if ( $_POST['delivery_time'] ) $delivery_time = 1;
if ( $_POST['takeaway_time'] ) $takeaway_time = 1;
// Only one message is possible for both
if ( ( $delivery_time + $takeaway_time ) == 0 ){
wc_add_notice( __('Please select a <strong>shipping time</strong>.' ), 'error');
}
}
## CALCULATING THE DELIVERY FEE (BASED ON COUNTING THE DIFFERENT DATES For all items) ##
add_filter( 'woocommerce_package_rates', 'custom_shipping_flat_rate_cost_calculation', 10, 2 );
function custom_shipping_flat_rate_cost_calculation( $rates, $package )
{
## --- CALCULATIONS Based on CART DATA (if needed) --- ##
foreach(WC()->cart->get_cart() as $cart_item ):
// HERE your incredibly complex code used to calculate delivery costs
endforeach;
## --- CHANGING DYNAMICALLY THE METHODS COSTS --- ##
foreach($rates as $rate_key => $rate_values):
$method_id = $rate_values->method_id;
$rate_id = $rate_values->id;
// "DELIVERY" - "local_pickup" method (if needed)
if ( 'flat_rate' === $method_id ){
// HERE your incredibly complex code used to calculate delivery costs
// Change the price cost
$price_excl_tax = $rates[$rate_id]->cost + 2.5;
$rates[$rate_id]->cost = number_format($price_excl_tax, 2);
$tax_calculation = $rates[$rate_id]->taxes[0] * 0.1;
$rates[$rate_id]->taxes[0] = number_format($tax_calculation, 2);
}
// "TAKE WAY" - "local_pickup" method (if needed)
elseif ( 'local_pickup' === $method_id )
{
// do something if needed
}
endforeach;
return $rates;
}
// Save the "shipping time" in order meta data
add_action( 'woocommerce_checkout_update_order_meta', 'save_shipping_time_in_order_meta', 100, 1 );
function save_shipping_time_in_order_meta( $order_id ) {
// Take away time
$takeaway_time = $_POST['takeaway_time'];
if ( ! empty( $takeaway_time ) ){
add_post_meta( $order_id, '_shipping_time', $takeaway_time );
add_post_meta( $order_id, '_shipping_type', __('Take away', 'woocommerce' ) );
}
// Delivery time
$delivery_time = $_POST['delivery_time'];
if ( ! empty( $delivery_time ) ){
add_post_meta( $order_id, '_shipping_time', $delivery_time );
add_post_meta( $order_id, '_shipping_type', __('Delivery', 'woocommerce' ) );
}
}
// Adding shipping time metabox (on right side) to Order edit pages
add_action( 'add_meta_boxes', 'add_order_shipping_time_meta_boxe' );
function add_order_shipping_time_meta_boxe(){
add_meta_box(
'woocommerce-order-shipping-time-values', __( 'Shipping type and time', 'woocommerce' ),
'order_shipping_time_values', 'shop_order', 'side', 'default'
);
}
// Adding content to shipping time metabox to Order edit pages
function order_shipping_time_values(){
global $post;
$type = get_post_meta($post->ID, '_shipping_type', true);
$time = get_post_meta($post->ID, '_shipping_time', true);
echo "<p><strong>Type:</strong> $type | <strong>time:</strong> $time</p>";
}
代码放在您的活动子主题(或主题)的function.php文件或也以任何插件文件。
此代码对WooCommerce 3+和工程测试。