jarvis Posted February 3, 2016 Share Posted February 3, 2016 Hi All, Arrays seem to be my downfall so hope someone can help! I have the following: $pa_no_of_doors_value = array_shift( wc_get_product_terms( 1008, 'pa_no-of-doors', array( 'fields' => 'names' ) ) ); echo '<br/>No Of Doors: '.$pa_no_of_doors_value.'<br/>'; $pa_no_of_doors['type'] = array( 'name' => htmlspecialchars(stripslashes('No Of Doors')), 'value' => $pa_no_of_doors_value, 'position' => 1, 'is_visible' => 1, 'is_variation' => 0, 'is_taxonomy' => 0 ); print_r($pa_no_of_doors); echo '<p> </p>'; $pa_car_make_value = array_shift( wc_get_product_terms( $post_id, 'pa_car-make', array( 'fields' => 'names' ) ) ); echo '<br/>Car Make: '.$pa_car_make_value.'<br/>'; $pa_car_make['type'] = array( 'name' => htmlspecialchars(stripslashes('Car Make')), 'value' => $pa_car_make_value, 'position' => 1, 'is_visible' => 1, 'is_variation' => 0, 'is_taxonomy' => 0 ); print_r($pa_car_make); I then use: update_post_meta($post_id, '_product_attributes', $pa_car_make); The above then updates the DB (based on Wordpress). As you can see, this is only updating with one of the options. What I'm trying to do is a foreach loop (as I've got many more add, then pass all of them into the _product_attributes field Having added them via Wordpress and looked at the DB, I can see how it needs to then appear: a:2:{ s:8:"car-make";a:6:{s:4:"name";s:8:"Car Make";s:5:"value";s:12:"Aston Martin";s:8:"position";s:1:"0";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:0;} s:14:"pa_no-of-doors";a:6:{s:4:"name";s:14:"pa_no-of-doors";s:5:"value";s:0:"";s:8:"position";s:1:"1";s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;} } So thinking something like: update_post_meta($post_id, '_product_attributes', serialize( $attributes )); But my brain just can't work out how to do a foreach of the various types! Sorry for asking a probably simple question!! Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 3, 2016 Share Posted February 3, 2016 First I would imagine that the "1008" found in the line below: $pa_no_of_doors_value = array_shift( wc_get_product_terms( 1008, 'pa_no-of-doors', array( 'fields' => 'names' ) ) ); ...is going to be the same as $post_id here: $pa_car_make_value = array_shift( wc_get_product_terms( $post_id, 'pa_car-make', array( 'fields' => 'names' ) ) ); Assuming that's correct, you'll want to use the variable instead of hard coding the number. As for the loop, what are you trying to loop through? A series of post IDs? Or does wc_get_product_terms() in the following code pass back multiple values that you need to process? $pa_no_of_doors_value = array_shift( wc_get_product_terms( 1008, 'pa_no-of-doors', array( 'fields' => 'names' ) ) ); $pa_car_make_value = array_shift( wc_get_product_terms( $post_id, 'pa_car-make', array( 'fields' => 'names' ) ) ); Quote Link to comment Share on other sites More sharing options...
jarvis Posted February 3, 2016 Author Share Posted February 3, 2016 Hi cyberRobot Thanks for the reply. Basically, I'm trying to grab the values from a submitted form and pass them into an array. I'm making progress and now have: $attributes = array( 'pa_car-make' => array_shift( wc_get_product_terms( $post_id , 'pa_car-make', array( 'fields' => 'names' ) ) ), 'pa_no-of-doors' => array_shift( wc_get_product_terms( $post_id, 'pa_no-of-doors', array( 'fields' => 'names' ) ) ) ); ?><pre><?php print_r($attributes); ?></pre> <?php echo '<hr/>'; $i = 0; // Loop through the attributes array foreach ($attributes as $name => $value) { $product_attributes[$i] = array ( 'name' => htmlspecialchars( stripslashes( $name ) ), // set attribute name 'value' => $value, // set attribute value 'position' => 1, 'is_visible' => 1, 'is_variation' => 0, 'is_taxonomy' => 0, 'row' => $i ); $i++; ?><pre><?php print_r($product_attributes); ?></pre> <?php } But for some odd reason, it shows the first row twice: Array ( [1] => Array ( [name] => pa_car-make [value] => Aston Martin [position] => 1 [is_visible] => 1 [is_variation] => 0 [is_taxonomy] => 0 [row] => 1 ) ) Array ( [1] => Array ( [name] => pa_car-make [value] => Aston Martin [position] => 1 [is_visible] => 1 [is_variation] => 0 [is_taxonomy] => 0 [row] => 1 ) [2] => Array ( [name] => pa_no-of-doors [value] => 3 Doors [position] => 1 [is_visible] => 1 [is_variation] => 0 [is_taxonomy] => 0 [row] => 2 ) ) Which I can't fathom out!? oh and it was my error on the post_id / 1008 you pointed out, it was from me testing Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 3, 2016 Share Posted February 3, 2016 But for some odd reason, it shows the first row twice: You need to move the following print_r() function outside of the foreach loop: <pre><?php print_r($product_attributes); ?></pre> Quote Link to comment Share on other sites More sharing options...
jarvis Posted February 3, 2016 Author Share Posted February 3, 2016 Ah! Of course - thank you With regards to: $attributes = array( 'pa_car-make' => array_shift( wc_get_product_terms( $post_id , 'pa_car-make', array( 'fields' => 'names' ) ) ), 'pa_no-of-doors' => array_shift( wc_get_product_terms( $post_id, 'pa_no-of-doors', array( 'fields' => 'names' ) ) ) ); Is it possible to check if the value is empty? As I know the following isn't possible: $attributes = array( if(isset($_POST['item_meta'][132])){ $pa_no_of_doors_value = array_shift( wc_get_product_terms( $post_id, 'pa_no-of-doors', array( 'fields' => 'names' ) ) ); if(isset($_POST['item_meta'][242])){ $pa_car_make_value = array_shift( wc_get_product_terms( $post_id, 'pa_car-make', array( 'fields' => 'names' ) ) ); } ); Thanks Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 3, 2016 Share Posted February 3, 2016 Is it possible to check if the value is empty? You could try something like this: if(empty($attributes['pa_car-make'])) { Quote Link to comment Share on other sites More sharing options...
jarvis Posted February 3, 2016 Author Share Posted February 3, 2016 Thanks again. Using: $attributes = array( if(empty($attributes['pa_no-of-doors'])) { 'no-of-doors' => $pa_no_of_doors_value = array_shift( wc_get_product_terms( $post_id, 'pa_no-of-doors', array( 'fields' => 'names' ) ) ); } if(empty($attributes['pa_car-make'])) { 'car-make' => $pa_car_make_value = array_shift( wc_get_product_terms( $post_id, 'pa_car-make', array( 'fields' => 'names' ) ) ); } ); Gives an error: Parse error: syntax error, unexpected 'if' (T_IF), expecting ')' Which points to line: if(empty($attributes['pa_no-of-doors'])) { But I've a feeling I've just misunderstood? Thanks again and apologies Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 3, 2016 Share Posted February 3, 2016 Sorry about the confusion, the $attributes array doesn't exist until after the following block of code: $attributes = array( 'pa_car-make' => array_shift( wc_get_product_terms( $post_id , 'pa_car-make', array( 'fields' => 'names' ) ) ), 'pa_no-of-doors' => array_shift( wc_get_product_terms( $post_id, 'pa_no-of-doors', array( 'fields' => 'names' ) ) ) ); So the if empty test needs to take place after the array is created. Assuming that you only want the foreach loop to execute if the "pa_car-make" value isn't empty, you could try something like this: if(!empty($attributes['pa_car-make'])) { $i = 0; // Loop through the attributes array foreach ($attributes as $name => $value) { $product_attributes[$i] = array ( 'name' => htmlspecialchars( stripslashes( $name ) ), // set attribute name 'value' => $value, // set attribute value 'position' => 1, 'is_visible' => 1, 'is_variation' => 0, 'is_taxonomy' => 0, 'row' => $i ); $i++; } } Quote Link to comment Share on other sites More sharing options...
Solution cyberRobot Posted February 3, 2016 Solution Share Posted February 3, 2016 (edited) Looking at your code a bit closer, you probably want to run the test within the loop. Maybe something like this: foreach ($attributes as $name => $value) { if(!empty($value)) { $product_attributes[$i] = array ( 'name' => htmlspecialchars( stripslashes( $name ) ), // set attribute name 'value' => $value, // set attribute value 'position' => 1, 'is_visible' => 1, 'is_variation' => 0, 'is_taxonomy' => 0, 'row' => $i ); $i++; } } That way one of your attributes can be empty and the loop will still execute. Edited February 3, 2016 by cyberRobot Quote Link to comment Share on other sites More sharing options...
jarvis Posted February 3, 2016 Author Share Posted February 3, 2016 Oh I see, yes that makes more sense. As I need to set up loads of attributes, I need to test which ones have a value and only add them into the main loop if that makes sense? So it's not just testing 1 input field Quote Link to comment Share on other sites More sharing options...
jarvis Posted February 3, 2016 Author Share Posted February 3, 2016 Thanks @cyberRobot That seems to have sorted it! Final code: $pa_no_of_doors_value = array_shift( wc_get_product_terms( $post_id, 'pa_no-of-doors', array( 'fields' => 'names' ) ) ); $pa_car_make_value = array_shift( wc_get_product_terms( $post_id, 'pa_car-make', array( 'fields' => 'names' ) ) ); $attributes = array( 'pa_car-make' => $pa_no_of_doors_value, 'pa_no-of-doors' => $pa_car_make_value, ); $i = 0; // Loop through the attributes array foreach ($attributes as $name => $value) { if(!empty($value)) { $product_attributes[$i] = array ( 'name' => htmlspecialchars( stripslashes( $name ) ), // set attribute name 'value' => $value, // set attribute value 'position' => 1, 'is_visible' => 1, 'is_variation' => 0, 'is_taxonomy' => 0 ); $i++; } } print_r($product_attributes); Thanks for all your help! Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 3, 2016 Share Posted February 3, 2016 No problem; glad to help! For what it's worth, you could eliminate some duplication by calling wc_get_product_terms() in a loop: $taxonomyOfInterest = array( 'pa_car-make', 'pa_no-of-doors' ); $attributes = array(); foreach($taxonomyOfInterest as $currTaxonomy) { $attributes[$currTaxonomy] = array_shift( wc_get_product_terms( $post_id , $currTaxonomy, array( 'fields' => 'names' ) ) ); } Quote Link to comment Share on other sites More sharing options...
jarvis Posted February 3, 2016 Author Share Posted February 3, 2016 oh wow! That's really helpful, thanks so much! Quote Link to comment Share on other sites More sharing options...
jarvis Posted February 4, 2016 Author Share Posted February 4, 2016 @cyberRobot Thanks again for your help yesterday! I know the last post you sent was to help prevent repetition by using this code: $taxonomyOfInterest = array( 'pa_car-make', 'pa_no-of-doors', ); $attributes = array(); foreach($taxonomyOfInterest as $currTaxonomy) { $attributes[$currTaxonomy] = array_shift( wc_get_product_terms( $post_id , $currTaxonomy, array( 'fields' => 'names' ) ) ); } I've managed to get a loop of all the attributes I need that are being manually typed in the first part: $taxonomyOfInterest = array( 'pa_car-make', 'pa_no-of-doors', ); This block of code produces the inner part: $attribute_taxonomies = wc_get_attribute_taxonomies(); if ( $attribute_taxonomies ) : foreach ($attribute_taxonomies as $tax) : echo esc_html(wc_attribute_taxonomy_name($tax->attribute_name)).'<br/>'; endforeach; endif; Which produces: pa_car-makepa_no-of-doors And any others in the CMS, so negates having to add them into the code manually. However, I'm struggling to see how I can effectively do this: $taxonomyOfInterest = array( $attribute_taxonomies = wc_get_attribute_taxonomies(); if ( $attribute_taxonomies ) : foreach ($attribute_taxonomies as $tax) : esc_html(wc_attribute_taxonomy_name($tax->attribute_name)) endforeach; endif; ); Or is this not possible? Thanks again Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 5, 2016 Share Posted February 5, 2016 You could try something like this: $taxonomyOfInterest = array(); $attribute_taxonomies = wc_get_attribute_taxonomies(); if ( $attribute_taxonomies ) : foreach ($attribute_taxonomies as $tax) : $taxonomyOfInterest[] = esc_html(wc_attribute_taxonomy_name($tax->attribute_name)); endforeach; endif; Quote Link to comment Share on other sites More sharing options...
jarvis Posted February 5, 2016 Author Share Posted February 5, 2016 Thanks cyberRobot! I came up with this: $attribute_taxonomies = wc_get_attribute_taxonomies(); if ( $attribute_taxonomies ) : $taxonomyOfInterest = array(); foreach ($attribute_taxonomies as $tax=>$tax_value) : #echo "Key=" . $tax . ", Value=" . $tax_value->attribute_name.'<br/>'; #debug $taxonomyOfInterest[] = 'pa_'.$tax_value->attribute_name; endforeach; endif; #print_r($taxonomyOfInterest); Is this acceptable or have I overcomplicated something? Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 5, 2016 Share Posted February 5, 2016 Seems fine to me Quote Link to comment Share on other sites More sharing options...
jarvis Posted February 5, 2016 Author Share Posted February 5, 2016 Thanks again and apologies for troubling you! Have a good day!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.