0

We need to get the text input from Add to cart page in cart_session and display it in the view cart page and save those in database too. I tried to add it in:

$this->cart_contents[$cart_item_key] = apply_filters( 'woocommerce_add_cart_item', array_merge( $cart_item_data, array('' => '','custom_text' => $_POST['custom_txt']) ), $cart_item_key );

It is saving the custom_txt one time and it clears it when the next product is added to cart. I think we need to do something to for save those in session. Please help us.

Howli
  • 12,291
  • 19
  • 47
  • 72
user2002740
  • 1
  • 1
  • 2

1 Answers1

3

I assume you have one (or more) fields in "Single Product" page and you want to insert the values from that (or those) field(s) inside the cart. In my one project I used the following code that might help you. You need to customize as your need.

        // Add to cart
        add_filter( 'woocommerce_add_cart_item', 'woocommerce_add_cart_item_custom' );


        // Add item data to the cart
        add_filter( 'woocommerce_add_cart_item_data', 'woocommerce_add_cart_item_data_custom', 10, 2 );

        // Load cart data per page load
        add_filter( 'woocommerce_get_cart_item_from_session', 'woocommerce_get_cart_item_from_session_custom', 20, 2 );

        // Get item data to display
        add_filter( 'woocommerce_get_item_data', 'woocommerce_get_item_data_custom', 10, 2 );

        // Validate when adding to cart
        add_filter( 'woocommerce_add_to_cart_validation', 'woocommerce_add_to_cart_validation_custom', 10, 3 );

        // Add meta to order
        add_action( 'woocommerce_add_order_item_meta', 'woocommerce_add_order_item_meta_custom', 10, 2 );

        // order again
        add_filter( 'woocommerce_order_again_cart_item_data', 'woocommerce_order_again_cart_item_data', 10, 3 );


    //  add to cart 
    function woocommerce_add_cart_item_custom( $cart_item ) {

        // operation done while item is added to cart.

        return $cart_item;

    }

    //  get cart from session

    function woocommerce_get_cart_item_from_session_custom( $cart_item, $values ) {

        if (!empty($values['cusotm_field'])) :
            $cart_item['cusotm_field'] = $values['cusotm_field'];
            $cart_item = woocommerce_add_cart_item_custom( $cart_item );
        endif;

        return $cart_item;

    }


    //  get item data
    function other_options_get_item_data( $other_data, $cart_item ) {

        if (!empty($cart_item['custom_option'])) :

            $other_data = array(
                'name' => $custom,
                'value' => $value,
                'display' => $custom .' : '.$value
            );

        endif;

        return $other_data;

    }

    function woocommerce_add_cart_item_data_custom($cart_item_meta, $product_id){
        global $woocommerce;

    if(empty($cart_item_meta['custom_field']))
        $cart_item_meta['custom_field'] = array();

$cart_item_meta['custom_field'] = esc_attr($_POST[sanitize_title('name_of_field')]);            

        return $cart_item_meta;

    }


    //  add to order meta

    function woocommerce_add_order_item_meta_custom( $item_id, $values ) {

        if ( ! empty( $values['custom_option'] ) ) {
            woocommerce_add_order_item_meta( $item_id, $option_name, $option_value );           
        }
    }


    //  validation

    function woocommerce_add_to_cart_validation_custom($passed, $product_id, $qty){

        global $woocommerce;

$option = ''; // your custom field's name

   if( isset($_POST[sanitize_title($option)]) && $_POST[sanitize_title($option)] == '' )
        $passed = false;

    if (!$passed)
        $woocommerce->add_error( sprintf( __('"%s" is a required field.', 'woocommerce'), $option) );

        return $passed;

    }

    function woocommerce_order_again_cart_item_data($cart_item_meta, $product, $order){
        global $woocommerce;

    // Disable validation
    remove_filter( 'woocommerce_add_to_cart_validation', array( $this, 'validate_add_cart_item' ), 10, 3 );

        if(empty($cart_item_meta['custom_field']))
            $cart_item_meta['custom_field'] = array();

$cart_item_meta['_other_options']= esc_attr($_POST[sanitize_title('field_name')]);

        return $cart_item_meta;
    }

This is a very general code. you need to modify that best suites you need.

Thanks.

maksbd19
  • 3,785
  • 28
  • 37
  • Thanks. I added a new attribute Customization_text and used that one for getting custom text. – user2002740 Mar 21 '14 at 04:13
  • Thats good, but my code was focusing on something that cant be done by using simple attributes. Anyway thanks – maksbd19 Mar 21 '14 at 08:21
  • In my opinion, this should have been accepted as it answers the original question asked. Though I am curious why you'd disable validation on the order again? – helgatheviking Dec 01 '14 at 12:20
  • Thanks for your comment. As far as I am concerned one would only see the order again button (if the website supports it) only if he/she already ordered the particular product. Since the user has already filled the required fields and they are already validated, they are not required to be validated again. Thats why I have disabled the validation on order again filter. – maksbd19 Feb 25 '15 at 09:04