产品CSV导入器Woocommerce工具在前端

时间:2018-05-23 21:21:52

标签: php wordpress woocommerce import-csv

我想在前端展示Woocommerce的原生CSV产品导入工具,以便具有特定角色的用户(供应商)可以使用它将他们的产品上传到网站。

我从未使用原生Woocommerce导入器,因为它出现在3.0更新中,但我有关于我应该如何开始的线索。

在WC文档中我找到了WC_Product_CSV_Importer类,在里面我找到了方法:Initialize importer

    Initialize importer.

    __construct( string $file, array $params = array()  )


    Parameters
        $file  --> File to read.
        $params  --> Arguments for the parser.

和方法:读取文件

Read file

read_file( )

我也认为如果找到产品导入器元数据库,我可以使用它。

有没有人试图做类似的事情?

任何意见都很受欢迎

Woocommerce类:https://docs.woocommerce.com/wc-apidocs/class-WC_Product_CSV_Importer.html

1 个答案:

答案 0 :(得分:0)

Woocommerce Rest API使您能够为用户提供从网站前端发布新产品的功能。

batch create, update and delete multiple products

<?php
$data = [
'create' => [
    [
        'name' => 'Woo Single #1',
        'type' => 'simple',
        'regular_price' => '21.99',
        'virtual' => true,
        'downloadable' => true,
        'downloads' => [
            [
                'name' => 'Woo Single',
                'file' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_4_angle.jpg'
            ]
        ],
        'categories' => [
            [
                'id' => 11
            ],
            [
                'id' => 13
            ]
        ],
        'images' => [
            [
                'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_4_angle.jpg',
                'position' => 0
            ]
        ]
    ],
    [
        'name' => 'New Premium Quality',
        'type' => 'simple',
        'regular_price' => '21.99',
        'description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.',
        'short_description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
        'categories' => [
            [
                'id' => 9
            ],
            [
                'id' => 14
            ]
        ],
        'images' => [
            [
                'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg',
                'position' => 0
            ],
            [
                'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg',
                'position' => 1
            ]
        ]
    ]
],
'update' => [
    [
        'id' => 799,
        'default_attributes' => [
            [
                'id' => 6,
                'name' => 'Color,
                'option' => 'Green'
            ],
            [
                'id' => 0,
                'name' => 'Size',
                'option' => 'M'
            ]
        ]
    ]
],
'delete' => [
    794
]
];

print_r($woocommerce->post('products/batch', $data));
?>