title: WP Import Function Editor - Using PHP to assign product categories
body: <p>I inherited a website which uses WP Import to create/update products in woocommerce and whilst some of the imports are done using the category mapping within the setup, quite a lot of it is done via the Function Editor and php.</p>
  <p>Unfortunately, I know almost nothing about how it works so I'm looking for some help please.</p>
  <p>The outcome I am looking for is that the import is first assigned a category based on the $category mapping, and then via the $productname if it contains keywords.</p>
  <p>I know enough to see that the below, a condensed snippet of the php, is splitting up these two ideas into separate functions. Meaning within the import I have to call a different function depending on whether I want to check product name or category.</p>
  <p>If anyone can help me combine these into one function, where I can build out category mapping based on import category followed by product name I would really appreciate it.</p>
  <pre><code>function join_categories($backupcategory, $category)
  {
      $formattedCurrentCat = strtoupper($category);
      $newCat2 = $category;
      
      if (strpos($formattedCurrentCat, &quot;SOMETHING ABSOLUTELY RIDICULOUS&quot;))
      {
          $newCat2 = &quot;Test Category&quot;;
      }
  else if (strpos($formattedCurrentCat, &quot;SHIRTS &gt; pale green&quot;))
  {
  $newCat2 = &quot;SHIRTS &gt; GREEN&quot;;
  }
  else if (strpos($formattedCurrentCat, &quot;SHIRTS &gt; navy blue&quot;))
  {
  $newCat2 = &quot;SHIRTS &gt; BLUE&quot;;
  }
      else
      {
          $newCat2 = $backupcategory;
      }
      //return $newCat2;
      $newCat2 = explode( &quot;&gt;&quot;, $newCat2 );
      array_walk( $newCat2, 'trim' );
      return array_pop( $newCat2 );
  }
  
  function map_categories($productname, $real_category)
  {
      $formattedCurrentCat = strtoupper($productname);
      $newCat = $productname;
      
      if (strpos($formattedCurrentCat, &quot;green shirt&quot;))
      {
          $newCat = &quot;SHIRTS &gt; GREEN&quot;;
      }
      else if (strpos($formattedCurrentCat, &quot;blue shirt&quot;)
          || (strpos($formattedCurrentCat, &quot;blue&quot;) &amp;&amp; strpos($formattedCurrentCat, &quot;shirt&quot;)))
      {
          $newCat = &quot;SHIRTS &gt; BLUE&quot;;
      }
      else if (strpos($formattedCurrentCat, &quot;shirt&quot;))
      {
          $newCat = &quot;SHIRTS&quot;;
      }
      else
      {
          $newCat = &quot;Other &gt; Misc&quot;;
      }
      //return $newCat;
      $newCat = explode( &quot;&gt;&quot;, $newCat );
      array_walk( $newCat, 'trim' );
      return array_pop( $newCat );
  }
  
  </code></pre>
  <p>Sorry if this is all poorly worded, I'm new to php.</p>