We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5934
    • 0 Posts
    I have 1000 products in a csv, i want to import in Modx. Is possible?
    I would like create a page for every products and a page for every category of products.
    Help Me please.
      • 37737
      • 2 Posts
      I've the same ask... How can we do?
        • 3749
        • 24,544 Posts
        It would take a custom snippet, but a fairly simple one, depending on how the CSV file is formatted.

        It would look something like this (note that you'd have to adjust the code to match the fields of the CSV file):

        <?php
        $resourcesCreated = 0;
        $output = "";
        
        if (($fp = fopen("test.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($fp, 1000, ",")) !== FALSE) {
                if (count($data)) {
                    /* fields from CSV file */
                    $fields['pagetitle'] = $data[0];
                    $fields['introtext'] = $data[1];
                    $fields['content'] = $data[2];
                    //etc.
        
                    /* other fields (optional) */
                    $fields['published'] = '1';
                    $fields['hidemenu'] = '0';
                    $fields['parent'] = '0';
                    $fields['isfolder'] = '0';
                    // etc.
        
                    $response = $this->modx->runProcessor('resource/create', $fields);
                    if ($response->isError()) {
                        if ($response->hasFieldErrors()) {
                            $fieldErrors = $response->getAllErrors();
                            $errorMessage = implode("\n", $fieldErrors);
                        } else {
                            $errorMessage = 'An error occurred: ' . $response->getMessage();
                            $output .= "\n" . $errorMessage;
                        }
                    } else {
                        $resourcesCreated++;
                        
                        /* you would only need this next section if you need to 
                         * set TV values from the CSV file */
        
                     /*
                        $object = $response->getObject();
                        $resourceId = $object['id'];
                        $resource = $modx->getObject('modResource', $resourceId);
                        if ($resource) {
                            $resource->setTVValue('MyTV', $data[3]);
                            $resource->save();
                        }
                        */
        
                    }
        
                }
            }
            fclose($fp);
            if (empty($output)) {
                return 'Created ' . $resourcesCreated . ' resources';
            } else {
                return $output;
            }
        }

          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting