我在WooCommerce中有这个自定义代码,它根据购物车页面,结帐页面和收到订单页面(感谢页面)中的产品可用性显示计算的交货日期范围。
下面的代码工作正常,除了在订单收到页(感谢页)的估计交付范围数据是错误的...
有什么帮助吗?如何在收到订单页面获得正确的预计交货范围数据?
下面是代码:
// The function that calculate the delivery dates range (Not hooked)
function calculate_delivery_range() {
$all_items_in_stock = true; // initializing
// Iterating through cart items (to get the stock info)
foreach (WC()->cart->get_cart() as $cart_item) {
# HANDLING SIMPLE AND VARIABLE PRODUCTS
// Variable products
$variation_id = $cart_item['variation_id'];
if( 0 != $variation_id) {
$variation_obj = new WC_Product_variation($variation_id);
$stock = $variation_obj->get_stock_quantity();
} else {
// Simple products
$product_id = $cart_item['product_id'];
$product_obj = new WC_Product($product_id);
$stock = $product_obj->get_stock_quantity();
}
if( $stock <= 0 ){
// if an item is out of stock
$all_items_in_stock = false;
break; // We break the loop
}
}
// Items "in stock" (1 to 4 week days)
if( $all_items_in_stock ){
for( $start=0, $count=-1 ; $count < 4; $start++ ){
$weekdays = date('w', strtotime("+$start days"));
if( $weekdays > 0 && $weekdays < 6 ){
$count++;
// echo date('D j (w)', strtotime("+$start days")).', ';
if($count == 1){
$from = date('D, d.m.', strtotime("+$start days") );
} elseif($count == 4) {
$to = date('D, d.m.', strtotime("+$start days") );
}
}
}
} else { // One or more Items are Out of stock (14 to 21 week days)
for( $start=0, $count=-1 ; $count < 21; $start++ ){
$weekdays = date('w', strtotime("+$start days"));
if( $weekdays > 0 && $weekdays < 6 ){
$count++;
if($count == 14){
$from = date('D, d.m.', strtotime("+$start days") );
} elseif($count == 21) {
$to = date('D, d.m.', strtotime("+$start days") );
}
}
}
}
## TRANSLATION ##
// DAYS IN ENGLISH (Source)
$days_en = array( 'Mon', 'Tue', 'Wed', 'Thu', 'Fri' );
// TRANSLATE the DAYS in GERMAN (replacement)
$days_ge = array( 'Mo', 'Di', 'Mi', 'Do', 'Fr' );
$from = str_replace($days_en, $days_ge, $from);
$to = str_replace($days_en, $days_ge, $to);
// Return the "from" and "to" values in an array
return array( 'from' => $from, 'to' => $to );
}
// Displaying the dates delivery range in Cart, Checkout and Order reiceived pages
add_filter ( 'woocommerce_cart_collaterals', 'lieferzeit'); // cart page
add_action ( 'woocommerce_review_order_before_payment', 'lieferzeit'); // checkout page
add_action ( 'woocommerce_thankyou', 'lieferzeit'); // Order recieved
function lieferzeit() {
// Calling the "delivery date range calculation"
$days_range = calculate_delivery_range();
## DISPLAYING ##
echo '<i class="shipping_icon fa fa-truck fa-flip-horizontal" aria-hidden="true"></i> <div class="lieferung"> Vorauslichtliche Lieferung </div> <div class="fromto_date"> ' . $days_range['from'] . ' – ' . $days_range['to'] . ' </div> <div class="tooltip">
<span class="tooltiptext">Gilt nur bei Lieferungen nach Deutschland.</span></div>' ;
}
add_action ( 'woocommerce_review_order_before_payment', 'include_delivery_range_hidden_checkout_fields');
function include_delivery_range_hidden_checkout_fields() {
// Calling the "delivery date range calculation"
$days_range = calculate_delivery_range();
// Output hidden imput fields with delivery dates range values
echo '<input type="hidden" name="delivery_range_from" value="'.$days_range['from'].'">
<input type="hidden" name="delivery_range_to" value="'.$days_range['to'].'">';
}
// Save the "delivery range values" in order meta data
add_action( 'woocommerce_checkout_update_order_meta', 'save_delivery_range_in_order_meta', 100, 1 );
function save_delivery_range_in_order_meta( $order_id ) {
$delivery_range_from = $_POST['delivery_range_from'];
if ( ! empty( $delivery_range_from ) )
add_post_meta( $order_id, '_delivery_range_from', $delivery_range_from );
$delivery_range_to = $_POST['delivery_range_to'];
if ( ! empty( $delivery_range_to ) )
add_post_meta( $order_id, '_delivery_range_to', $delivery_range_to );
}
// Adding Delivery range metabox to Order edit pages
add_action( 'add_meta_boxes', 'add_order_delivery_range_meta_boxe' );
function add_order_delivery_range_meta_boxe(){
add_meta_box(
'woocommerce-order-delivery-range-values', __( 'Voraussichtliche Lieferung', 'woocommerce' ),
'order_delivery_range_values', 'shop_order', 'side', 'default'
);
}
// Adding content to Delivery range metabox to Order edit pages
function order_delivery_range_values(){
global $post;
$from = get_post_meta($post->ID, '_delivery_range_from', true);
$to = get_post_meta($post->ID, '_delivery_range_to', true);
echo "<p>$from - $to</p>";
}
以下是一些交货日期变更的截图。
购物车页面上的交货日期范围
结帐页面上的交货日期范围。仍然显示正确。
感谢页的发货日期范围,这里显示错误。
谢谢
1条答案
按热度按时间omqzjyyz1#
问题来自**
woocommerce_thankyou
**操作挂钩中挂钩的函数lieferzeit(),不再获取购物车数据 (因为购物车已在此步骤中删除,在结帐后)。1)* * 删除代码:**(删除代码中的此行):
2)* * 添加代码:**(例如在
save_delivery_range_in_order_meta()
函数之后):此代码经过测试并正常工作