0
Error: Class "Test\CsvImport\Model\Import" not found in /var/www/html/testproject/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:121

This is the error I am getting. Here are my code

  1. webapi.xml
    <?xml version="1.0"?>
    <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi/etc/webapi.xsd">
        <route url="/V1/import/csv" method="POST">
            <service class="Test\CsvImport\Api\ImportInterface" method="importCsv"/>
            <resources>
                <resource ref="Magento_Backend::admin"/>
            </resources>
        </route>
    </routes>
  1. di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Test\CsvImport\Api\ImportInterface" type="Test\CsvImport\Model\Import" />
</config>
  1. Test\CsvImport\Api\ImportInterface.php
<?php
namespace Test\CsvImport\Api;

interface ImportInterface
{
    /**
     * Import CSV data
     *
     * @param string $csvData Base64 encoded CSV data
     * @return mixed
     */
    public function importCsv($csvData);
}
  1. Test\CsvImport\Model\Import.php
<?php

namespace Test\CsvImport\Model;

use Test\CsvImport\Api\ImportInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;

class Import implements ImportInterface
{
    /**
     * @var \Magento\Catalog\Api\ProductRepositoryInterface
     */
    protected $productRepository;
    
    /**
     * Import constructor.
     * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
     */
    public function __construct(
        ProductRepositoryInterface $productRepository
    ) {
        $this->productRepository = $productRepository;
    }
    /**
     * @return bool|mixed
     * @throws Exception
    */
    public function importCsv($csvData)
    {
        echo "here";
        // Decode the CSV data
        $csvData = base64_decode($csvData);
        $rows = array_map('str_getcsv', explode("\n", $csvData));

        foreach ($rows as $row) {
            // Assuming SKU is in the first column and price in the second
            $sku = $row[0];
            $price = $row[1];
            try {
                $product = $this->productRepository->get($sku);
                $product->setPrice($price);
                $this->productRepository->save($product);
            } catch (\Exception $e) {
                throw new LocalizedException(__('Error updating product: %1', $e->getMessage()));
            }
        }
        return $true;
    }
}

All commands have successfully run and no issues at all. But still I am getting the error in Postman.

4
  • Namespace error in your model. The rest are fine Commented Jun 25 at 6:54
  • Corrected and commands run. Still getting the error
    – NotEqual
    Commented Jun 25 at 6:57
  • check permissions on the file and make sure it's 755 or greater. Commented Jun 25 at 7:17
  • Still no luck. Permission is correct
    – NotEqual
    Commented Jun 25 at 7:24

2 Answers 2

1

Your code is almost correct, and I have made a few necessary changes. The changes I made are marked with comment like this my change for easier identification. Please review the updated code below:

File path should be Test\CsvImport\Model\Import.php

<?php

namespace Test\CsvImport\Model; // this is my change

use Test\CsvImport\Api\ImportInterface; // this is my change
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;

class Import implements ImportInterface
{
    /**
     * @var \Magento\Catalog\Api\ProductRepositoryInterface
     */
    protected $productRepository;
    
    /**
     * Import constructor.
     * @param \Magento\Catalog\Api\ProductRepositoryInterface 
      $productRepository
     */
    public function __construct(
        ProductRepositoryInterface $productRepository
    ) {
        $this->productRepository = $productRepository;
    }
    
    /**
     * @return bool|mixed
     * @throws LocalizedException
    */
    public function importCsv($csvData)
    {
        echo "here";
        // Decode the CSV data
        $csvData = base64_decode($csvData);
        $rows = array_map('str_getcsv', explode("\n", $csvData));

        foreach ($rows as $row) {
            // Assuming SKU is in the first column and price in the second
            $sku = $row[0];
            $price = $row[1];
            try {
                $product = $this->productRepository->get($sku);
                $product->setPrice($price);
                $this->productRepository->save($product);
            } catch (\Exception $e) {
                throw new LocalizedException(__('Error updating product: %1', $e->getMessage()));
            }
        }

        return true; // this is my change
    }
}
11
  • Hi Devang, this was a copy-paste mistake, my actual code is exactly same as you suggested. but still getting same error.
    – NotEqual
    Commented Jun 25 at 6:55
  • Your filepath is also incorrect please check Commented Jun 25 at 6:56
  • Updated. still no luck. I tried creating another API with different file names, same kind of class not found error obtained.
    – NotEqual
    Commented Jun 25 at 6:59
  • Please run all commands Commented Jun 25 at 7:01
  • "message": "Class \"Test\\CsvImport\\Model\\Import\" does not exist", "code": -1, "trace": "#0 /var/www/html/testproject/vendor/magento/framework/Code/Reader/ClassReader.php(34):
    – NotEqual
    Commented Jun 25 at 7:07
0

Here is how I resolved my problem.

Generated a new module with the same API by giving the same vendor,Module and API names in the https://mage2gen.com/ website and that works. But I did update my old module similar to this and never worked. Also when I changed the Payload parameter name I obtained in the new module(param) to something else, I got the same old class not found error.

2
  • there must be some syntax error in your code Commented Jun 25 at 10:19
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jun 25 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.