4

I'm trying to import about 1000 coupon codes to woocoommerce from a csv file

I've used this code but it's not working

this code can generate 1 coupon programmly :

$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' );

I need to do this function for each line of the csv file

would you help me please, I need the solution today.

iCode98
  • 41
  • 1
  • 7

2 Answers2

0

This could be accomplished with a for each loop if someone is still looking for an answer. I dont know the exact functions off the top of my head but the steps are pretty simple and google will help you.

You start by importing your CSV into an array. Very similar to the way you would with a MySQL query.

Then you do a PHP foreach loop.

Inside the loop you use the code in the question filling the variables from teh array. eg,

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

You can also include array data in the meta section like this:

 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] );

Again this is just a general outline to the function that could do what iCode98 asked. The php manual has great information and there are plenty of tutorials on turning a csv into an array and for each loops. I may have a use for this and I will update this with a working example if I build it or if someone really needs it still.

UPDATE:

This should be able to work but it does depend on the input file

$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);
JpaytonWPD
  • 485
  • 1
  • 7
  • 22
0

Be aware to COMMENT OUT the add_action after you done. IF not you will generate codes over and over every time when somebody visit the page.

Or you can add some more restrictions to the function.

    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 bellow REMOVE ALL codes in your shop! be aware to use it.

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');
Gavo
  • 21
  • 3