【问题标题】:Woocommerce product notes add to order metaWoocommerce 产品说明添加到订单元
【发布时间】:2016-08-14 18:21:48
【问题描述】:

我创建了 textarea 字段来保存 woocommerce 产品备注,如果购物车中有任何产品可用并且有产品备注,我想将这些备注保存到管理员订单中。

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

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    好的,我找到了解决方案。

    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 );
        	}
       	}
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      • 2018-02-18
      • 2014-08-16
      • 1970-01-01
      相关资源
      最近更新 更多