最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - How to get rid of variations with unspecified attributes

programmeradmin0浏览0评论

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

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.

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

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.

Share Improve this question edited Jan 12, 2021 at 14:25 Kristián Filo asked Jan 12, 2021 at 14:16 Kristián FiloKristián Filo 4316 silver badges20 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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.

发布评论

评论列表(0)

  1. 暂无评论