if title contains string ‘aaa’,’bbb’ or ,’ccc’ replace field {category[1}

Hello gods of Stackoverflow,

Now i hate to be “that guy” who didnt search properly but i am running into a problem that i need a fix for and can’t find a solution i can work with because of my lack in coding skills, my knowledge barely tickles the surface.

Read More

Here’s the thing.

I am using a tool to import feeds into my website (WP all Import) as woocommerceproducts. But in the categorization the feed suppliers made errors which i want to tackle without emailing them everytime i stumble upon one.

i.e: the title contains words like satchel, bag, clutch etc but the product is categorized as ‘jewellery > earrings’ in the CSV or XML.

The import tool will ask me where to find the product category, i point it to the node {category[1]}

But when the category is missing or faulty i want it to check the title for certain words, and if present change the value of it to that found word.

something like: 

[if ({title[1]}contains "satchel") {
    category = "bags > satchel",
} else if ({title[1]} contains clutch) {
    category = "bags > clutch",
} else {
    category = {sub_category[1]} #the normal value if nothing was found 
}]

I just can’t find the pieces to put the formatting together. I might need to work towards a function that i could expand to generate categories based solely out of presence of certain words in the title but maybe when i get better that would be an option.

I hope i was able to provide a clear view on the problem. The “[ ]” is there because thats how the plugin wants code to be entered instead of a {fieldname[1]}, another example below:

The following was an example of a problem i was able to fix:
i needed to replace values like “0/3months/1/2months” to “0-3 months/1-2months” before i replaced the slash”/” with a pipe”|” for wordpress to recognize it as a seperate value.

[str_replace("/","|",
   str_replace("0/3","0-3",
     str_replace("1/2","1-2",
       str_replace("3/6","3-6",{size[1]}))))]

The fields can also be used to call functions but only in the ‘pro’ version of the plugin.

Any help is very much appreciated, thanks in advance.

Related posts

2 comments

  1. So after an afternoon of poking around, testing stuff, and not knowing alot about php i came up with a solution with help from a friend.

    Wp All Import does not allow custom php directly from the field itself, it does however support custom php functions and provides an editor for these on the bottom of the import configuration page. I did the following:

    <?php
    
    
        //Checks Title for keywords and 
        //uses those to create categories
        //We are using our own Main categories so only 
        //sub and subsub categroies need to be created.
        //Call function
        [get_subcat_from_title({title[1]},{category[1]})]
        //Function
        function get_subcat_from_title($title,$defaultcat)
        {    
            if (strpos($title,"Satchel") !== false) {   
                $cat = "Tassen";    
            } elseif (strpos($title,"Travel") !== false) {      
                $cat = "Tassen";     
            } elseif (strpos($title,"Gusset") !== false) {      
                $cat = "Tassen";        
            } else { 
                $cat = $defaultcat;
            }
            return $cat;
        }
    
    
        //Checks Title for keywords and uses those to create subcategories
        //Call Function
        [get_subsubcat_from_title({title[1]},{sub_category[1]})]
        //Function
        function get_subsubcat_from_title($title,$defaultcat)
        {    
            if (strpos($title,"Satchel") !== false) {   
                $cat = "Satchel";   
            } elseif (strpos($title,"Travel") !== false) {      
                $cat = "Travel";     
            } elseif (strpos($title,"Gusset") !== false) {      
                $cat = "Gusset";        
            } else { 
                $cat = $defaultcat;
            }
            return $cat;
        }
    
    ?>
    

    On the Taxonomies, Tags, Categories option we can create our own hierarchical order like this:

    [main category]
    +[sub category]
    ++[sub sub category]
    

    The main category field is given a name we use as our main category.
    The sub category is filled with function SUBCAT
    The subsub category is filled with function SUBSUBCAT

    this way it will create a parent by our own name, a child that has the named ‘Tassen‘ if any keyword is present, and a grandchild with the specific keyword as it’s name.

    This way i can expand the function using all kinds of statements when the right category isn’t present in de provided feed.

    thanks @sebastianForsberg for responding.

    I hope this helps anyone who comes across a similar problem in the future..

Comments are closed.