我如何从csv导入批量优惠券到woocommerce

0ejtzxu1  于 2023-07-31  发布在  其他
关注(0)|答案(2)|浏览(110)

我正试图从一个CSV文件导入大约1000个优惠券代码到WooCoommerce
我用过这个密码,但是不管用
此代码可以程序化生成1张优惠券:

$coupon_code = 'UNIQUECODE'; // Code
$amount = '10'; // Amount
$discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product
$coupon = array(
'post_title' => $coupon_code,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon'
);
$new_coupon_id = wp_insert_post( $coupon );
// Add meta
update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
update_post_meta( $new_coupon_id, 'individual_use', 'no' );
update_post_meta( $new_coupon_id, 'product_ids', '' );
update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
update_post_meta( $new_coupon_id, 'usage_limit', '' );
update_post_meta( $new_coupon_id, 'expiry_date', '' );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );

字符串
我需要对csv文件的每一行都做这个函数
你能帮我一下吗?我今天就需要答案。

41ik7eoe

41ik7eoe1#

如果有人仍在寻找答案,可以使用for each循环来完成。我不知道确切的功能关闭我的头上,但步骤是相当简单的,谷歌将帮助你。
首先将CSV导入到数组中。与MySQL查询的方式非常相似。
然后执行PHP foreach循环。
在循环内部,您使用问题中的代码填充数组中的变量。例如,

$coupon_code = '$array[code]'; // Code
 $amount = '$array[amount]'; // Amount
 $discount_type = '$array[discount_type]';

字符串
你也可以像这样在Meta部分包含数组数据:

update_post_meta( $new_coupon_id, 'product_ids', $array[ids] );
 update_post_meta( $new_coupon_id, 'exclude_product_ids', $array[exclude] );
 update_post_meta( $new_coupon_id, 'usage_limit', $array[limit] );
 update_post_meta( $new_coupon_id, 'expiry_date', $array[expires] );


同样,这只是一个概括的功能,可以做什么iCode98要求。php手册有很好的信息,也有很多关于将csv转换为数组和for each循环的教程。我可能有一个使用这个,我会更新这个与一个工作的例子,如果我建立它或如果有人真的需要它仍然。
更新:
这应该能够工作,但它确实依赖于输入文件

$file = fopen('myCSVFile.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
  //$line is an array of the csv elements, This is where you would add the script above. 
  //Each column of the csv is in the array by id. So if you have: 
  //`a,b,c` Then `a=$line[0]` `b=$line[1]` `c=$line[2]` and so on.
}
fclose($file);

muk1a3rh

muk1a3rh2#

注意,在你完成之后,要注解掉add_action。如果没有,你将生成代码一遍又一遍,每次有人访问该页面。
或者您可以为函数添加更多限制。

function tymy_cpn_import()
{
    //delete_option('run_tymy_cpn_import_only_once_01', 1);
    if (!get_option('run_tymy_cpn_import_only_once_01')) {
         add_option('run_tymy_cpn_import_only_once_01', 1);

        $arr = [

        'CY461SDCCC','DDSCCCCCC',.........

        ];

        foreach ($arr as $cpn) {
            $coupon_code = $cpn; // Code
            $amount = '100'; // Amount
            $discount_type = 'percent'; // Type: fixed_cart, percent, fixed_product, percent_product
            $product_ids = '572,574,575';

            $coupon = array(
            'post_title' => $coupon_code,
            'post_content' => '',
            'post_status' => 'publish',
            'post_author' => 1,
            'post_type' => 'shop_coupon'
            );

            $new_coupon_id = wp_insert_post($coupon);
        // Add meta
            update_post_meta($new_coupon_id, 'discount_type', $discount_type);
            update_post_meta($new_coupon_id, 'coupon_amount', $amount);
            update_post_meta($new_coupon_id, 'individual_use', 'no');
            update_post_meta($new_coupon_id, 'product_ids', $product_ids);
            update_post_meta($new_coupon_id, 'exclude_product_ids', '');
            update_post_meta($new_coupon_id, 'usage_limit', '1');
            update_post_meta($new_coupon_id, 'usage_limit_per_user', '1');
            update_post_meta($new_coupon_id, 'expiry_date', '');
            update_post_meta($new_coupon_id, 'apply_before_tax', 'yes');
            update_post_meta($new_coupon_id, 'free_shipping', 'no');
        }
    }
}

//add_action('init', 'tymy_cpn_import');

字符串
删除您商店中的所有代码!注意使用它。

function tymy_cpn_import_delete()
{

    if (get_option('run_tymy_cpn_import_only_once_01')) {
        delete_option('run_tymy_cpn_import_only_once_01', 1);
        $allposts = get_posts(array('post_type' => 'shop_coupon','numberposts' => -1));
        foreach ($allposts as $eachpost) {
            wp_delete_post($eachpost->ID, true);
        }
    }
}
//add_action('init', 'tymy_cpn_import_delete');

相关问题