0

I am using Magento 2.3 in ubuntu 16.4 with apache server. I am syncing data from Dropbox csv into Magento 2 attributes. Everything working fine, but I have more than 17,000 SKU's and need to process in a single shot.

My code below.

public function downloadcsv(){
        $curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://content.dropboxapi.com/2/files/download",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_HTTPHEADER => array(
    "authorization: Bearer DropboxAuthorizationKey",
    "cache-control: no-cache",
    "content-type: text/plain",
    "dropbox-api-arg: {\"path\":\"/test.dat\"}",

  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {

 // echo $response;
  $fp = fopen("/var/www/html/csv/test.csv","wb");
fwrite($fp,$response);
fclose($fp);
    }

  }

    function csv_to_multidimension_array()
{
  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $filename='/var/www/html/csv/test.csv';
   $delimiter=',';

  $result = array();


  $keys = array('sku', 'Description','LocalStock','empty1','Gross price','Discount','Net Price', 'Specials' ,'Group','Minor Group','empty2','empty3','empty4','Cost','empty5','Division','NET','Barcode','Pack Size','empty5', 'National Stock');

  foreach (file($filename) as $key => $str)
  {
      // if ($key == 0)
      //     continue;   // skip first line

      $values = str_getcsv($str, ",", '"');

      $result[] = array_combine($keys, $values);

  }

  foreach($result as $res){

                $cat_array = explode(' ', $res['Specials']);
                $sku = $res['sku'];             
                $productId = $objectManager->get('\Magento\Catalog\Model\Product')->getIdBySku($res['sku']);
                $products = $objectManager->create("Magento\Catalog\Model\Product")->getCollection()->addAttributeToFilter('entity_id',array('eq'=> $productId));

              if($res['LocalStock'] != ''){
                $localqty = $res['LocalStock'];
                $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
                $connection = $resource->getConnection();

                 $sql = "UPDATE inventory_source_item SET quantity = $localqty WHERE sku = '$sku' AND source_code = 'sample'";
                 $connection->query($sql);  

              }

I was able to update everything using this method but when the data are large I couldn't update. It loads for a long time and gives connection reset error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.