0

How add custom column to woocommerce shopping cart and then add info of some input from this column to order, checkout page and to email? Actually i need add friends list from buddypress to each product row(price must depends on how many friends checked). Here i found suggestion, but it`s partly helpfull WooCommerce: Add input field to every item in cart wp community also keep silence http://wordpress.org/support/topic/woocommerce-custom-column-in-cart?replies=1

what i do - its just add list of avalaible friends and no idea how can I save data on update cart or proceed.

if ( bp_has_members( 'user_id=' . bp_loggedin_user_id() ) ){
                        function ggUserFrom(){
                            $arrUsers = array();
                            while ( bp_members() ){
                                bp_the_member();
                                $arrUsers[ bp_get_member_user_nicename() ] = bp_get_member_user_nicename();
                            }
                            return $arrUsers;
                        }

                        echo "<div class='friends-holder'>";
                        foreach ( ggUserFrom() as $friend ){
                            echo '<p><input type="checkbox"  name="cart['.$cart_item_key.'][friendsfromcart]" value="'.$friend.'">
                            <span>'.$friend.'</span></p>';
                        }
                        echo "</div>";
                    }

Im seek ANY info about this question.

Community
  • 1
  • 1
WebArtisan
  • 3,996
  • 10
  • 42
  • 62

3 Answers3

0

Here, since user may choose multiple checkboxes, it may contain multiple values. Therefore, we have used "serialize" function.

for example,

add_filter( 'woocommerce_get_cart_item_from_session', 'wdm_get_cart_items_from_session', 1, 3 ); 

if(!function_exists('wdm_get_cart_items_from_session')) 
{ 
    function wdm_get_cart_items_from_session($item,$values,$key) 
    { 

    $item['custom_field_name'] =  isset( $values['friendsfromcart'] )? serialize($values['friendsfromcart']) : ''; 

        return $item; 
    } 
} 

While you are adding Order meta data, you can fetch individual values and add corresponding keys as follows,

add_action('woocommerce_add_order_item_meta','wdm_add_values_to_order_item_meta',1,2);

    function wdm_add_values_to_order_item_meta($item_id, $values) 
    { 

        $user_custom_values = unserialize($values['friendsfromcart']); 

        if(count($user_custom_values) > 0) 
        { 
        foreach($user_custom_values as $single_value) 
        { 
            wc_add_order_item_meta($item_id,ucfirst($single_value),single_value);    
        } 
        }
       }

since order meta will be sent in E - mail.

Domain
  • 11,562
  • 3
  • 23
  • 44
0

Thank for reply and - yes, variable isset in checkout page, but his empty in

var_dump($_POST)...

 ...[custom_field_name] => 
...

and ofc is empty in email. Perhaps i incorrect send it ?

name="friendsfromcart"

or send values its a array and must send:

name="friendsfromcart[]"

or

name="[friendsfromcart]"

or need session key

name="cart['.$cart_item_key.'][friendsfromcart]" ?
WebArtisan
  • 3,996
  • 10
  • 42
  • 62
  • This should work for you. name="cart['.$cart_item_key.'][friendsfromcart][]" This will create checkbox array. – Domain Jul 25 '14 at 05:23
0

It works, array in cart but something wrong with unserialize:

add_action( 'init', 'update_cart_action', 9);
function update_cart_action() {
global $woocommerce;
if ( ( ! empty( $_POST['update_cart'] ) || ! empty( $_POST['proceed'] ) ) && $woocommerce->verify_nonce('cart')) {
    $cart_totals = isset( $_POST['cart'] ) ? $_POST['cart'] : '';
    if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
            if ( isset( $cart_totals[ $cart_item_key ]['friendsfromcart'] ) ) {
                $woocommerce->cart->cart_contents[ $cart_item_key ]['friendsfromcart'] = $cart_totals[ $cart_item_key ]['friendsfromcart'];
            }
        }
    }
}

}

add_filter( 'woocommerce_get_cart_item_from_session', 'wdm_get_cart_items_from_session', 1, 3 );
function wdm_get_cart_items_from_session($item,$values,$key){
$item['friendsfromcart'] =  isset( $values['friendsfromcart'] )? serialize($values['friendsfromcart']) : '';
return $item;

}

add_action('woocommerce_add_order_item_meta','wdm_add_values_to_order_item_meta',1,2);
function wdm_add_values_to_order_item_meta($item_id, $values){
$user_custom_values = unserialize($values['friendsfromcart']);
if(count($user_custom_values) > 0){
    foreach($user_custom_values as $single_value){
        wc_add_order_item_meta($item_id,ucfirst($single_value),single_value);
    }
}

}

WebArtisan
  • 3,996
  • 10
  • 42
  • 62