• Hi All,

    we have a PHP procedure that automatically imports (through a CRON process every night) all products from a CSV file to our Woocommerce.

    The problem is that, when setting subcategories, if there are 2 (or more) of them with the same name, under DIFFERENT parent categories, it automatically gives the product the first one found.

    For example, I want to get this:

    Medals
    |-> Silver
    |--> Large
    |--> Small
    |-> Gold
    |--> Large
    |--> Small

    So, under the parent category “Medals”, there are 2 different subcategories “Silver” and “Gold”. They both have another depth level of subcategories with the same NAME.

    Instead, when importing, I get this:

    Medals
    |-> Silver
    |-> Gold
    |--> Large
    |--> Small

    So every Silver medal takes “Large” or “Small” subcategory under the “Gold” tree (instead of “Silver” tree) since Gold is the first one found in the CSV and so the first to create “Large” and “Small” subcategories.

    It’s like WordPress says “Hey, there’s already a Large subcategory existing, I’m going to put all your products there” ignoring the fact that the parent category is “Silver” instead of “Gold”.

    The code creating categories from the CSV is:

    $cat = sanitize_text_field($data[2]);
    $cat = strtolower($cat);
    $cat2 = sanitize_text_field($data[3]);
    $cat2 = strtolower($cat2);
    $cat3 = sanitize_text_field($data[4]);
    $cat3 = strtolower($cat3);
    wp_set_object_terms($post_id, $cat, ‘product_cat’);
    wp_set_object_terms($post_id, $cat2, ‘product_cat’, $append = true ) ;
    wp_set_object_terms($post_id, $cat3, ‘product_cat’, $append = true ) ;

    where $data is an array of values of the CSV file (every row in the CSV is a single product, in this case, a medal).

    Sorry for the wall of text but I’ve tried a lot and cannot figure this out.
    If I wasn’t clear explaining this, go ahead and tell me!

    Thanks in advance. Would be really grateful if you have any ideas.

    • This topic was modified 5 years, 2 months ago by bcworkz.
    • This topic was modified 5 years, 2 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 2 replies - 16 through 17 (of 17 total)
  • Thread Starter charlieadmino

    (@charlieadmino)

    @bcworkz

    “There’s a distinct term slug provided with every item!? That’s useful ??”

    Well, I discovered that WordPress does it on its own.
    Tried to add manually another material “Opal” and sub-category “Small”.
    The slug got automatically “opal-small”.

    “You’re right, relating images to terms is another topic.” I knew I didn’t explain myself correctly.

    These medals are special products since in the e-commerce there’s a special drang and drop area where you can upload a custom image to be printed on the medal itself.
    We managed this with a plugin.

    In the WordPress admin panel, you have a field (in the plugin settings) to specify for which product categories have to be displayed this upload area in the product page.
    So the word “Medals” is in this text field.

    Issue is that, on every import, the category “Medals” (like every other category) is deleted and created again. So this plugin is, of course, not intended to dinamically search that category again in the db.

    That means every morning I find this field emply and have to manually specify again “Medals” and hit “Save”.

    Was looking for a way to make a CRON process and insert “Medals” term after the import every night. Tried to ask the plugin dev but didn’t answer. Tried to look if there’s a table in the DB containing this text field but with no luck. It seems something Javascript stored(?) but I’m not able to edit the plugin.

    So yeah this is the minor issue now.

    Moderator bcworkz

    (@bcworkz)

    Categories are created and assigned to posts through a rather complex relationship between 4 different DB tables. I recommend you do not directly manipulate these tables. You’re better off using WP functions intended for the purpose.

    You could use wp_schedule_event() to cause PHP code to run on a regular interval. It registers another function to run at regular intervals. Don’t schedule events with code that runs every time WP initializes because you’ll end up stacking the same events on top of each other. It’s customary to schedule events on plugin activation to ensure it’s only done once, but any mechanism to ensure it’s run once will work.

Viewing 2 replies - 16 through 17 (of 17 total)
  • The topic ‘Issue importing Woocommerce products with same name subcategory’ is closed to new replies.