• Hi,

    I am having 2 issues with the Woo Commerce API .

    First Issue:
    Product variations are getting orphaned in the wp_postmeta table, it seems like the API is causing this not the back end of Woo Commerce. My application is not removing any products only creating them.

    I have found a temporary fix that goes through and removes the orphaned variants.

    -- Routine to delete orphaned Variants
    CREATE TABLE IF NOT EXISTS TEMP_ID AS (SELECT distinct post_parent FROM wp_posts WHERE post_type IN ('product','product_variation')
    and post_parent not in ((select id from wp_posts WHERE post_type IN ('product','product_variation') UNION SELECT ID=0 )));
    
    DELETE FROM wp_posts WHERE
    post_parent IN (SELECT post_parent
    FROM TEMP_ID);
    
    DROP TABLE TEMP_ID;
    
    DELETE pm
    FROM wp_postmeta pm
    LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
    WHERE wp.ID IS NULL;

    Second Issue:
    When creating new variable products. I have an application that sends new products up daily and on some products I get a 400 response. Usually due to a SKU already existing in Woo Commerce. However some of the variants attached to the product(s) get created anyway even when the 400 is being thrown, is this how the API was intended to function? Most other APIs would roll back the insert/update if it failed halfway through.

    What would be causing some variants to be left behind/orphaned from the parent product?
    Why is the API inserting half of the product if one or more variants are causing a 400 response?

    The first issue could be caused by the second issue.

    WC Version: 2.4.12
    WP Version: 4.3.1
    WC Database Version: 2.4.12
    API Version: 3.0.0

    Thanks,

    Sean O’Loughlin

    https://wordpress.org/plugins/woocommerce/

The topic ‘Woo Commerce API Issues – Product variation orphans’ is closed to new replies.