【问题标题】:WooCommerce Add Shipping Class to 1000 ProductsWooCommerce 为 1000 种产品添加运输类别
【发布时间】:2014-04-15 16:15:58
【问题描述】:

我刚刚更新了我的 WooCommerce 运输课程,以便更好地使用某些商品的免费送货服务。

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

问题:有没有可以加快这个速度的 SQL 查询?

我似乎无法在数据库中找到每种产品的运输等级保存在哪里:(

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:
    1. 在页面顶部的屏幕选项中,选择每个显示 1000 个产品 页面并删除除标题之外的所有其他字段。

    2. 选择所有产品

    3. 批量编辑

    【讨论】:

    • > 问:有没有可以加快这个速度的 SQL 查询?
    【解决方案2】:

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

    <?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,请注意这是一次性使用的 sn-p,因为它会在每次管理员后端初始化时运行。

    该插件已使用最新的 Woocomerce 4.x 和最新的 WordPress 5.x 进行了测试。

    【讨论】:

    • 我已经为此等了 6 年。我给个测试看看好不好用!
    • 它已经测试到 Woocomerce 4.x 和最新的 WordPress 版本。
    • @svelandiag 谢谢。这非常有用。如何更改多个运输类别?例如:运输类别 1 设置为产品类别 1,运输类别 2 设置为产品类别 2 和 ...
    猜你喜欢
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多