2

I am using Magento 2.3 and creating a product doesn't need any image. How do I make it required?

2
  • If you want to image required, you can set Values Required to yes for Image Gallery Attribute.
    – Pawan
    Jul 16, 2020 at 6:37
  • Hi pawan. I am using vendor marketplace extension. In vendor login, I want product image required. How can I change gallery.html file.
    – gill
    Jul 16, 2020 at 6:47

1 Answer 1

0

There are a lot of steps to do here but they could be implemented:

  1. Set the Media Gallery Values Required to Yes like the following enter image description here

This will make the image required for creating new services, but I found this field has validation issues while updating so I believe you need to create your own plugin to handle its validation yourself.

  1. In your di.xml file add the following:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Product\Validator">
         <plugin name="worxmart_service_validate_media_gallery"
                 type="Vendor\Extension\Plugin\ValidateMediaGallery"/>
    </type>
</config>

Then add the following to your ValidateMediaGallery.php class:

<?php

namespace Vednor\Extension\Plugin;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Validator;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;

class ValidateMediaGallery
{
    /**
    * @param Validator $subject
    * @param Product $product
    * @param RequestInterface $request
    * @param DataObject $response
    * @return array
    * @throws LocalizedException
    */
    public function beforeValidate(Validator $subject, Product $product, RequestInterface $request, DataObject $response): array
    {
        $attribute = $product->getResource()->getAttribute(ProductInterface::MEDIA_GALLERY);
        $attrCode = $attribute->getAttributeCode();
        $productData = $request->getPost('product', []);

        if (empty($productData[$attrCode])) {
            $label = $attribute->getFrontend()->getLabel();
            throw new LocalizedException(
                 __('The "%1" attribute value is empty. Set the attribute and try again.', $label)
            );
        }

        $mediaGalleryEmpty = true;
        // To avoid deleting all product pics
        foreach ($productData[$attrCode]['images'] as $image) {
            if (empty($image['removed'])) {
                $mediaGalleryEmpty = false;
                break;
            }
        }

        if ($mediaGalleryEmpty) {
            $label = $attribute->getFrontend()->getLabel();
            throw new LocalizedException(
                __('The "%1" attribute value is empty. Set the attribute and try again.', $label)
            );
        }

        return [$product, $request, $response];
    }
}
  1. For setting the maximum number I believe you need to overwrite the file uploader by overwriting the method add for $('#fileupload').fileupload() to stop adding if it gets to the maximum.

Hint: this answer is open for contribution as I wish it to help someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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