2017-06-12 337 views

回答

2
// Add the field to the product 
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field'); 

function my_custom_checkout_field() { 
    echo '<div id="my_custom_checkout_field"><h4>'.__('Warranty On Your Product:').'</h4>'; 
    echo '<p><label>Product Name</label> <input type="text" name="Product_name"></p>'; 
    echo '<p><label>Product SNO</label> <input type="text" name="SNO_number"></p>'; 
    echo '<p><label>Invoice N0</label> <input type="text" name="Invoice_NO"></p>'; 
    echo '<p><label>Date Of Purchage</label> <input type="text" name="PurchageDate"></p>'; 
    echo '<p><label>Location</label> <input type="text" name="Location"></p>'; 
    echo '<p><label>Product Price </label> <input type="text" name="Product_price"></p>'; 
    echo '<p><label>Brand Warranty</label> <input type="text" name="BrandWarranty"></p>'; 
    echo '<p><label>Customer Name</label> <input type="text" name="customer_name"></p>'; 
    echo '<p><label>Mobile No</label> <input type="text" name="MobileNo"></p>'; 
    echo '</div>'; 
} 

// Store custom field 
function save_my_custom_checkout_field($cart_item_data, $product_id) { 
    if(isset($_REQUEST['Product_name'])) { 
     $cart_item_data['Product_name'] = $_REQUEST['Product_name']; 
     $cart_item_data['SNO_number']  = $_REQUEST['SNO_number']; 
     $cart_item_data['Invoice_NO']  = $_REQUEST['Invoice_NO']; 
     $cart_item_data['PurchageDate'] = $_REQUEST['PurchageDate']; 
     $cart_item_data['Location']  = $_REQUEST['Location']; 
     $cart_item_data['Product_price'] = $_REQUEST['Product_price']; 
     $cart_item_data['BrandWarranty'] = $_REQUEST['BrandWarranty']; 
     $cart_item_data['customer_name'] = $_REQUEST['customer_name']; 
     $cart_item_data['MobileNo']  = $_REQUEST['MobileNo']; 
     /* 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['Product_name']) &&(!empty($cart_item['Product_name']))) { 
     $custom_items[] = array("name" =>'Warranty On Your Product', "value" => '-'); 
     $custom_items[] = array("name" =>'Product name', "value" => $cart_item['Product_name']); 
     $custom_items[] = array("name" =>'SNO', "value" => $cart_item['SNO_number']); 
     $custom_items[] = array("name" =>'Invoice NO', "value" => $cart_item['Invoice_NO']); 
     $custom_items[] = array("name" =>'Purchage Date', "value" => $cart_item['PurchageDate']); 
     $custom_items[] = array("name" =>'Location', "value" => $cart_item['Location']); 
     $custom_items[] = array("name" =>'Product price', "value" => $cart_item['Product_price']); 
     $custom_items[]= array("name" =>'Product name', "value" => $cart_item['BrandWarranty']); 
     $custom_items[] = array("name" =>'customer name', "value" => $cart_item['customer_name']); 
     $custom_items[] = array("name" => 'Mobile No', "value" => $cart_item['MobileNo']); 
    } 
    return $custom_items; 
} 
add_filter('woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2); 

// Display as order meta 
function my_field_order_meta_handler($item_id, $values, $cart_item_key) { 
    if(isset($values['my_field_name'])) { 
     wc_add_order_item_meta($item_id, "Product_name", $values['Product_name']); 
     wc_add_order_item_meta($item_id, "SNO_number", $values['SNO_number']); 
      wc_add_order_item_meta($item_id, "Invoice_NO", $values['Invoice_NO']); 
      wc_add_order_item_meta($item_id, "PurchageDate", $values['PurchageDate']); 
      wc_add_order_item_meta($item_id, "Location", $values['Location']); 
      wc_add_order_item_meta($item_id, "Product_price", $values['Product_price']); 
       wc_add_order_item_meta($item_id, "BrandWarranty", $values['BrandWarranty']); 
       wc_add_order_item_meta($item_id, "customer_name", $values['customer_name']); 
       wc_add_order_item_meta($item_id, "MobileNo", $values['MobileNo']); 
    } 
} 
add_action('woocommerce_add_order_item_meta', 'my_field_order_meta_handler', 1, 3); 

/** THIS IS WHERE I'M STUCK **/ 

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process'); 
function my_custom_checkout_field_process() { 
    global $woocommerce; 

    // Check if set, if its not set add an error. This one is only requite for companies 
    if ($_POST['billing_company']) 
     if (!$_POST['Product_name']) 
      $woocommerce->add_error(__('Please enter your XXX.')); 
} 
// Update the user meta with field value 
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta'); 
function my_custom_checkout_field_update_user_meta($user_id) { 
    if ($user_id && $_POST['Product_name']) 
    update_user_meta($user_id, 'Product_name', esc_attr($_POST['Product_name'])); 
    update_user_meta($user_id, 'SNO_number', esc_attr($_POST['SNO_number'])); 
    update_user_meta($user_id, 'Invoice_NO', esc_attr($_POST['Invoice_NO'])); 
    update_user_meta($user_id, 'PurchageDate', esc_attr($_POST['PurchageDate'])); 
    update_user_meta($user_id, 'Location', esc_attr($_POST['Location'])); 
    update_user_meta($user_id, 'Product_price', esc_attr($_POST['Product_price'])); 
    update_user_meta($user_id, 'BrandWarranty', esc_attr($_POST['BrandWarranty'])); 
    update_user_meta($user_id, 'customer_name', esc_attr($_POST['customer_name'])); 
    update_user_meta($user_id, 'MobileNo', esc_attr($_POST['MobileNo'])); 
} 

// 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 ($_POST['Product_name']) 
    update_post_meta($order_id, 'My Name', esc_attr($_POST['Product_name'])); 
    update_post_meta($order_id, 'SNO Number', esc_attr($_POST['SNO_number'])); 
    update_post_meta($order_id, 'Invoice NO', esc_attr($_POST['Invoice_NO'])); 
    update_post_meta($order_id, 'Purchage Date', esc_attr($_POST['PurchageDate'])); 
    update_post_meta($order_id, 'Location', esc_attr($_POST['Location'])); 
    update_post_meta($order_id, 'Product Price', esc_attr($_POST['Product_price'])); 
    update_post_meta($order_id, 'Brand Warranty', esc_attr($_POST['BrandWarranty'])); 
    update_post_meta($order_id, 'Customer Name', esc_attr($_POST['customer_name'])); 
    update_post_meta($order_id, 'Mobile No', esc_attr($_POST['MobileNo'])); 
} 
2
// Add the field to the product 
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field'); 

function my_custom_checkout_field() { 
    echo '<div id="my_custom_checkout_field"><h4>'.__('Warranty On Your Product:').'</h4>'; 
    echo '<p><label>Product Name</label> <input type="text" name="Product_name"></p>'; 
    echo '<p><label>Product SNO</label> <input type="text" name="SNO_number"></p>'; 
    echo '<p><label>Invoice N0</label> <input type="text" name="Invoice_NO"></p>'; 
    echo '<p><label>Date Of Purchage</label> <input type="text" name="PurchageDate"></p>'; 
    echo '<p><label>Location</label> <input type="text" name="Location"></p>'; 
    echo '<p><label>Product Price </label> <input type="text" name="Product_price"></p>'; 
    echo '<p><label>Brand Warranty</label> <input type="text" name="BrandWarranty"></p>'; 
    echo '<p><label>Customer Name</label> <input type="text" name="customer_name"></p>'; 
    echo '<p><label>Mobile No</label> <input type="text" name="MobileNo"></p>'; 
    echo '</div>'; 
} 

// Store custom field 
function save_my_custom_checkout_field($cart_item_data, $product_id) { 
    if(isset($_REQUEST['Product_name'])) { 
     $cart_item_data['Product_name'] = $_REQUEST['Product_name']; 
     $cart_item_data['SNO_number']  = $_REQUEST['SNO_number']; 
     $cart_item_data['Invoice_NO']  = $_REQUEST['Invoice_NO']; 
     $cart_item_data['PurchageDate'] = $_REQUEST['PurchageDate']; 
     $cart_item_data['Location']  = $_REQUEST['Location']; 
     $cart_item_data['Product_price'] = $_REQUEST['Product_price']; 
     $cart_item_data['BrandWarranty'] = $_REQUEST['BrandWarranty']; 
     $cart_item_data['customer_name'] = $_REQUEST['customer_name']; 
     $cart_item_data['MobileNo']  = $_REQUEST['MobileNo']; 
     /* 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['Product_name']) &&(!empty($cart_item['Product_name']))) { 
     $custom_items[] = array("name" =>'Warranty On Your Product', "value" => '-'); 
     $custom_items[] = array("name" =>'Product name', "value" => $cart_item['Product_name']); 
     $custom_items[] = array("name" =>'SNO', "value" => $cart_item['SNO_number']); 
     $custom_items[] = array("name" =>'Invoice NO', "value" => $cart_item['Invoice_NO']); 
     $custom_items[] = array("name" =>'Purchage Date', "value" => $cart_item['PurchageDate']); 
     $custom_items[] = array("name" =>'Location', "value" => $cart_item['Location']); 
     $custom_items[] = array("name" =>'Product price', "value" => $cart_item['Product_price']); 
     $custom_items[]= array("name" =>'Product name', "value" => $cart_item['BrandWarranty']); 
     $custom_items[] = array("name" =>'customer name', "value" => $cart_item['customer_name']); 
     $custom_items[] = array("name" => 'Mobile No', "value" => $cart_item['MobileNo']); 
    } 
    return $custom_items; 
} 
add_filter('woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2); 

// Display as order meta 
function my_field_order_meta_handler($item_id, $values, $cart_item_key) { 
    if(isset($values['my_field_name'])) { 
     wc_add_order_item_meta($item_id, "Product_name", $values['Product_name']); 
     wc_add_order_item_meta($item_id, "SNO_number", $values['SNO_number']); 
      wc_add_order_item_meta($item_id, "Invoice_NO", $values['Invoice_NO']); 
      wc_add_order_item_meta($item_id, "PurchageDate", $values['PurchageDate']); 
      wc_add_order_item_meta($item_id, "Location", $values['Location']); 
      wc_add_order_item_meta($item_id, "Product_price", $values['Product_price']); 
       wc_add_order_item_meta($item_id, "BrandWarranty", $values['BrandWarranty']); 
       wc_add_order_item_meta($item_id, "customer_name", $values['customer_name']); 
       wc_add_order_item_meta($item_id, "MobileNo", $values['MobileNo']); 
    } 
} 
add_action('woocommerce_add_order_item_meta', 'my_field_order_meta_handler', 1, 3); 

/** THIS IS WHERE I'M STUCK **/ 

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process'); 
function my_custom_checkout_field_process() { 
    global $woocommerce; 

    // Check if set, if its not set add an error. This one is only requite for companies 
    if ($_POST['billing_company']) 
     if (!$_POST['Product_name']) 
      $woocommerce->add_error(__('Please enter your XXX.')); 
} 
// Update the user meta with field value 
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta'); 
function my_custom_checkout_field_update_user_meta($user_id) { 
    if ($user_id && $_POST['Product_name']) 
    update_user_meta($user_id, 'Product_name', esc_attr($_POST['Product_name'])); 
    update_user_meta($user_id, 'SNO_number', esc_attr($_POST['SNO_number'])); 
    update_user_meta($user_id, 'Invoice_NO', esc_attr($_POST['Invoice_NO'])); 
    update_user_meta($user_id, 'PurchageDate', esc_attr($_POST['PurchageDate'])); 
    update_user_meta($user_id, 'Location', esc_attr($_POST['Location'])); 
    update_user_meta($user_id, 'Product_price', esc_attr($_POST['Product_price'])); 
    update_user_meta($user_id, 'BrandWarranty', esc_attr($_POST['BrandWarranty'])); 
    update_user_meta($user_id, 'customer_name', esc_attr($_POST['customer_name'])); 
    update_user_meta($user_id, 'MobileNo', esc_attr($_POST['MobileNo'])); 
} 

// 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 ($_POST['Product_name']) 
    update_post_meta($order_id, 'My Name', esc_attr($_POST['Product_name'])); 
    update_post_meta($order_id, 'SNO Number', esc_attr($_POST['SNO_number'])); 
    update_post_meta($order_id, 'Invoice NO', esc_attr($_POST['Invoice_NO'])); 
    update_post_meta($order_id, 'Purchage Date', esc_attr($_POST['PurchageDate'])); 
    update_post_meta($order_id, 'Location', esc_attr($_POST['Location'])); 
    update_post_meta($order_id, 'Product Price', esc_attr($_POST['Product_price'])); 
    update_post_meta($order_id, 'Brand Warranty', esc_attr($_POST['BrandWarranty'])); 
    update_post_meta($order_id, 'Customer Name', esc_attr($_POST['customer_name'])); 
    update_post_meta($order_id, 'Mobile No', esc_attr($_POST['MobileNo'])); 
} 
相关问题