Tuesday, 17 July 2018

Add Custom Fields To Woocommerce Products Single Page, Cart Page, Checkout Page, Order Detaile And Save To Database

<?php
//add custom fields to WooCommerce products Single Page, Cart Page, Checkout Page, Order Detaile And Save To Database Table Name "wp_woocommerce_order_itemmeta"

add_action( 'woocommerce_before_add_to_cart_button', 'add_fields_before_add_to_cart' );
function add_fields_before_add_to_cart(){ ?>
    <table>
        <tr>
            <td>
                <?php _e( "Name:", "aoim"); ?>
            </td>
            <td>
                <input type = "text" name = "customer_name" id = "customer_name" placeholder = "Name on Gift Card">
            </td>
        </tr>
        <tr>
            <td>
                <?php _e( "Gift Price:", "aoim"); ?>
            </td>
            <td>
                <input type = "text" name = "custom_price" id = "custom_price" placeholder = "Your GiftPrice on Gift Card">
            </td>
        </tr>
    </table>
    <?php
}

/**
 * Add data to cart item
 */
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 25, 2 );
function add_cart_item_data( $cart_item_meta, $product_id ) {
    if ( isset( $_POST ['customer_name'] ) && isset( $_POST ['custom_price'] ) ) {
      
        $product = wc_get_product($product_id);      
        $price = $product->get_regular_price();
      
        $custom_data  = array() ;
        $custom_data [ 'customer_name' ]    = isset( $_POST ['customer_name'] ) ?  sanitize_text_field ( $_POST ['customer_name'] ) : "" ;
        $custom_data [ 'custom_price' ] = isset( $_POST ['custom_price'] ) ? sanitize_text_field ( $_POST ['custom_price'] ): "" ;
      
        $custom_data['totalPrice'] = $price + $custom_data [ 'custom_price' ];              
      
        $cart_item_meta ['custom_data']     = $custom_data ;
    }  
    return $cart_item_meta;
}

/**
 * Display custom data on cart and checkout page.
 */
add_filter( 'woocommerce_get_item_data', 'get_item_data' , 25, 2 );
function get_item_data ( $other_data, $cart_item ) {
    if ( isset( $cart_item [ 'custom_data' ] ) ) {
        $custom_data  = $cart_item [ 'custom_data' ];          
        $other_data[] = array( 'name' => 'Name', 'display'  => $custom_data['customer_name'] );
        $other_data[] = array( 'name' => 'GiftPrice', 'display'  => $custom_data['custom_price'] );
    }
    return $other_data;
}

// Set the new calculated price replacing cart item price
add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_calculated_price', 20, 1 );
function set_cart_item_calculated_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
  
    // Loop through cart items
    foreach ( $cart->get_cart() as $key=>$value ){                      
        if( ! isset( $value['custom_data']['totalPrice'] ) ){ continue; }
      
        if( $value['custom_data']['totalPrice'] > 0 ){
            $value['data']->set_price((float)$value['custom_data']['totalPrice']);
        }
    }
}

/**
 * Add order item meta.
*/
add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta' , 10, 2);
function add_order_item_meta ( $item_id, $values ) {
    if ( isset( $values [ 'custom_data' ] ) ) {
        $custom_data  = $values [ 'custom_data' ];
        wc_add_order_item_meta( $item_id, 'Name', $custom_data['customer_name'] );
        wc_add_order_item_meta( $item_id, 'GiftPrice', $custom_data['custom_price'] );
    }
}

15 comments:

  1. dobhran.com Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Thanks

    ReplyDelete
  2. This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. Buy Fake Documents Online

    ReplyDelete
  3. If you set out to make me think today; mission accomplished! I really like your writing style and how you express your ideas. Thank you. Hk

    ReplyDelete
  4. Thanks for sharing the post.. parents are worlds best person in each lives of individual..they need or must succeed to sustain needs of the family. portable ac power

    ReplyDelete
  5. I found that site very usefull and this survey is very cirious, I ' ve never seen a blog that demand a survey for this actions, very curious... The FoxBeast

    ReplyDelete
  6. I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favorites blog site list and will be checking back soon. Please check out my site as well and let me know what you think. my guide on thereviewdaily

    ReplyDelete
  7. I admit, I have not been on this web page in a long time... however it was another joy to see It is such an important topic and ignored by so many, even professionals. I thank you to help making people more aware of possible issues. fly fishing sling packs overview

    ReplyDelete
  8. We have sell some products of different custom boxes.it is very useful and very low price please visits this site thanks and please share this post with your friends. best portable scanner

    ReplyDelete
  9. We have sell some products of different custom boxes.it is very useful and very low price please visits this site thanks and please share this post with your friends. home appliances

    ReplyDelete
  10. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. best deal

    ReplyDelete
  11. Whenever I have some free time, I visit blogs to get some useful info. Today, I found your blog with the help of Google. Believe me; I found it one of the most informative blog. CAMPING SCREEN HOUSE

    ReplyDelete
  12. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. Buying Guide

    ReplyDelete
  13. You have a real talent for writing unique content. I like how you think and the way you express your views in this article. I am impressed by your writing style a lot. Thanks for making my experience more beautiful. Best Air Purifiers

    ReplyDelete
  14. I’ve been surfing online more than 5 hours today, yet I never found any interesting article like yours without a doubt. It’s pretty worth enough for me. Thanks... 15mm silicone beads

    ReplyDelete
  15. thanks for the tips and information..i really appreciate it.. buy mdvp

    ReplyDelete