Tuesday 26 June 2018

Load denied by X-Frame-Options: does not permit cross-origin framing in Magento 2


Step 1 : Open magentoroot/app/etc/env.php 
Step 2 : Find -> 'x-frame-options' => 'SAMEORIGIN',
Step 3 : Replace -> 'x-frame-options' => 'GOFORIT',

Monday 25 June 2018

Delete operation is forbidden for current area in Magento 2

You can use following code for Delete operation is forbidden for current area in products or any un secured area // Here is for product delete

$registry = $objectManager->get('\Magento\Framework\Registry');
$registry->register('isSecureArea', true);
$removeproduct = $objectManager->create('\Magento\Catalog\Model\Product');
$removeproduct->load(21); / Product id
$removeproduct->delete();

Product image roles randomly disappear when update product through the Rest API

I think its issue on Magento 2.2.3 or below version

Open root/app/code/Magento/Catalog/Model/ProductRepository.php

protected function processMediaGallery(ProductInterface $product, $mediaGalleryEntries)
{

find : if (isset($entry['value_id']) // near line no : 499 TO
if (isset($entry['value_id']) && $entry['value_id'] != '') {

}

Saturday 23 June 2018

Remove special characters from string php

$title = 'Apple Fritter E Juice 120ml By Loaded E Liquid';
$urltitle = str_replace(' ', '-', $title);
$urlkey = preg_replace("/[^a-zA-Z0-9]+/[^a-zA-Z0-9]+/", "", strtolower($urltitle));
echo $urlkey;

How to Import / Insert / Add product image from URL in Magento 2

use Magento\Framework\App\Action\Context;

class Createproduct extends \Magento\Framework\App\Action\Action {

    protected $directoryList;
    protected $file;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory,
        \Magento\Framework\App\Filesystem\DirectoryList $directoryList,
        \Magento\Framework\Filesystem\Io\File $file) {

        $this->_resultpageFactory = $pageFactory;
        $this->directoryList = $directoryList;
        $this->file = $file;
        return parent::__construct($context);
    }


    protected function getMediaDirTmpDir()
    {
        return $this->directoryList->getPath($this->directoryList::MEDIA) . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR;
    }

    public function setImage($product, $imageUrl, $visible = false, $imageType = [])
    {
        $tmpDir = $this->getMediaDirTmpDir();

        $this->file->checkAndCreateFolder($tmpDir);

        $newFileName = $tmpDir . baseName($imageUrl);


        $result = $this->file->read($imageUrl, $newFileName);
        if ($result) {
            $product->addImageToMediaGallery($newFileName, $imageType, true, $visible);
        }
        return $result;
    }

    public function execute() {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->create('\Magento\Catalog\Model\Product')->load(1);
        $imagePath = "https://domain.com/image.jpg"; // path of the image
            $this->setImage($product, $imagePath, false, $imageType = ['image', 'small_image', 'thumbnail']);
            $product->save();
    }
}

How to check image is valid or not in php from URL in PHP

$imagePath = "https://jaydip.kansagra.com/image.jpg";
if (@getimagesize($imagePath)) {
   echo 'Image exists';
} else {
      echo 'Image not exists';
}

Friday 15 June 2018

When create child product of configurable product so why its create virtual product in magento 2

You do not set the weight before generation of children's products. Depending on this field the Magento decides what type of product is created.