WooCommerce使用MySQL批量更改产品类型

nxagd54h  于 2022-10-24  发布在  Mysql
关注(0)|答案(3)|浏览(261)

当使用WpAllImport导入产品时,我们所有的产品都存储为可变产品,即使它们没有任何变化。我们需要将这些作为单一产品存储。
对于没有任何变化的所有产品,我们如何使用MySQL将所有产品类型都更改为Single?我们在查询时遇到了问题。
希望一些WooCommerceMaven能帮助我们解决这个问题。
非常感谢!

sczxawaw

sczxawaw1#

首先从MySQL备份您的数据库,然后您可以从下面的查询中获取您的产品类型的ID:

select term_id from wp_terms where name='variable' // assume return 4 as result

select term_id from wp_terms where name='simple' // assume return 2 as result

通过上面的查询,您可以获得下面提到的更新查询中需要使用的两种产品类型的id。

UPDATE wp_term_relationships
INNER JOIN wp_terms ON (wp_term_relationships.term_taxonomy_id = wp_terms.term_id)
SET wp_term_relationships.term_taxonomy_id = '2' where wp_term_relationships.term_taxonomy_id = '4'
hc8w905p

hc8w905p2#

首先用循环得到所有变量的乘积。使用每个产品帖子ID,获取可用的变体。如果该产品没有任何变化。然后将每个都更改为简单。

<?php
            $args = array(
                'post_type' => 'product',
        'product_type' => 'variable'
                );
            $products = new WP_Query( $args );
            if ( $products->have_posts() ) {
                while ( $products->have_posts() ) : $products->the_post();
     $variations = array();
$id = the_ID();
$args = array(
    'post_parent' => $id
);
$variations = get_children( $args );

    if(sizeOf($variations) == 0){
    wp_set_object_terms( $id, 'simple', 'product_type' );
    }
                endwhile;
            } 
        ?>
cbjzeqam

cbjzeqam3#

-- update WooCommerce variable products without variations to simple
--1
CREATE TABLE PPCD_EDI_TEMP_UPDATE
SELECT r.object_id
FROM wp_posts wp 
LEFT JOIN wp_term_relationships r ON wp.ID = r.object_id 
LEFT JOIN wp_postmeta SKU on SKU.post_id = wp.ID and SKU.meta_key = '_sku' -- SKU.meta_value as sku
LEFT JOIN wp_postmeta PRICE on PRICE.post_id = wp.ID and PRICE.meta_key = '_price' -- PRICE.meta_value as price
LEFT JOIN wp_wc_product_custom_lookup PCL on PCL.product_id = wp.ID
LEFT JOIN wp_term_taxonomy tt ON r.term_taxonomy_id = tt.term_taxonomy_id 
LEFT JOIN wp_terms t ON t.term_id = tt.term_id 
LEFT JOIN wp_posts wpv ON wp.id = wpv.post_parent AND wpv.post_type != 'attachment'
WHERE tt.taxonomy = 'product_type' 
AND t.name = 'variable' 
and wpv.ID is null; --test for one product, replace ; with the following: and SKU.meta_value = '10001894';
--2
UPDATE wp_term_relationships 
SET term_taxonomy_id = (select term_id from wp_terms where name='simple')
WHERE term_taxonomy_id = (select term_id from wp_terms where name='variable') AND object_id IN (select object_id from PPCD_EDI_TEMP_UPDATE);
--test
SELECT SKU.meta_value as sku, PRICE.meta_value as price,
wp.id AS 'Product Id', 
wpv.id AS 'Variant Id', 
wp.post_title as parent_title, 
wpv.post_title as variant_title, 
wpv.post_excerpt 
FROM wp_posts wp 
LEFT JOIN wp_term_relationships r ON wp.ID = r.object_id 
LEFT JOIN wp_postmeta SKU on SKU.post_id = wp.ID and SKU.meta_key = '_sku' -- SKU.meta_value as sku
LEFT JOIN wp_postmeta PRICE on PRICE.post_id = wp.ID and PRICE.meta_key = '_price' -- PRICE.meta_value as price
LEFT JOIN wp_wc_product_custom_lookup PCL on PCL.product_id = wp.ID
LEFT JOIN wp_term_taxonomy tt ON r.term_taxonomy_id = tt.term_taxonomy_id 
LEFT JOIN wp_terms t ON t.term_id = tt.term_id 
LEFT JOIN wp_posts wpv ON wp.id = wpv.post_parent AND wpv.post_type != 'attachment'
WHERE tt.taxonomy = 'product_type' 
AND t.name = 'variable' 
and wpv.ID is null;

相关问题