简体   繁体   English

使用 Woocommerce 3 中的购物车项目自定义数据更新订单元数据

[英]Update order meta with cart item custom data in Woocommerce 3

On a woocommerce single page product, I have created a custom field.在 woocommerce 单页产品上,我创建了一个自定义字段。 And i want its value to be stored in the order meta, when the order is validated.我希望它的值存储在订单元数据中,当订单被验证时。 After several hours of research i have not been able to find any solution to come to the end !经过几个小时的研究,我一直无法找到任何解决方案来结束! But i'm sure i'm pretty close to it...但我确定我很接近它......

Here is my actual code:这是我的实际代码:

 // create the custom field
  add_action( 'woocommerce_before_add_to_cart_button', 'add_fields_before_add_to_cart', 100 );
  function add_fields_before_add_to_cart($post) {
    ?>
    <select class="selectpicker" name="premiere_box_abo" id="premiere_box_abo">
      <option value="Septembre 2018">Septembre 2018</option>
      <option value="Octobre 2018">Octobre 2018</option>        
    </select>
    <?php
  }

  // Store custom field
  function save_my_custom_checkout_field( $cart_item_data, $product_id ) {
    if( isset( $_POST['premiere_box_abo'] ) ) {
      $cart_item_data[ 'premiere_box_abo' ] = $_POST['premiere_box_abo'];
      /* below statement make sure every add to cart action as unique line item */
      $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    return $cart_item_data;
  }
  add_action( 'woocommerce_add_cart_item_data', 'save_my_custom_checkout_field', 10, 2 );

  // Render meta on cart and checkout
  function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
    $custom_items = array();
    /* Woo 2.4.2 updates */
    if( !empty( $cart_data ) ) {
      $custom_items = $cart_data;
    }
    if( isset( $cart_item['premiere_box_abo'] ) ) {
      $custom_items[] = array( "name" => '1e box abonnement', "value" => $cart_item['premiere_box_abo'] );
    }
    return $custom_items;
  }
  add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );

  // Display as order meta
  add_action( 'woocommerce_add_order_item_meta', 'my_field_order_meta_handler', 1, 3 );
  function my_field_order_meta_handler( $item_id, $values ) {
    if( isset( $values['premiere_box_abo'] ) ) {
      wc_add_order_item_meta( $item_id, "1e box abonnement", $values['premiere_box_abo'] );
    }
  }

 // Update the order meta with field value
 add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
   function my_custom_checkout_field_update_order_meta( $order_id ) {
      if ( ! empty( $_POST['premiere_box_abo'] ) ) {
        update_post_meta( $order_id, 'premiere_box_abo', sanitize_text_field( $_POST['premiere_box_abo'] ) );
   }
  }

And here is What I have so far:这是我到目前为止所拥有的:

到目前为止我所拥有的

That is what I would like :这就是我想要的:

我想要什么

How can I save this custom field data as order meta data to make appear the value in "custom fields" meta box.如何将此自定义字段数据保存为订单元数据,以使“自定义字段”元框中的值出现。

Any help is appreciated.任何帮助表示赞赏。

Since woocommerce 3, you code is a bit outdated and has small mistakes.从 woocommerce 3 开始,你的代码有点过时并且有小错误。

Also to answer your main question, you are trying to save some "item" data as order meta data, which is not the best thing to do as you can have many items for an order.同样为了回答您的主要问题,您正在尝试将一些“项目”数据保存为订单元数据,这不是最好的做法,因为您可以为一个订单提供许多项目。

I have revisited your code below:我在下面重新访问了您的代码:

// Display a custom select field below add to cart
add_action( 'woocommerce_before_add_to_cart_button', 'add_field_before_add_to_cart', 10 );
function add_field_before_add_to_cart() {
    global $product;
    ?>
    <select class="selectpicker" name="premiere_box_abo" id="premiere_box_abo">
        <option value="Septembre 2018">Septembre 2018</option>
        <option value="Septembre 2018">Octobre 2018</option>
    </select>
    <?php
}

// Add select field value as custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_custom_data', 20, 2 );
function add_cart_item_custom_data( $cart_item_data, $product_id ) {
    if( isset( $_POST['premiere_box_abo'] ) ) {
        $cart_item_data['custom_data'] = esc_attr($_POST['premiere_box_abo']);
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    return $cart_item_data;
}

// Display custom cart item data in cart and checkout
add_filter( 'woocommerce_get_item_data', 'display_custom_cart_item_data', 20, 2 );
function display_custom_cart_item_data( $cart_item_data, $cart_item ) {
    if( isset( $cart_item['custom_data'] ) ) {
        $cart_item_data[] = array(
            'name' => __('1e box abonnement'),
            'value' => $cart_item['custom_data']
        );
    }
    return $cart_item_data;
}

// Save / Display custom field value as custom order item meta data
add_action( 'woocommerce_checkout_create_order_line_item', 'custom_field_update_order_item_meta', 20, 4 );
function custom_field_update_order_item_meta( $item, $cart_item_key, $values, $order ) {
    if( isset($values['custom_data']) ){
        $item->update_meta_data( __('1e box abonnement'), $values['custom_data'] );
    }
}

// Save custom fields values as custom order meta data
add_action( 'woocommerce_checkout_create_order', 'my_custom_checkout_field_update_order_meta', 20, 2 );
function my_custom_checkout_field_update_order_meta( $order, $data ) {
    $premiere_box_abo = array();

    // Loop through order items
    foreach( $order->get_items() as $item ){
        // Set each order item '1e box abonnement' in an array
        $premiere_box_abo[] = $item->get_meta( __('1e box abonnement') );
    }
    // Save the data as a coma separated string in order meta data
    if( sizeof($premiere_box_abo) > 0 )
        $order->update_meta_data( 'premiere_box_abo', implode( ',', $premiere_box_abo ) );
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

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

相关问题 将自定义购物车商品值添加到WooCommerce订单商品元数据 - Add a custom cart item value to WooCommerce order item meta data 在 WooCommerce 中添加自定义字段作为购物车项目元和订单项目元 - Add custom fields as cart item meta and order item meta in WooCommerce 将 Woocommerce 购物车商品自定义数据保存为订单商品元数据,在订单和电子邮件中显示 - Save Woocommerce cart item custom data as order item meta data displaying it on orders and emails 更新 WooCommerce 中的自定义订单项元 - Update custom order item meta in WooCommerce 通过使用 Woocommerce 中的自定义元数据的 Get 请求将商品添加到购物车 - Add item to cart through a Get request with custom meta data in Woocommerce 添加到 Woocommerce 的自定义元数据未显示在订单项元数据中 - Custom meta data added to Woocommerce not displayed in order item meta 在 WooCommerce 中添加订单号作为自定义订单商品元数据 - Add order number as custom order item meta data in WooCommerce 在Woocommerce 3中检索自定义订单项目元数据值 - Retrieve custom order item meta data values in Woocommerce 3 Woocommerce 预订添加自定义订单商品元数据 - Woocommerce Booking Add custom order item meta data WooCommerce PDF 水印:使用自定义订单商品元数据 - WooCommerce PDF Watermark: Using custom order item meta data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM