简体   繁体   English

将自定义元数据保存到 Woocommerce 订单

[英]Save custom metadata to Woocommerce orders

In WooCommerce i've added 2 fields to a product with Toolset Types.在 WooCommerce 中,我向具有工具集类型的产品添加了 2 个字段。 So when you add a product you can set the expected delivery time in days or weeks.因此,当您添加产品时,您可以将预期交货时间设置为数天或数周。

On the cart and review page i've wrote some code to display the longest delivery time in the totals table.在购物车和评论页面上,我编写了一些代码来显示总计表中的最长交货时间。

前端布局

i've done this with the following code added before the closing table tag in cart-totals.php and review-order.php (ofcourse i've added this to my child theme).我已经在cart-totals.php 和review-order.php 中的结束表标签之前添加了以下代码(当然我已将其添加到我的子主题中)。

     foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
                $product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );

                $amount_of_days = 0;

                $delivery_amount = get_post_meta($product_id, 'wpcf-aantal', true);
                $delivery_week_or_days = get_post_meta($product_id, 'wpcf-dagen-of-weken', true);

                if($delivery_week_or_days == "weken"){
                    $amount_of_days = $delivery_amount * 7;
                } else {
                    $amount_of_days = $delivery_amount;
                }
                $array_of_days[] = $amount_of_days;
    }

    $most_days = max($array_of_days);

    if ($most_days % 7 == 0){
        $most_time_string = $most_days/7 . ' weken';
    } else {
        $most_time_string = $most_days . ' dagen';
    }

    if (!empty($most_days)){

        echo '<tr class="custom-cart-delivery-time">';
            echo '<th>Geschatte levertijd</th>';
            echo '<td>' . $most_time_string. '</td>';
        echo '</tr>';
    }

My question is how do i actually safe this to an order?我的问题是我如何才能真正确保订单安全? I want the estimated shipping time (as example: "3 weeks" or "2 days") to show in the order and invoice.我希望在订单和发票中显示预计的运输时间(例如:“3 周”或“2 天”)。

You could try to use the following (commented):您可以尝试使用以下内容(评论):

// Utility function to get the "most day" value from cart object (cart items)
function get_the_most_day(){
    $array_of_days = []; // Initializing;

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $delivery_amount       = get_post_meta($cart_item['product_id'], 'wpcf-aantal', true);
        $delivery_week_or_days = get_post_meta($cart_item['product_id'], 'wpcf-dagen-of-weken', true);
        $amount_of_days        = 0;

        $array_of_days[] = $delivery_week_or_days == "weken" ? $delivery_amount * 7 : $delivery_amount;
    }
    $most_days = max($array_of_days);

    return $most_days % 7 == 0 ? $most_days / 7 . ' weken' : $most_days . ' dagen'; 
}

// Save the most day in Woocommerce session (on checkout page)
add_action( 'template_redirect', 'save_most_days_in_wc_session' );
function save_most_days_in_wc_session() {
    // Only checkout page
    if( is_checkout() && ! is_wc_endpoint_url() ){
        WC()->session->set( 'most_day', get_the_most_day() );
    }
}

// Save the most day as custom meta data in the order
add_action('woocommerce_checkout_create_order', 'save_the_most_day_as_order_meta_data', 20, 2);
function save_the_most_day_as_order_meta_data( $order, $data ) {
    if( $most_day = WC()->session->get( 'most_day' ) ){
        $order->update_meta_data( '_most_day', $most_day );
    }
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 It should works.它应该有效。


To get this "most day" value (2 ways) :要获得这个“最一天”的价值(2 种方式)

1) From the WC_Order $order variable Object 1) 来自WC_Order $order变量对象

$most_day_value = get_meta( '_most_day' );

2) From the dynamic Order ID variable 2) 来自动态订单 ID 变量

$most_day_value = get_post_meta( $order_id, '_most_day', true );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将产品自定义字段保存为 WooCommerce 管理员手动订单的自定义订单项元数据 - Save product custom-field as custom order item metadata for WooCommerce admin manual orders 将自定义元数据保存到 Shipstation 的 WooCommerce 订单 - Save custom meta data to WooCommerce orders for Shipstation 仅在 WooCommerce 管理订单上显示自定义订单项元数据 - Display custom order item metadata only on WooCommerce admin orders 在 WooCommerce 订单和电子邮件中保存并显示产品自定义元数据 - Save and display product custom meta on WooCommerce orders and emails 在WooCommerce的电子邮件和订单页面中保存并显示产品自定义数据 - Save and display product custom data in emails & orders pages in WooCommerce 保存结帐自定义字段值并在 WooCommerce 管理订单中显示 - Save checkout custom field value and display it in WooCommerce admin orders 保存自定义结帐字段并将其显示在管理员Woocommerce订单中 - Save custom checkout fields and display them in admin Woocommerce Orders 将 WooCommerce 订单的总订单项目量保存为自定义元数据 - Save total order items volume of WooCommerce orders as custom meta data 在 Woocommerce Cart、Checkout 和 Orders 上显示和保存添加的自定义购物车项目数据 - Display and save added custom cart item data on Woocommerce Cart, Checkout and Orders 手动将产品添加到 WooCommerce 管理订单时,将产品自定义元保存为订单项元 - Save product custom meta as order item meta when manually adding products to WooCommerce admin orders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM