将自定义投放日期范围计算保存到WooCommerce订单元数据

时间:2017-07-20 12:54:56

标签: php wordpress woocommerce shipping orders

我使用woocommerce并编写了一个功能,根据库存状态显示预期交货的日期范围...
供参考:Display an estimated delivery date range based on WooCommerce cart item stock

现在我想将计算出的日期范围输出到每个订单的管理面板中。

对于Wordpress后端的订单编辑页面,我使用以下代码:

 add_action( 'add_meta_boxes', 'add_meta_boxes' );
function add_meta_boxes() {
    add_meta_box(  'woocommerce-order-my-custom', __( 'Order Custom' ), 
        'order_my_custom', 'shop_order', 'side', 'default' 
    );
}

function order_my_custom(){
  echo "Voraussichtliche Lieferung<br> $from - $to";
}

使用以下代码计算日期范围并显示在前端:

add_filter ( 'woocommerce_cart_collaterals', 'lieferzeit');
add_filter ( 'woocommerce_thankyou_lieferung', 'lieferzeit');
function lieferzeit() {

    $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 { // 1 is Items 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);

    ## OUTPUT ##
    //    echo "Voraussichtliche Lieferung<br> $from - $to";
    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"> ' . $from . ' – ' . $to . ' </div> <div class="tooltip">
  <span class="tooltiptext">Gilt nur bei Lieferungen nach Deutschland.</span></div>' ;
}

我的问题是,函数lieferzeit()的“估计投放范围”数据未出现在函数add_meta_boxes()中。

当提交订单以在相应的订单后端元数据框中显示时,它希望存储“估计的交付范围”数据(函数lieferzeit())。

如果我尝试在感谢页面中添加函数lieferzeit()的输出,它可以工作,但它不存储数据。

那么如何保存并获取计算出的日期范围数据,以便在我需要的任何地方使用它?

由于

2 个答案:

答案 0 :(得分:2)

我在代码中做了一些小改动。

  1. 我已根据产品库存可用性将所有购物车计算代码嵌入到交货范围内的单独功能(非挂钩),我可以在任何地方调用。

  2. 我添加并更改了一些钩子:

    • 用于显示范围的结帐挂钩
    • 一个结帐摘要,用于添加隐藏的输入字段,其中包含 $from $to 值,以便在提交订单时发布该数据。
    • 将在订单元数据中保存交货范围数据的挂钩
  3. 所以你的代码将是:

    // 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>";
    }
    

    代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

    此代码经过测试并正常运行

    您将进入后端订单编辑页面:

    enter image description here

答案 1 :(得分:0)

我建议您在功能前加上前缀以避免冲突。

例如add_action( 'add_meta_boxes', 'cust_add_meta_boxes' );

  

如果有一种存储函数输出的方法会很棒   lieferzeit()的方式不再改变输出。

您可以将其存储为元数据,例如

$order_id = WC()->order->id;
update_post_meta( $order_id, '_lieferzeit_from', $from );
update_post_meta( $order_id, '_lieferzeit_to', $to );

将函数order_my_custom()修改为:

function order_my_custom()
{
   global $post;  
   $from = get_post_meta($post->ID, '_lieferzeit_from', true);
   $to = get_post_meta($post->ID, '_lieferzeit_to', true);
   echo "Voraussichtliche Lieferung<br> $from - $to";
}