4

csvファイルから約1000個のクーポンコードをwoocommerceにインポートしようとしています

このコードを使用しましたが、機能しません

このコードは、プログラムで 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ファイルの各行に対してこの機能を実行する必要があります

助けてください、今日解決策が必要です。

4

1 に答える 1

0

誰かがまだ答えを探している場合、これは for each ループで達成できます。頭のてっぺんからの正確な機能はわかりませんが、手順は非常に簡単で、グーグルがお手伝いします。

まず、CSV を配列にインポートします。MySQL クエリの場合と非常によく似ています。

次に、PHPforeachループを実行します。

ループ内で、質問のコードを使用して、配列から変数を埋めます。例えば、

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

次のように、メタ セクションに配列データを含めることもできます。

 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);
于 2015-03-03T04:34:52.823 に答える