Last active
February 28, 2023 07:44
-
-
Save wpchannel/5046d74572d4312d5e40c727fa8a2fce to your computer and use it in GitHub Desktop.
Custom script to import CSV file containing products into a WordPress site using Polylang as multilingual plugin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once('wp/wp-load.php'); | |
// Set the path to the CSV file | |
$file_path = 'products.csv'; | |
$count = 0; | |
$csv_data = array(); | |
// Load the CSV file into an array | |
if (($handle = fopen($file_path, 'r')) !== FALSE) { | |
while (($data = fgetcsv($handle, NULL, ';')) !== FALSE) { | |
$csv_data[] = $data; | |
} | |
fclose($handle); | |
} | |
// Sort the array by the first column in ascending order | |
array_multisort(array_column($csv_data, 0), SORT_ASC, $csv_data); | |
// Loop through each row in the sorted CSV data | |
foreach ($csv_data as $data) { | |
$count++; | |
$term_id = get_terms( array( | |
'taxonomy' => 'brand', | |
'meta_key' => 'wpc_brand_id', | |
'meta_value' => $data[15], | |
'hide_empty' => false, | |
'fields' => 'ids' | |
) ); | |
$args = [ | |
'post_type' => 'product', | |
'post_status' => 'publish', | |
'post_title' => $data[3], | |
'post_date' => $data[2], | |
'tax_input' => [ | |
'brand' => $term_id | |
], | |
'meta_input' => [ | |
'meta_key_1' => $data[0], | |
'meta_key_2' => $data[5], | |
'meta_key_3' => $data[6], | |
'meta_key_4' => $data[7], | |
'meta_key_5' => wp_strip_all_tags( $data[10] ), | |
] | |
]; | |
$post_id = wp_insert_post( $args ); | |
update_field( 'wpc_product_video', $data[13], $post_id ); | |
pll_set_post_language( $post_id, $data[1] ); | |
$options=array( | |
"ssl"=>array( | |
"verify_peer" => false, | |
"verify_peer_name" => false, | |
), | |
); | |
// Get the image path and filename | |
$image_path = $data[14]; | |
$image_filename = basename($image_path); | |
if ($image_path) { | |
$upload_file = wp_upload_bits( $image_filename, null, file_get_contents( $image_path ) ); | |
if ( ! $upload_file['error'] ) { | |
$wp_filetype = wp_check_filetype( $image_filename, null ); | |
$attachment = [ | |
'post_mime_type' => $wp_filetype['type'], | |
'post_parent' => $post_id, | |
'post_title' => $image_filename | |
]; | |
$attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], $post_id ); | |
if ( ! is_wp_error( $attachment_id ) ) { | |
require_once( ABSPATH . "wp-admin" . '/includes/image.php' ); | |
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] ); | |
wp_update_attachment_metadata( $attachment_id, $attachment_data ); | |
if ( $count % 2 == 0 ) { | |
$attachmentFR = $attachment_id; | |
} else { | |
$attachmentEN = $attachment_id; | |
} | |
if ( $count % 2 === 0 ) { | |
$args = [ | |
'fr' => $attachmentFR, | |
'en' => $attachmentEN | |
]; | |
pll_save_post_translations( $args ); | |
} | |
set_post_thumbnail( $post_id, $attachment_id ); | |
} | |
} | |
} | |
if ($count % 2 == 0) { | |
$postFR = $post_id; | |
} else { | |
$postEN = $post_id; | |
} | |
if ($count % 2 === 0) { | |
$args = [ | |
'fr' => $postFR, | |
'en' => $postEN | |
]; | |
pll_save_post_translations( $args ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment