We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    I have added the new tab & added the file upload field as per my requirements. I have also added some CSV checking functionality in that code. You can modify the code as per your requirement.

    So here I will be showing you the controller code to import multiple products through CSV in the gift registry.

    My Upload file name is ‘importdata’.

    You can add the below code

    // Add back tag for redirect on the edit item page while importing items through CSV
    $back = false;
    
    if ($registry->getId()) {
        $data['image'] = $registry->getImage();
    
        /*** CSV Import Functionality ***/
        if ( (isset($_FILES['importdata']['name'])) && ($_FILES['importdata']['name'] != '') )  {
            $back = true;
            try  {    
                $uploaderFactory = $this->uploaderFactory->create(['fileId' => 'importdata']);
                $uploaderFactory->setAllowedExtensions(['csv']);
                $uploaderFactory->setAllowRenameFiles(true);
                $uploaderFactory->setFilesDispersion(true);
    
                $mediaDirectory = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA);
                $destinationPath = $mediaDirectory->getAbsolutePath('import_gift_registry_products');
    
                $result = $uploaderFactory->save($destinationPath);
    
                if (!$result) {
                    throw new LocalizedException
                    (
                    __('File cannot be saved to path: $1', $destinationPath)
                    );
                }
                else {
                    $csvPath = 'import_gift_registry_products'.$result['file'];
    
                    $mediaDirectory = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA);
    
                    $destinationfilePath = $mediaDirectory->getAbsolutePath($csvPath);
    
    
                    $f_object = fopen($destinationfilePath, "r");
    
                    $column = fgetcsv($f_object);
    
                    if($f_object) {
                        if( ($column[0] == 'Item Sid') && ($column[1] == 'Local UPC') && ($column[2] == 'DCS') && ($column[3] == 'Vend Code') && ($column[4] == 'Desc1') && ($column[5] == 'Attr') && ($column[6] == 'Size') && ($column[7] == 'Price') && ($column[8] == 'Qty') && ($column[9] == 'Qty Sent') && ($column[10] == 'Qty Due') && ($column[11] == 'Ext Ord Price') && ($column[12] == 'Disc %') && ($column[13] == 'Disc$') && ($column[14] == 'Disc Rsn') && ($column[15] == 'Tax%') && ($column[16] == 'Lookup')  && ($column[17] == 'Tax$')  && ($column[18] == 'Tax') && ($column[19] == 'Assoc') ) { // check the column sequence if it is not matched then import will no proceed further   
    
                            // Delete all items before importing
                            $items = $this->itemCollectionFactory->create()
                            ->addFieldToFilter('registry.registry_id', $registry->getId())
                            ->setOrder('item_id', 'desc');
                            $remainingItems = array();
                            foreach ($items as $deleteItem) {
                                $item = $this->itemFactory->create()->load($deleteItem->getId());
                                if($item->getQtyOrdered() == 0 && $item->getQtyReceived() == 0) {
                                    $item->delete();
                                }
                                else {
                                    $remainingItems[$item->getProductId()] = $item->getId();
                                }
                            }
    
    
    
                            $positiveCount = 0;
                            $negativeCount = 0;
                            $flag = false;
                            $notExistString = '';
                            while (($columns = fgetcsv($f_object)) !== FALSE) {
    
                                $sku = $columns[0];
                                $product = $this->product->getIdBySku($sku); // Get Product By SKU
    
                                if($product) { // Checking product is empty or not
    
                                    if(array_key_exists($product,$remainingItems)) { //check product is exist or not in the remainingItems as a key
    
                                        // Update the exisitng item
                                        $item = $this->itemFactory->create()->load($remainingItems[$product]);
                                        $itemData = array('key' => $data['key'], 'form_key' => $data['form_key'], 'qty' => $columns[8], 'qty_ordered' => $item->getQtyOrdered(), 'qty_received' => $item->getQtyReceived(), 'priority_id' => $item->getPriorityId(),'note' => $item->getNote(), 'registry_id' => $registry->getId(), 'product_id' => $product, 'store_id' => 1);
                                        $item->addData($itemData);
                                        $item->save();
    
                                        $positiveCount++;
                                    }
                                    else { // Add the item
    
                                        $itemData = array('key' => $data['key'], 'form_key' => $data['form_key'], 'qty' => $columns[8], 'qty_ordered' => '0', 'qty_received' => '0', 'priority_id' => '','note' => '', 'registry_id' => $registry->getId(), 'product_id' => $product);
    
    
                                        $item = $this->itemFactory->create();
                                        $item->addData($itemData);
                                        $item->save();
    
                                        // Prepare buy request
                                        $buyRequest = new MagentoFrameworkDataObject($itemData);
                                        $buyRequest->setProduct(str_replace('product/', '', $item->getProductId()))
                                            ->setProductId($buyRequest->getProduct());
    
                                        // Add item in buyrequet
                                        $item->updateItem($buyRequest);
                                        $positiveCount++;
                                    }
                                    
                                }
                                else {
                                    $flag = true;
                                    $negativeCount++;
                                    $notExistString .= $sku .' product not exist in the magento.<br/>';
                                }
                            }                                    
    
                            if($flag) {
                                $this->messageManager->addError($notExistString);
                                $this->messageManager->addError(__('A total of %1 record(s) have not added.', $negativeCount));
                                
                            }
    
                            $this->messageManager->addSuccessMessage(__('A total of %1 record(s) have been added.', $positiveCount));
                            
                        }
                        else {
                            $this->messageManager->addError(__("invalid Formated File"));
                        }
                    }
                    else {
                        $this->messageManager->addError(__("File hase been empty"));
                    }
    
                }
            }
            catch (Exception $e) {   
               $this->messageManager->addError(__($e->getMessage()));
               $this->_redirect('*/*/edit', ['id' => $this->getRequest()->getParam('id')]);
            }
        }
    
        /*** CSV Import Functionality ***/
    }

    in the below file
    File Path: app/code/Mirasvit/Giftr/Controller/Adminhtml/Registry/Save.php

    Here I have added the code as per my requirement. You can change or modify the code as per your requirement.

    field_5bfb909c5ccae

      Get a Free Quote