首页 > 解决方案 > 如何在我的自定义 PHP 文件中使用一类 WooCommerce?(产品 CSV 导入器)

问题描述

我有一个 WooCommerce 网站,我需要通过 CSV 文件自动更新产品。为此,我创建了一个执行 PHP 文件的 cron 作业,它每小时创建一个 csv 文件(命名为:jsonproducts.csv)。

现在,我计划创建一个新的自定义 PHP 文件,该文件将使用 Woocommerce 的 WC_Product_CSV_Importer 类的 import() 方法。(

当我尝试执行此操作时,结果被导入:0,失败:0,更新:0,跳过:0。

我怎样才能成功执行这个?

<?php
include("wp-load.php");
require_once('wp-content/plugins/woocommerce/includes/import/class-wc-product-csv-importer.php');

    
$args = array(
    'start_pos'        => 0, // File pointer start.
    'end_pos'          => 5, // File pointer end.
    'lines'            => 5, // Max lines to read.
    'mapping'          => array(), // Column mapping. csv_heading => schema_heading.
    'parse'            => true, // Whether to sanitize and format data.
    'update_existing'  => true, // Whether to update existing items.
    'delimiter'        => ',', // CSV delimiter.
    'prevent_timeouts' => false, // Check memory and time usage and abort if reaching limit.
    'enclosure'        => '"', // The character used to wrap text in the CSV.
    'escape'           => "\0", // PHP uses '\' as the default escape character. This is not RFC-4180 compliant. This disables the escape character.
);
    
$file = 'json2csv.csv';

$WC_Product_CSV_Importer = new WC_Product_CSV_Importer($file, $args);


var_dump($WC_Product_CSV_Importer->import());

?>

标签: phpwordpresswoocommerceimport

解决方案


推荐阅读