<?php /* Template Name: CustomPageT1 */

$i = 0;
// Open the file for reading
if (($h = fopen(“products.csv”, “r”)) !== FALSE)
{
// Convert each line into the local $data variable
while (($data = fgetcsv($h, 1000, “,”)) !== FALSE)
{
// Read the data from a single line

if($i > 0){
$product = get_product_by_sku($data[4]);
$product1 = json_decode($product);
$post_id = $product1->id ;
//$shortdesciption = $data[32].’ ‘.$data[33].’ ‘.$data[34].’ ‘.$data[35].’ ‘.$data[36];

update_post_meta( $post_id, ‘srp’, $data[15] );
update_post_meta( $post_id, ‘etailer_price_after_discount’, $data[14] );
update_post_meta( $post_id, ‘unfi_product’, $data[4] );
update_post_meta( $post_id, ‘pack’, $data[10] );
update_post_meta( $post_id, ‘size’, $data[11] );
update_post_meta( $post_id, ‘selling_units’, $data[12] );
update_post_meta( $post_id, ‘ingredients’, $data[23] );
update_post_meta( $post_id, ‘upc’, $data[24] );
update_post_meta( $post_id, ‘country_of_origin’, $data[30] );
update_post_meta( $post_id, ‘brand’, $data[45] );
//echo update_post_meta( $post_id, ‘_post_excerpt’, $shortdesciption );

//print_r($data); exit;
echo $post_id.'<br/>’;
}

$i++;
}

// Close the file
fclose($h);
}

function get_product_by_sku( $sku ) {

global $wpdb;

$product_id = $wpdb->get_var( $wpdb->prepare( “SELECT post_id FROM $wpdb->postmeta WHERE meta_key=’_sku’ AND meta_value=’%s’ LIMIT 1”, $sku ) );

if ( $product_id ) return new WC_Product( $product_id );

return null;
}

?>

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *