首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Woocommerce 3中用购物车项目自定义数据更新订单元

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

Stack Overflow用户
提问于 2018-08-05 22:44:37
回答 1查看 3.3K关注 0票数 1

在woocommerce单页产品上,我创建了一个自定义字段。当订单被验证时,我希望它的值存储在order元中。经过几个小时的研究,我一直未能找到任何解决办法来结束!但我肯定我很接近它..。

以下是我的实际代码:

代码语言:javascript
运行
复制
 // 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'] ) );
   }
  }

下面是,我到目前为止拥有的:

这就是我想要的:

如何将此自定义字段数据保存为订单元数据,以使“自定义字段”元框中的值出现。

任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-06 08:13:45

从woocommerce 3开始,您的代码就有点过时了,并且有一些小错误。

另外,为了回答您的主要问题,您正在尝试将一些“项”数据保存为order元数据,这不是最好的方法,因为您可以为一个订单拥有许多项。

我在下面重新讨论了您的代码:

代码语言:javascript
运行
复制
// 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 ) );
}

代码在您的活动子主题(或活动主题)的function.php文件中。测试和工作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51698790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档