首页 文章

将所有产品更改为在Woocommerce中具有相同的运输类别

提问于
浏览
2

我有一个有1000种产品的woocommerce商店,但是,我注意到随机产品没有适用于他们的运输类 . 因此,当客户到达结账时,这些项目会导致运输计算出现问题 .

有没有办法显示没有运输类别的产品列表,或者将商店中的所有产品更改为相同的运输类别而不单独检查每个产品?

我知道我可以使用批量操作来更改它们,但是有25页,即使我更改了显示的产品数量 .

1 回答

  • 0

    这应该会更改所有已发布帖子的运送类别 .

    Updated answer

    只记得fields参数,所以这是新代码:

    $shipping_class_slug = 'your-shipping-class-slug';
    
    // get just the IDs of all products published
    $products = new WP_Query( array (
        'post_type'      => 'product',
        'posts_per_page' => -1,
        'fields'         => 'ids'
    ));
    
    // for each product change the shipping class
    foreach ( $products->posts as $pID ) {
        wp_set_object_terms( $pID, $shipping_class_slug, 'product_shipping_class' );
    }
    

    Old answer

    $shipping_class_slug = 'your-shipping-class-slug';
    
    // get all products published
    $products = new WP_Query( array (
        'post_type' => 'product',
        'posts_per_page' => -1
    ));
    
    // retrieve products IDs
    $productsIDs = wp_list_pluck( $products->posts, 'ID' );
    
    // for each product change the shipping class
    foreach ( $productsIDs as $pID ) {
        wp_set_object_terms( $pID, $shipping_class_slug, 'product_shipping_class' );
    }
    

    您可以将其包装在函数中(在functions.php文件中)以使用action进行调用 .

    记得用你想要的改变 'your-shipping-class-slug' .

    最重要的是: DO A BACKUP 在你尝试任何事情之前! ;)

    (更好:在本地克隆网站并首先尝试)

相关问题