0

I am using magneto2.3.0 and I already free plugin use for custom attachment for product edit page but there PDF attached after product not save it.

Issues come in magneto2.3.0 version if anyone tries to this version please reply to me just simple PDF file added in product edit page admin side after saving the product.

0

You can create a new attribute in your product. With this is not necessary use any plugin.

 <?php

namespace Vendor\Catalog\Setup;

use Magento\Eav\Model\Config;
use Magento\Eav\Setup\EavSetup; 
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Setup\CategorySetupFactory;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Eav\Api\AttributeRepositoryInterface;

class UpgradeData implements UpgradeDataInterface {

private $eavSetupFactory;
private $attributeSetFactory;
private $attributeSet;
private $categorySetupFactory;  

public function __construct(
    Config $eavConfig,
    EavSetupFactory $eavSetupFactory,
    AttributeSetFactory $attributeSetFactory,
    CategorySetupFactory $categorySetupFactory,
    AttributeRepositoryInterface $attributeRepositoryInterface
    ) {
    $this->eavConfig = $eavConfig;
    $this->eavSetupFactory = $eavSetupFactory; 
    $this->attributeSetFactory = $attributeSetFactory; 
    $this->attributeRepository = $attributeRepositoryInterface;
    $this->categorySetupFactory = $categorySetupFactory; 
} 

public function upgrade(
    ModuleDataSetupInterface $setup,
    ModuleContextInterface $context
    ) {
    $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

    if (version_compare($context->getVersion(), '0.0.13', '<')) {

        $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'your_attr_name');

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY, 'your_attr_name', [
            'group' => 'Ficha Técnica',
            'type' => 'varchar',
            'label' => 'Attr Name',
            'input' => 'file',
            'backend' => 'Vendor\Catalog\Model\Product\Attribute\FileUpload',
            'frontend' => '',
            'class' => '',
            'source' => '',
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'visible' => true,
            'required' => false,
            'user_defined' => false,
            'default' => '',
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => true,
            'unique' => false,
            'used_in_product_listing' => true,
            'sort_order' => 3
            ]);

    }

}

}

The backend class will save this attribute. Vendor/Catalog/Model/Product/Attribute/FileUpload.php

namespace Vendor\Catalog\Model\Product\Attribute;

use Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\MediaStorage\Model\File\UploaderFactory;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Driver\File;
use Psr\Log\LoggerInterface;

class FileUpload extends AbstractBackend {

    protected $_uploaderFactory;

    protected $_filesystem;

    protected $_fileUploaderFactory;

    protected $_logger;

    protected $_file;

    public function __construct(LoggerInterface $logger, Filesystem $filesystem, UploaderFactory $fileUploaderFactory, File $file)
    {
        $this->_filesystem = $filesystem;
        $this->_fileUploaderFactory = $fileUploaderFactory;
        $this->_logger = $logger;
        $this->_file = $file;
    }

    public function afterSave($object)
    {
        $attributeCode = $this->getAttribute()->getAttributeCode();

        $value = $object->getData($attributeCode);
        $deleteValue = $object->getData($attributeCode . '_delete');

        $this->prepareFiles($attributeCode);

        // Delete old image or unset
        if ((($value ?? false) && ($_FILES[$attributeCode]['tmp_name'] ?? false)) || 
            (($value ?? false) && ($deleteValue ?? false))
        ) {
            try {
                $this->deleteFile($value);
                $object->setData($attributeCode, null);
            } catch (\Exception $e) {
                throw new FrameworkException($e->getMessage());
            }

        }

        if ($_FILES[$attributeCode]['tmp_name'] ?? false) {
            try {
                $file = $this->saveFile($attributeCode);
                $object->setData($attributeCode, $file['file']);
            } catch (\Exception $e) {
                throw new FrameworkException($e->getMessage());
            }
        }

        $this->getAttribute()->getEntity()->saveAttribute($object, $attributeCode);

        return parent::afterSave($object);
    }

    private function prepareFiles($attributeCode)
    {
        if ($_FILES['product'] ?? false) {
            foreach($_FILES['product'] as $value => $key) {
                if ($key[$attributeCode] ?? false) {
                    $_FILES[$attributeCode][$value] = $key[$attributeCode];
                    unset($_FILES[$value][$key][$attributeCode]);
                }
            }
        }
    }

    private function saveFile($attributeCode)
    {
        $path = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath( 'catalog/product/files/' );

        $uploader = $this->_fileUploaderFactory->create(['fileId' => $attributeCode]);
        $uploader->setAllowRenameFiles(true);
        $uploader->setFilesDispersion(false);
        $uploader->setAllowCreateFolders(true);

        return $uploader->save($path);
    }

    private function deleteFile($fileName)
    {
        $basePath = 'catalog/product/files/';

        $imagePath = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath($basePath . $fileName);

        if ($this->_file->isExists($imagePath)) {
            $this->_file->deleteFile($imagePath);
        }
    }
}
  • In this path vendor/magento/module-catalog/Model/Product/Attribute we are create FileUpload.php or not? please brief descibe me – Rv Singh Jul 15 at 14:32
  • Yes, we need to create this. This module will extend the standard magento-catalog. – Gabriel Fernandes Jul 15 at 14:41
  • In this file define use and namespace Vendor/Catalog/Setup/UpgradeData.php please provide full code so easily put it – Rv Singh Jul 15 at 14:44
  • I have check it in this path Vendor/Catalog/Setup/UpgradeData.php there other file "CategorySetup.php" open it and check it there code define more than so i think setup file code missing it in your side – Rv Singh Jul 15 at 14:45
  • I just updated my answer, please check it. – Gabriel Fernandes Jul 15 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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