0

I need help with my PHP code.

We have an e-commerce site made with wordpress and woocommerce. The site needs to import products from another site's CSV-file through a datafeed.

The datafeed setup requires a PHP-file in the website's file system and a Cron Job that executes the PHP-file.

The PHP-file takes the data from 5dkmiglm.csv (cdn.sandberg.world) and appends it to pricelist_206876.csv (in our system)

These two CSV files differ in number of columns.

5dkmiglm.csv has these columns that should be imported to our pricelist:

  1. Partno
  2. Name
  3. Weight
  4. Width
  5. Height
  6. SE:retailPriceIncVat
  7. sv:CategoryName

The PHP-code looks like this:

`<?php
$current = file_get_contents("https://cdn.sandberg.world/feeds/5dkmiglm.csv");

$pricefile = "/pricelist_206876.csv"; 

file_put_contents($pricefile,$current); 
?>`

I placed the PHP-file in the same folder as the pricelist that needs to be updated but nothing happens. pricelist_206876 is not updated as it should be.

The Cron Job is not in the wp-admin panel, but in the control panel (DirectAdmin) of our web host Inleed. The Cron Job command looks like this: /usr/bin/php -q /dev/null "/home/goffero/domains/goffero.com/datafeedcode.php"

I tried modifying the PHP code in various ways with no result. realpath() didn't work.

I changed the write/read/execute system permissions for the files but that didn't solve the problem.

  • `/` in front suggests that you are trying to save to the very ROOT of the server, so try removing the `/` or provide the actual absolute path th where you want the file to be saved exactly, like `/path/to/your/folder/pricelist_206876.csv`. Also why do you do this with PHP and not just `wget` the file ? – Ron Dec 20 '22 at 18:50
  • `These two CSV files differ in number of columns.` Than a simple appending doesn't seem useful and if you append the whole file, you will have the row with column names in the middle of your file. - Is your question how to tackle that one or firstly to write in the file at all? – Uwe Dec 20 '22 at 19:10
  • I wrote the question primarily about how to write the file, but i also need advice on how to tackle the problem with the number of columns. I know wget downloads files, but then i don't know how to do the next step and take that data and append it to the pricelist. – PrimaryGroupEmployee Dec 20 '22 at 19:25
  • I tried removing the / in front but it didn't work. – PrimaryGroupEmployee Dec 23 '22 at 10:39

1 Answers1

1

I have a project in Magento and the import of the products is done the same way you want. We take a csv with 10 or more columns but we export only qty and SKU in this case.

Here is what we have done:

<?php

// downloaded file path
$filesRoot = BP . '/pub/path/to/your/folder/';

// If folder doesn't exist, create it
if ( ! file_exists( $filesRoot ) ) {
    mkdir( $filesRoot, 0777, true );
}

// their file
$fileIn = $filesRoot . '5dkmiglm.csv';

// your file
$fileOut = $filesRoot . 'pricelist_206876.csv';

$url = 'https://cdn.sandberg.world/feeds/5dkmiglm.csv';

// Download their csv in your environment
if ( ! file_put_contents( $fileIn, file_get_contents( $url ) ) ) {
    die( 'error download file' );
}

$countLine  = 0;
$fileInOpn  = fopen( $fileIn, 'r' );
$fileOutOpn = fopen( $fileOut, 'w' );
while ( ( $line = fgetcsv( $fileInOpn ) ) !== false ) {

    // position of each column
    if ( $countLine == 0 ) {
        $keyQty = array_search( 'qty', $line );
        $keySku = array_search( 'sku', $line );
    }
    $newLineRev = array();
    foreach ( $line as $key => $el ) {
        if ( $key == $keyQty || $key == $keySku ) {
            $newLineRev[ $keyQty ] = $line[ $keyQty ];
            $newLineRev[ $keySku ] = $line[ $keySku ];
            continue;
        }
    }

    // Write in your file
    fputcsv( $fileOutOpn, $newLineRev );
    $countLine++;
}
fclose( $fileInOpn );
fclose( $fileOutOpn );
Vijay Hardaha
  • 2,411
  • 1
  • 7
  • 16