Woocommerce / WP All Import: automatically add product to category if product name contains the category name.
When import with wp all import from xml/csv/json file, i want to automatically add products to a category if their name contains the category (for example the product name is: iPhone X case, the product should automatically be added to the category iPhone->iPhone X case)
Example:
Load all product categories via get_product_categories( $fields ); https://woocommerce.wp-a2z.org/oik_api/wc_api_productsget_product_categories/
Use get the product name to find the name of the product https://woocommerce.wp-a2z.org/oik_api/wc_productget_name/
Loop through the categories and compare (https://woocommerce.wp-a2z.org/oik_api/wc_productget_name) each of them to the product name. Depending on situation and what values are in product category title or product names, need to use a regex for this (https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285)
foreach($product_category_list as $current_category) { if (strpos($product_name, $current_category->term_id) !== false) { $categories_to_put_product_in[] = $current_category; } } $product->set_category_ids($categories_to_put_product_in);
Do it via API: http://www.wpallimport.com/documentation/developers/action-reference/ (see the 'pmxi_saved_post' hook). Strongly suggest that cache the category list in an option (https://codex.wordpress.org/Options_API) so that not querying all of the product category terms every time a post is saved.
Innovative Mobile App with Blogging Features Category: Content Writing, Digital Marketing, Social Media Management, Social Media Marketing, YouTube Video Editing Budget: $15 - $25 USD