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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
// 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 \Magento\Framework\DataObject($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.
[crayon-63e0a04b951c4781305012/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.