Convert Attribute to Uppercase
-
Hi Michel
In our feed we combine two attributes called attribute_pa_band and attribute_pa_cup into the Size field. Currently the latter attribute is a letter and this is output in lowercase, we’d like to change this to upper case.
I’d appreciate your assistance with the appropriate code to accomplish this.
Thanks
Lorian
-
Hello Lorian,
You can use the wppfm_feed_item_value filter to do this. See https://gist.github.com/Auke1810/c62bb043926f539f9a99b418c06a3e6e for how to use this filter.
In the filter function you could use the strtoupper() function to change the letter to upper case.
I’m struggling with the correct syntax for this to work.
Would you be able to provide an example please?Thanks
No problem, but I’m not in today and tomorrow so I please be patient until Thursday.
Hello @atheda,
Let me see if I can help you out so you can get started.
I have added a GIST with a script you could use.GIST: Convert Attribute to Uppercase
Add it to your functions.php
1. In your feed add custom_label_0 and custom_label_1
2. Add the source attribute_pa_band and attribute_pa_cup to them.The script will than convert attribute_pa_cup to uppercase and combine them in the size attribute.
Test it in your setup and let me know if this is helpfull.
Hey @atheda, have you had the change to look at the script and tried it out?!
/** * Alter Product feed item * Uppercase a source value (attribute_pa_cup) and combine with an other source value in the size attribute */ function alter_feed_item( $attributes, $feed_id, $product_id ) { // get values $attribute_pa_band = $attributes['custom_label_0']; //add the attribute_pa_band to attribute "custom_label_0" $attribute_pa_cup = $attributes['custom_label_1']; //add the attribute_pa_cup to attribute "custom_label_1" // uppercase attribute_pa_cup $attribute_pa_cup = strtoupper($attribute_pa_cup); //make upper case //combine and add to the size attribute $attributes['size'] = $attribute_pa_band . " " . $attribute_pa_cup; // IMPORTANT! Always return the $attributes return $attributes; } add_filter( 'wppfm_feed_item_value', 'alter_feed_item', 10, 3 );-
This reply was modified 7 years, 10 months ago by
AukeJomm.
Hi guys
Sorry for the delay coming back to you.
Yes this code does work, thank you. However it introduces a small problem.The shop sells lingerie, whilst the Bra sizes are now correct (eg 30 DD), the addition of this code means that regular sizes for other garments (Knickers, Suspender Belts etc) no longer show. Sizes for these items now appear blank in our feed.
Is there a way to code it so that it only applies to items which exist in the ‘Bras’ category?
Hello @atheda,
yes, there should be a check if the custom_label_0 and custom_label_1 contain values
SOmthing like this:<?php /** * Alter Product feed item * Uppercase a source value (attribute_pa_cup) and combine with an other source value in the size attribute */ function alter_feed_item( $attributes, $feed_id, $product_id ) { // get values $attribute_pa_band = $attributes['custom_label_0']; //add the attribute_pa_band to attribute "custom_label_0" $attribute_pa_cup = $attributes['custom_label_1']; //add the attribute_pa_cup to attribute "custom_label_1" // uppercase attribute_pa_cup $attribute_pa_cup = strtoupper($attribute_pa_cup); //make upper case //combine and add to the size attribute BUT only if custom_label_0 and custom_label_1 contain values. if( !empty($attributes['custom_label_0'] ) && !empty($attributes['custom_label_1'] ) ){ $attributes['size'] = $attribute_pa_band . " " . $attribute_pa_cup; } // IMPORTANT! Always return the $attributes return $attributes; } add_filter( 'wppfm_feed_item_value', 'alter_feed_item', 10, 3 );https://gist.github.com/Auke1810/df975590ff5c841c792580bb5d176c26
That works perfectly, thank you.
Great it worked for you @atheda.
Let me know if you have anything else. -
This reply was modified 7 years, 10 months ago by
The topic ‘Convert Attribute to Uppercase’ is closed to new replies.