Skip to main content
added 81 characters in body
Source Link

Looking at Magento\Catalog\Model\ProductRepository::232 i see that the NoSuchEntityException is the one thrown by the ProductRepository when the loaded Product by sku has no entity_id (Because there is no product with that sku) and see that in the XML i know that certain SKUs are non-existent in the catalog

Looking at Magento\Catalog\Model\ProductRepository::232 i see that the NoSuchEntityException is the one thrown by the ProductRepository when the loaded Product by sku has no entity_id (Because there is no product with that sku)

Looking at Magento\Catalog\Model\ProductRepository::232 i see that the NoSuchEntityException is the one thrown by the ProductRepository when the loaded Product by sku has no entity_id (Because there is no product with that sku) and see that in the XML i know that certain SKUs are non-existent in the catalog

added 446 characters in body
Source Link

Looking at Magento\Catalog\Model\ProductRepository::232 i see that the NoSuchEntityException is the one thrown by the ProductRepository when the loaded Product by sku has no entity_id (Because there is no product with that sku)

  $productId = $this->resourceModel->getIdBySku($sku);
            if (!$productId) {
                throw new NoSuchEntityException(__('Requested product doesn\'t exist'));
            }

In the cron_schedule table shows this: {{{El producto solicitado no existe}}{{El producto solicitado no existe}}{{Requested product doesn't exist}}{{theme}}}

In the cron_schedule table shows this: {{{El producto solicitado no existe}}{{El producto solicitado no existe}}{{Requested product doesn't exist}}{{theme}}}

Looking at Magento\Catalog\Model\ProductRepository::232 i see that the NoSuchEntityException is the one thrown by the ProductRepository when the loaded Product by sku has no entity_id (Because there is no product with that sku)

  $productId = $this->resourceModel->getIdBySku($sku);
            if (!$productId) {
                throw new NoSuchEntityException(__('Requested product doesn\'t exist'));
            }

In the cron_schedule table shows this: {{{El producto solicitado no existe}}{{El producto solicitado no existe}}{{Requested product doesn't exist}}{{theme}}}

Source Link

How to capture a \Magento\Framework\Exception\NoSuchEntityException in a module

I have set up a cron job in a custom module in which reads from a XML file, search the product by Sku and then update prices. The problem is that even when i catch the exception of Magento\Catalog\Model\ProductRepository::get which is Magento\Framework\Exception\NoSuchEntityException in my module's try/catch block, when the cronjob runs automatically it throws the error and stops the cronjob

For example, if i stop the crontab jobs of Magento and manually run a php bin/magento cron:run the code just works.

Function code is:

public function processXML($xmlPath){
    $xml = $this->xmlParser;
    $coreRootPath = $this->directoryList->getRoot();
    $xml->load($coreRootPath.'/'.$xmlPath);
    $elements = $xml->xmlToArray();
    $this->logger->info('Total: '.count($elements['PRODUCTOS']['items']['PRODUCTO']));
    $currencyConverter = $this->currencyFactory->create();
    $currencyConverter->load(self::CURRENCY_FROM);
    $productRepository = $this->productRepo;
    foreach($elements['PRODUCTOS']['items']['PRODUCTO'] as $product){
      $sku = $product['CODI'];
      $qty = $product['UNIDS'];
      $price = $product['WPVP'];
      $pvenPrice = $product['PVEN'];
      $this->logger->info('$sku: '.$sku);

      //WPVP
      $convertedPricePounds = $currencyConverter->convert($price, self::CURRENCY_TO);
      $newPricePounds = round(($convertedPricePounds + 5 / 2) / 5) * 5;

      //PVEN
      $convertedPricePoundsPVEN = $currencyConverter->convert($pvenPrice, self::CURRENCY_TO);
      $newPricePoundsPVEN = round(($convertedPricePoundsPVEN + 5 / 2) / 5) * 5;

      try{
          $product = $productRepository->get($sku);
          if(is_object($product)){
            $stockData = ['qty' => $qty, 'is_in_stock' => 1];
            $product->setStockData($stockData);

            $resourceModelProduct = $this->resourceModelProduct->load($product, $product->getId());
            //EUR EN
            $this->logger->info('----- EN -----');
            $this->logger->info('$price: '.$price);
            $this->logger->info('$pvenPrice: '.$pvenPrice);
            $product->setStoreId(self::EUR_EN_STORE_ID);
            $product->setPrice($price);
            $product->setPvenPrice($pvenPrice);
            $resourceModelProduct->saveAttribute($product, 'price');
            $resourceModelProduct->saveAttribute($product, 'pven_price');

            //EUR ES
            $this->logger->info('----- ES -----');
            $this->logger->info('$price: '.$price);
            $this->logger->info('$pvenPrice: '.$pvenPrice);
            $product->setStoreId(self::EUR_ES_STORE_ID);
            $product->setPrice($price);
            $product->setPvenPrice($pvenPrice);
            $resourceModelProduct->saveAttribute($product, 'price');
            $resourceModelProduct->saveAttribute($product, 'pven_price');

            //GBP EN
            $this->logger->info('----- GB -----');
            $this->logger->info('$newPricePounds: '.$newPricePounds);
            $this->logger->info('$newPricePoundsPVEN: '.$newPricePoundsPVEN);
            $product->setStoreId(self::GBP_EN_STORE_ID);
            $product->setPrice($newPricePounds);
            $product->setPvenPrice($newPricePoundsPVEN);
            $resourceModelProduct->saveAttribute($product, 'price');
            $resourceModelProduct->saveAttribute($product, 'pven_price');

            $productRepository->save($product);
          }
      }catch(\Magento\Framework\Exception\NoSuchEntityException $e){
          $this->logger->info('SKU no existe: '.$sku);
        }
    }
  }

In the cron_schedule table shows this: {{{El producto solicitado no existe}}{{El producto solicitado no existe}}{{Requested product doesn't exist}}{{theme}}}