'WP Import Function Editor - Using PHP to assign product categories
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.
Unfortunately, I know almost nothing about how it works so I'm looking for some help please.
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.
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.
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.
function join_categories($backupcategory, $category)
{
$formattedCurrentCat = strtoupper($category);
$newCat2 = $category;
if (strpos($formattedCurrentCat, "SOMETHING ABSOLUTELY RIDICULOUS"))
{
$newCat2 = "Test Category";
}
else if (strpos($formattedCurrentCat, "SHIRTS > pale green"))
{
$newCat2 = "SHIRTS > GREEN";
}
else if (strpos($formattedCurrentCat, "SHIRTS > navy blue"))
{
$newCat2 = "SHIRTS > BLUE";
}
else
{
$newCat2 = $backupcategory;
}
//return $newCat2;
$newCat2 = explode( ">", $newCat2 );
array_walk( $newCat2, 'trim' );
return array_pop( $newCat2 );
}
function map_categories($productname, $real_category)
{
$formattedCurrentCat = strtoupper($productname);
$newCat = $productname;
if (strpos($formattedCurrentCat, "green shirt"))
{
$newCat = "SHIRTS > GREEN";
}
else if (strpos($formattedCurrentCat, "blue shirt")
|| (strpos($formattedCurrentCat, "blue") && strpos($formattedCurrentCat, "shirt")))
{
$newCat = "SHIRTS > BLUE";
}
else if (strpos($formattedCurrentCat, "shirt"))
{
$newCat = "SHIRTS";
}
else
{
$newCat = "Other > Misc";
}
//return $newCat;
$newCat = explode( ">", $newCat );
array_walk( $newCat, 'trim' );
return array_pop( $newCat );
}
Sorry if this is all poorly worded, I'm new to php.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
