Question

Some of my products have these weird variations with unspecified attributes (I am using Size and Color in my store). It looks like this:

enter image description here

So, if you would purchase a 85 B, Black variation of the product (ID 111224), in reality the order would contain the variation with unspecified attributes (ID 112392), because it is higher. It is a huge problem because those unspecified variations contain wrong SKUs and additional data.

I am using WP All Import for importing and updating products, I'm suspecting the plugin (or my settings) for this mess and I will look at it. However I have thousands of products in the store and deleting them manually is out of the question. Is there a way to delete variations that has at least one unspecified variation programatically?

I've tried to query such product variations using this tax_query:

'tax_query' = array(
    'relation' => 'OR',
    array(
        'taxonomy'         => 'pa_size',
        'terms'            => array(''),
        'field'            => 'slug',
        'operator'         => 'IN',
    ),
    array(
        'taxonomy'         => 'pa_color',
        'terms'            => array(''),
        'field'            => 'slug',
        'operator'         => 'IN',
    ),
);

But the query doesn't return anything. It doesn't work when I try to pass empty string as a value inside 'term' array. I've also tried the '=' operator and empty string (not an array) as a value.

By the way, I've checked if those empty attributes really are empty strings and yes, they are. This code:

$product = wc_get_product(112392);
$atts = $product->get_attributes();
    
foreach($atts as $att) {
    echo gettype($att) . ' : ' . $att .  '<br>';
}

outputs this:

string : 
string : 

While for correct variations (e.g. 111224) it outputs:

string : 85-b
string : black

Any idea how could I get rid of them? I could just query all products variations, loop through them and delete all empty ones. Maybe repeat it couple of times (based on execution time) but it doesn't seem like a good solution, it is very performance expensive - the same problem might appear in the future as well and I would like a quick solution that would only query broken variations.

Thank you very much.

Was it helpful?

Solution

I've managed to write an SQL query that would get all product variation IDs that do not have both size and color set up (not present in the wp_postmeta table). After that I just need to loop through these IDs and delete those variations using wp_delete_post. This is the query:

SELECT p.ID 
FROM wp_posts p 
LEFT JOIN wp_postmeta pm 
ON p.ID = pm.post_id 
WHERE p.post_type = 'product_variation' 
AND (pm.post_id NOT IN (SELECT post_id FROM wp_postmeta WHERE meta_key = 'attribute_pa_size' OR meta_key = 'attribute_pa_color')) 
GROUP BY ID 

Works well for my case. I was originaly asking about variations that has at least one attribute unspecified, but turned out that wouldn't be the solution, I had to look for variations with boths attributes empty.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top