Adding Variation Details to WooComerce Checkout

Posted about 9 years ago by Morgan Creech

Post a topic
Answered
M
Morgan Creech

Just a bit of code that might be helpful to someone else.

The following goes in your theme's function php file:

 

add_filter( 'woocommerce_get_item_data', 'wc_checkout_description_tp', 10, 2 );
function wc_checkout_description_tp( $other_data, $cart_item ){
    //re-convert the fpd dtails from the json sting to an array
    $fpd_detail_arr = json_decode( html_entity_decode( stripslashes( $cart_item['fpd_data']['fpd_product'] ) ), true );
    //remove the first element as we are not dealing with shadows
    $fpd_elems_arr = array_slice($fpd_detail_arr[0]['elements'], 1);

    //loop each element and set the details to a var
    foreach($fpd_elems_arr as $elem_key => $element_arr){
        foreach($element_arr as $key => $value){
            //the currentColor value is set inside yet another array
            if(is_array($value)){
                foreach($value as $val_key => $val_val){
                    if( $val_key == 'currentColor' && !empty($val_val) ){
                        $fpd_detail_value = $val_val;
                    }
                }

            }
            //set the title value
            else{
                if( $key == 'title' && !empty($value) ){
                    $fpd_detail_name = trim($value);
                }
            }
        }
       //add the name and value pair to the detail array
        $other_data[] = array(
            'name' => $fpd_detail_name,
            'value' => $fpd_detail_value
        );

    }
    //return the final detail array
    return $other_data;
}

 

0 Votes

M

Morgan Creech posted almost 9 years ago Best Answer

This is the code to get the description of your products in the cart. See attached for how it looks on my website. 

0 Votes


9 Comments

Sorted by
G

Gregory St. Gelais posted about 7 years ago

How would you get the description on the admin order page?

 

0 Votes

Roger Beaujard

Roger Beaujard posted over 8 years ago

@Mark I have a solution to limit the output and also a bonus to swap out the hex vals for color names. First to limit what elements are shown simply add an if statement to the last block. Like so:  

       //add the name and value pair to the detail array
		if($fpd_detail_name == 'T-Shirt') {
			$other_data[] = array(
				'name' => $fpd_detail_name,
				'value' => $fpd_detail_value
			);
		}

You'l want to pay attention to the part: 

if($fpd_detail_name == 'T-Shirt') {

You can change "T-Shirt" to whatever you want to KEEP. Or you could use a != and EXCLUDE "T-Shirt" and keep everything else. I suppose you could build a switch if you'd like to keep more than one thing but I'm sure you can see what's happening here.


Bonus: Here is some code you can use to switch out the hex vals for color names. Add this to the block just above the last one: 

            //the currentColor value is set inside yet another array
            if(is_array($value)){
                foreach($value as $val_key => $val_val){
                    if( $val_key == 'currentColor' && !empty($val_val) ){
                        $fpd_detail_value = $val_val;
						// *** Swap out Hex for Color Names ***
						switch ($fpd_detail_value) {
							case "#000000":
								$fpd_detail_value = "Black";
								break;
							case "#ffffff":
								$fpd_detail_value = "White";
								break;
							case "#ff0000":
								$fpd_detail_value = "Red";
								break;
							case "#cccccc":
								$fpd_detail_value = "Heather";
								break;
							case "#ffff00":
								$fpd_detail_value = "Yellow";
								break;
							case "#003366":
								$fpd_detail_value = "Navy";
								break;
							case "#33cc33":
								$fpd_detail_value = "Green";
								break;
						}
                    }
                }

The custom parts are after "*** Swap out Hex for Color Names ***". Here we are just checking and resetting the final var to what we want. 


Hope this helps some peeps ;) 


-RJB

0 Votes

M

Mark posted over 8 years ago

Got this working. My problem is that it shows all the elements from the canvas. in my shopping cart. How can I change it that it only shows selected variation?

0 Votes

M

Mark posted over 8 years ago

Any updates with this after 7 months?  I couldn't get this working

0 Votes

D

Davorin Savovic posted over 8 years ago

Fantastic! Thank you life-saver!!!

0 Votes

C

Charles Heller posted almost 9 years ago

Is there anyway that inserting this code into the theme's function php file could crash my website, or are the two events entirely unrelated...?


 

0 Votes

rady kal

rady kal posted almost 9 years ago Admin

Thanks Morgan for sharing. Good approach!

0 Votes

M

Morgan Creech posted almost 9 years ago Answer

This is the code to get the description of your products in the cart. See attached for how it looks on my website. 

0 Votes

a

alexus posted almost 9 years ago

Good day! Can you explain what this code for?

 

0 Votes

Login or Sign up to post a comment