woocommerce批量添加商品

it不难
2022-12-28 / 0 评论 / 18 阅读 / 正在检测是否收录...

批量创建分类

安装插件

使用

分类提取

cat a.txt | sort |uniq |tr "\n"  ","

批量导入商品

批量导入商品图片

利用媒体库导入图片

查看图片文件,插件:Add From Server

批量导入商品

利用默认功能import即可

数据转换处理

利用python脚本处理原始数据,生成csv文件

#coding=utf-8
# xls 转 csv 脚本

import xlrd
import time
import random
import re
from collections import defaultdict
import numpy as np

import json

#数据源文件
R_FILE = './data/Product_List.xls'

#起始行
START_ROW = 7

#处理后保存到文件
S_FILE = '../../Downloads/products_%s.csv' % (time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime()))
S_FILE2 = '../../Downloads/price_rules_%s.csv' % (time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime()))

#读取表格
class excel_read:
    def __init__(self, rfile, encoding='utf-8',index=0):

      self.data=xlrd.open_workbook(rfile)  ##获取文本对象
      self.table=self.data.sheets()[index]  ###根据index获取某个sheet
      self.rows=self.table.nrows            ##获取当前sheet页面的总行数,把每一行数据作为list放到 list

    def get_data(self):
        result=[]
        for i in range(self.rows):
            col=self.table.row_values(i)  ##获取每一行数据
            if i == 6:
                print(col)
            result.append(col)

        return result[START_ROW:17]

#处理数据
class produce_data:
    def __init__(self, rawdata):
        self.rawdata = rawdata

    def _get_ns(self):
        '''
        生成随机数ns(0s至1s之间)
        '''
        return int(random.random()*1000000000)


    def produce(self):
        '''
        数据处理主函数
        '''
        #表头信息
        head_str ='' + \
                'Type,' + \
                'SKU,' + \
                'Name,' + \
                'Published,' + \
                '"Is featured?",' + \
                '"Visibility in catalog",' + \
                '"Short description",' + \
                'Description,' + \
                '"In stock?",' + \
                '"Sale price",' + \
                '"Regular price",' + \
                'Categories,' + \
                'Tags,' + \
                'Images'

        #价格列表
        prices = []

        #后面一定要有空格
        body_str = ''

        for item in self.rawdata:
            print(item)

            #图片链接
            img_url='https://4ilogo.com/wp-content/uploads/2022/12/{}.jpg'.format(item[1])

            #构建条目
            body_str += '{},{},"{}",{},{},{},"{}","{}",{},{},{},"{}","{}",{}\n'.format(
                    'simple',     # Type
                    item[1],      # SKU
                    item[2],      # Name
                    1,            # Published
                    0,            # Is featured
                    'visible',    # Visibility in catalog
                    item[11],     # Short description
                    item[10],     # Description
                    1,            # is stock?
                    item[29],     # Sale price
                    item[29],     # Regular price
                    item[6],      # Categories
                    item[6],      # Tags
                    img_url       # Images
                    )

            #价格规则
            prices.append({
                    "type": "single_item",
                    "rule_type": "common",
                    "filters": [
                         {
                              "qty": 1,
                              "type": "product_sku",
                              "method": "in_list",
                              "value": [
                                   item[1]
                              ],
                              "product_exclude": {
                                   "on_wc_sale": "",
                                   "already_affected": "",
                                   "backorder": "",
                                   "values": []
                              }
                         }
                    ],
                    "title": item[1],
                    "priority": "",
                    "enabled": "on",
                    "sortable_blocks_priority": [
                         "roles",
                         "bulk-adjustments"
                    ],
                    "additional": {
                         "conditions_relationship": "and",
                         "is_replace": "",
                         "replace_name": "",
                         "is_replace_free_products_with_discount": "",
                         "free_products_replace_name": "",
                         "is_replace_auto_add_products_with_discount": "",
                         "auto_add_products_replace_name": "",
                         "sortable_apply_mode": "consistently"
                    },
                    "conditions": [],
                    "cart_adjustments": [],
                    "limits": [],
                    "bulk_adjustments": {
                         "type": "bulk",
                         "qty_based": "not",
                         "discount_type": "price__fixed",
                         "ranges": [
                                {
                                     "from": int(item[23]),
                                     "to": int(item[24]),
                                     "value": item[29]
                                },
                                {
                                     "from": int(item[24] + 1),
                                     "to": int(item[25]),
                                     "value": item[30]
                                },
                                {
                                     "from": int(item[25] + 1),
                                     "to": int(item[26]),
                                     "value": item[31]
                                },
                                {
                                     "from": int(item[26] + 1),
                                     "to": int(item[27]),
                                     "value": item[32]
                                },
                                {
                                     "from": int(item[27] + 1),
                                     "to": int(item[28]),
                                     "value": item[33]
                                },
                                {
                                     "from": int(item[28] + 1),
                                     "to": "",
                                     "value": item[34]
                                }
                         ],
                         "table_message": ""
                    },
                    "role_discounts": [],
                    "get_products": {
                         "repeat": "-1",
                         "repeat_subtotal": "",
                         "max_amount_for_gifts": "null"
                    },
                    "auto_add_products": {
                         "repeat": "-1",
                         "repeat_subtotal": ""
                    },
                    "options": {
                         "apply_to": "expensive",
                         "repeat": -1
                    },
                    "advertising": {
                         "discount_message": "",
                         "discount_message_cart_item": "",
                         "long_discount_message": "",
                         "sale_badge": ""
                    },
                    "version": "4.2.0"
                })


        pro_str = head_str + '\n' + body_str

        #返回字符串
        return pro_str, prices

#主函数
if __name__ == '__main__':
    print('正在读取数据... %s' % (R_FILE))
    datalist = excel_read(R_FILE).get_data()

    print('正在处理数据...')
    product_str, price_arr = produce_data(datalist).produce()

    #将结果写入文件
    print('正在保存商品信息到 %s' % (S_FILE))
    with open(S_FILE, 'w') as fs:
        fs.write(product_str)

    print('正在保存价格规则到 %s' % (S_FILE2))
    with open(S_FILE2, 'w') as fs:
        fs.write(json.dumps(price_arr, ensure_ascii=False))

    print('完成')
    exit()

批量设置批发价格

插件: Advanced Dynamic Pricing for WooCommerce

0

评论 (0)

取消