dongpin4611 2016-08-13 09:08
浏览 32
已采纳

Woocommerce产品说明添加到订单元

I created textarea field to save woocommerce products notes, i want to save these notes into admin order if any product available in cart and have product notes.

// WooCommerce Products Custom Field
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
  global $woocommerce, $post;
  echo '<div class="options_group">';
  // Textarea Field
    woocommerce_wp_textarea_input( 
        array( 
            'id'          => 'product_notes', 
            'label'       => __( 'Product Notes', 'woocommerce' ), 
            'placeholder' => 'Enter product notes here.',
            'desc_tip'    => 'true',
            'description' => __( 'Enter product notes here.', 'woocommerce' ) 
        )
    );
  echo '</div>';  
}
// Save Product notes
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
    // Textarea
    $woocommerce_textarea = $_POST['product_notes'];
    if( !empty( $woocommerce_textarea ) )
        update_post_meta( $post_id, 'product_notes', esc_html( $woocommerce_textarea ) );
}

</div>
  • 写回答

1条回答 默认 最新

  • doumeng9188 2016-08-14 10:21
    关注

    Ok, i find solution.

    add_action( 'woocommerce_checkout_update_order_meta', 'custom_product_notes_order_meta', 10, 2 );
    
    function custom_product_notes_order_meta( $order_id ) {
        global $woocommerce;
        $i=1; //product counter
        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
            $product_id = $cart_item['product_id'];     
            $product_note = 'product_notes_'.$i++;
            if( !empty(get_post_meta( $product_id, 'product_notes', true )) ){
                $product_notes = get_post_meta( $product_id, 'product_notes', true );
                add_post_meta( $order_id, $product_note, $product_notes );
            }
        }
    }

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决