1

i am trying to create downloadable products programmatically. The product data are saved into a xml file. The downloadable data are PDFs and not URLs. When only one xml file is into the data folder, the product get created. But when more than one xml file with product data are into the folder, i become following Exception:

Exception #0 (Magento\Framework\Exception\NoSuchEntityException): Es gibt keinen herunterladbaren Link mit der vorgesehen ID. "There is no downloadable link with provided ID"

Here is the code:

$storeId=$this->_storeManager->getStore()->getId();

        $generatedRandomString=$this->generateRandomString();
        $productSku="imgdwnld-".$bildElement["eventid"]."-".$bildElement["bildid"]."-".$bildElement["name"]."-".$generatedRandomString;
        $productUrl=$productSku;
        $bildName=$bildElement["name"];
        $productName="Bildbestellung-download";

        $newDownloadProduct = $this->_productModel;
        $newDownloadProduct->setSku($productSku); // Set your sku here
        $newDownloadProduct->setName($productName); // Name of Product
        $newDownloadProduct->setStatus(1); // Status on product enabled/ disabled 1/0
        $newDownloadProduct->setProductHasWeight(0); // if product has weight
        $newDownloadProduct->setUrlKey($productSku); // if product has weight
        $newDownloadProduct->setVisibility(1); // visibilty of product (catalog / search / catalog, search / Not visible individually)
        $newDownloadProduct->setTaxClassId(1); // Tax class id
        $newDownloadProduct->setTypeId('downloadable'); // Type of product (simple/virtual/downloadable/configurable)
        $newDownloadProduct->setPrice("0.99");  // price of product
        //$newDownloadProduct->setFullEditionDate($date);
        $newDownloadProduct->setWebsiteIds(array(1));
        $newDownloadProduct->setCategoryIds(array(5));
        $newDownloadProduct->setAttributeSetId(11);     
        $newDownloadProduct->setStockData(
         array(
            'use_config_manage_stock' => 0,
            'manage_stock' => 1,
            'is_in_stock' => 1,
            'qty' => 9999999
             )
         );
         $newDownloadProduct->setLinksExist(false);
         $newDownloadProduct->setCreatedAt(strtotime('now'));
         $newDownloadProduct->setStoreId($storeId);
        $newDownloadProduct->save();

        $newDownloadProduct=$this->_productRepository->get($productSku);



        $existingMediaGalleryEntries = $newDownloadProduct->getMediaGalleryEntries();
        foreach ($existingMediaGalleryEntries as $key => $entry) {
        unset($existingMediaGalleryEntries[$key]);
        }
        $newDownloadProduct->setMediaGalleryEntries($existingMediaGalleryEntries);
        //$productRepository->save($product);
        /*Add Images To The Product*/
        //$imagePath = $imagePath; // path of the image
        $imagePath =$basePathForFotos.$bildName; // path of the image
        $newDownloadProduct->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);
        $newDownloadProduct->save();

        //Generate Link Data (File to download)

        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/images.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info(date("H:i:s")." - ".$productSku);


        $b64img = chunk_split(base64_encode(file_get_contents($imagePath)));
        $this->_linkInterface->setLinksTitle($bildName);
        $this->_linkInterface->setTitle($bildName);
        $this->_linkInterface->setPrice(0);
        //$this->_linkInterface->setId($generatedRandomString);
        $this->_linkInterface->setNumberOfDownloads(3);
        $this->_linkInterface->setIsShareable(1);
        $this->_contenInterface->setFileData($b64img);
        $this->_contenInterface->setName($bildName);
        $this->_linkInterface->setLinkType('file'); 
        $this->_linkInterface->setLinkFile("/files/wzfotos/IMAGES/py140125ver005.jpg"); 
        $this->_linkInterface->setSortOrder(0);
        ;
        $this->_linkInterface->setLinkFileContent($this->_contenInterface);
        $this->_linkRepositoryInterface->save($productSku, $this->_linkInterface); // param1 is the sku of your product

        //Add Product into cart
        $newDownloadProduct=$this->_productRepository->get($productSku);
        $newDownloadProductId=$newDownloadProduct->getId();
        $sessionId=$this->_sessionManager->getSessionId();

        $params = array(
        'product' => $newDownloadProductId,
        'qty' => $bildElement["anzahl"],
        "options"=>array(
        //"image_path"=>$imageFile,
        "unique_maker"=>serialize(array(time()))
            )
        );  
        //$copiedProduct->addCustomOption('additional_options', serialize($additionalOptions));     
        $this->_cart->addProduct($newDownloadProductId,$params);    
        $this->_cart->save();  

Here is the constructor:

public function __construct(\Magento\Framework\Session\SessionManager $sessionManager,\Magento\Catalog\Model\Product $productModel,
\Magento\Checkout\Model\Cart $cart, \Magento\Framework\App\Action\Context $context, \Adnord\Images\Model\Product\Copier $productCopier,
\Magento\Framework\App\State $appState, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Downloadable\Api\Data\File\ContentInterface $contentInterface,
\Magento\Downloadable\Api\LinkRepositoryInterface $linkRepositoryInterface,
\Magento\Downloadable\Api\Data\LinkInterface $linkInterface,
\Magento\Store\Model\StoreManagerInterface $storeManager
//,\Magento\Downloadable\Model\Link $link
){
    $this->_productModel=$productModel;
    $this->_productCopier=$productCopier;
    $this->_productRepository=$productRepository;
    $this->_sessionManager=$sessionManager;
    $this->_cart=$cart;
    $this->_linkInterface=$linkInterface;
    $this->_contenInterface=$contentInterface;
    $this->_linkRepositoryInterface=$linkRepositoryInterface;
    $this->_storeManager=$storeManager;
    //$this->_link=$link;
    parent::__construct($context);
}

I have this problem only into an iteration. Something is going wrong with the Ids of the Links.

Can someone please help me?

Thank you.

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.