WooCommerce 为 1000 种产品添加运输类别 [英] WooCommerce Add Shipping Class to 1000 Products

查看:33
本文介绍了WooCommerce 为 1000 种产品添加运输类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚更新了我的 WooCommerce 配送等级,以便更好地为某些商品提供免费配送服务.

I've just updated my WooCommerce Shipping Classes to work better with free shipping on certain items.

但是,我需要将新的标准配送"类应用于 1000 多种产品.浏览 Products > Bulk Edit 无法一次全部选择它们,也不能一次选择 100 个,我不想诉诸于 10 个 10 个,直到我知道没有其他方法这个.

However, I need to apply my new 'Standard Shipping' class to 1000+ Products. Going through Products > Bulk Edit can't handle selecting them all at once, nor 100 at once, I don't want to resort to going through 10 by 10 until I know there's no other way of doing this.

问题:是否有可以加快速度的 SQL 查询?

我似乎无法在数据库中找到每个产品的运输类别的位置:(

I can't seem to find where in the database the Shipping Class is kept for each product :(

推荐答案

我制作了这个插件,它循环遍历某个类别的所有产品并分配所需的运输类别,更多详细信息在评论中:

I made this plugin, it loops through all the products of a certain category(s) and assigns the desired shipping class, more details are in the comments:

<?php
/*
Plugin Name: Bulk Shipping class update
Description: Bulk Update of shipping class for woocommerce products
Version: 0.0.1
Contributors: svelandiag
Author: svelandiag
Text Domain: woocommerce-ship-class-by-cat
*/


// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
    die;
}

// define the woocommerce_init callback 
function action_woocommerce_init( $array ) { 
    /** The category ID's with the posts you want to change */
    $category_ids = array(635, 1022, 289, 291); // array of ints or single int
    
    /** The shipping class to set for the products */
    $shipping_class_slug = "shipping-class-slug"; // found in "shipping classes" in woocommerce settings
    
    /** Run query to collect our data */
    $products = new WP_Query(array(
        'post_type' => 'product',
        'posts_per_page' => -1,
        'fields' => 'ids',
        'tax_query' => array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'term_id',
                'terms' => $category_ids,
                'operator' => 'IN'
            )
        )
    ));
    
    /** Set our shipping class on each product */
    foreach ($products->posts as $pid) {
        wp_set_object_terms($pid, $shipping_class_slug, 'product_shipping_class');
    }
    
    /** reset the query */
    wp_reset_query();
}; 
         
// add the action 
add_action( 'admin_init', 'action_woocommerce_init', 10, 1 );

?>

您还可以提取钩子和回调并将其添加到您的子主题的 functions.php 中,请注意这是一次性使用的代码段,因为它会在每次管理后端初始化时运行.

You can also extract the hook and the callback and add it to your functions.php of your child theme, be careful this is a one-time use snippet since it runs every time admin backend initializes.

该插件已经过最新的 Woocomerce 4.x 和最新的 WordPress 5.x 测试.

这篇关于WooCommerce 为 1000 种产品添加运输类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆