I am new in magento. I saw some links about add video programmatically, but I didn't get output which is working perfect.
How to add product video programmatically to the specific product ?
Please guide me.
Thanks.
magento-2.2.5product-video
I am new in magento. I saw some links about add video programmatically, but I didn't get output which is working perfect.
How to add product video programmatically to the specific product ?
Please guide me.
Thanks.
After some goggling and with the help of some core modules I came up with following solution :
/**
* @var \Magento\Sales\Api\OrderRepositoryInterface
*/
protected $orderRepository;
/**
* @var \Magento\Catalog\Api\ProductRepositoryInterface
*/
protected $productRepository;
/**
* @var \Magento\Quote\Api\Data\CartItemInterfaceFactory
*/
protected $cartItemFactory;
/**
* @var \Magento\Quote\Api\CartRepositoryInterface
*/
protected $quoteRepository;
/**
* @var \Magento\Sales\Model\Order\ItemFactory
*/
protected $orderItemFactory;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Quote\Api\Data\CartItemInterfaceFactory $cartItemFactory,
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
\Magento\Sales\Model\Order\ItemFactory $orderItemFactory
) {
parent::__construct($context);
$this->productRepository = $productRepository;
$this->orderRepository = $orderRepository;
$this->cartItemFactory = $cartItemFactory;
$this->quoteRepository = $quoteRepository;
$this->orderItemFactory = $orderItemFactory;
}
public function execute()
{
$postData = $this->getRequest()->getPostValue();
$productSku = 'your-product-sku'; // Static Virtual Product created. You may also use static ID.
$product = $this->productRepository->get($productSku);
$order = $this->orderRepository->get($postData['order_id']);
$quote = $this->quoteRepository->get($order->getQuoteId());
try {
/* Add Quote Item Start */
$quoteItem = $this->cartItemFactory->create();
$quoteItem->setProduct($product);
$quoteItem->setCustomPrice($postData['custom_price']);
$quoteItem->setOriginalCustomPrice($postData['custom_price']);
$quoteItem->getProduct()->setIsSuperMode(true);
$quote->addItem($quoteItem);
$quote->collectTotals()->save();
/* Add Quote Item End */
/* Add Order Item Start */
$orderItem = $this->orderItemFactory->create();
$orderItem
->setStoreId($order->getStoreId())
->setQuoteItemId($quoteItem->getId())
->setProductId($product->getId())
->setProductType($product->getTypeId())
->setName($product->getName())
->setSku($product->getSku())
->setQtyOrdered(1)
->setPrice($postData['custom_price'])
->setBasePrice($postData['custom_price'])
->setOriginalPrice($postData['custom_price'])
->setBaseOriginalPrice($postData['custom_price'])
->setPriceInclTax($postData['custom_price'])
->setBasePriceInclTax($postData['custom_price'])
->setRowTotal($postData['custom_price'])
->setBaseRowTotal($postData['custom_price'])
->setRowTotalInclTax($postData['custom_price'])
->setBaseRowTotalInclTax($postData['custom_price'])
->setWeight(1)
->setIsVirtual(1);
$order->addItem($orderItem);
/* Add Order Item End */
/* Update relavant order totals Start */
$order->setBaseGrandTotal($order->getBaseGrandTotal() + $postData['custom_price']);
$order->setGrandTotal($order->getGrandTotal() + $postData['custom_price']);
$order->setBaseSubtotal($order->getBaseSubtotal() + $postData['custom_price']);
$order->setSubtotal($order->getSubtotal() + $postData['custom_price']);
$order->setBaseSubtotalInclTax($order->getBaseSubtotalInclTax() + $postData['custom_price']);
$order->setSubtotalInclTax($order->getSubtotalInclTax() + $postData['custom_price']);
$order->setTotalItemCount($order->getTotalItemCount() + 1);
$order->setTotalQtyOrdered($order->getTotalQtyOrdered() + 1);
$this->orderRepository->save($order);
/* Update relavant order totals End */
}
catch (\Exception $e) {
$this->messageManager->addError($e->getMessage());
}
}
You can get Media collection of particular product by loading it in custom helper as below:
<?php
namespace Your\Module\Helper;
use Magento\Catalog\Api\ProductRepositoryInterface;
class Data extends \Magento\Framework\Url\Helper\Data
{
public function __construct(
...
ProductRepositoryInterface $productRepository,
...
) {
...
$this->productRepository = $productRepository;
...
}
public function getMediaGalleryImages($product)
{
$_product = $this->productRepository->get($product->getSku(), false, null, true);
return $_product->getMediaGalleryImages();
}
}
Please copy
vendor/magento/module-catalog/view/frontend/templates/product/list.phtml
to your custom theme.
app/design/frontend/package/theme/Magento_Catalog/templates/product/list.phtml
Now in list.phtml file, you can call this function to load the video url if it's available otherwise show the regular image:
<?php
$video = false;
$galleryImages = $this->helper('Your\Module\Helper\Data')->getMediaGalleryImages($_product);
if ($galleryImages) {
foreach ($galleryImages as $primage) {
$imageData = $primage->getData();
// Check media type
if (isset($imageData['media_type']) && $imageData['media_type'] == 'external-video') {
$video = true;
if (strpos($imageData['video_url'], 'youtube.com') !== false) {
echo preg_replace("/\s*[a-zA-Z\/\/:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i","<iframe width=\"240\" height=\"300\" src=\"//www.youtube.com/embed/$1\" frameborder=\"0\" allowfullscreen></iframe>", $imageData['video_url']);
}elseif(strpos($imageData['video_url'], 'vimeo.com') !== false){
echo "<iframe width=\"240\" height=\"300\" src=".$imageData['video_url']." frameborder=\"0\" allowfullscreen></iframe>";
}
}
}
}
?>
<?php if(!$video){ ?>
<?= $productImage->toHtml() ?>
<?php } ?>
For vimeo, add embedded urls for product video to make them playable at list page. Please note that it would take some extra time to load page first time as we are loading product to get media collection.
Best Answer
Reference : Click here
=> Follow the step to create video :
Create a controller Test.php file on this below path :
Then, create a Processor.php file to implement video processor on this below path :
Clean cache and run it. Hope this will helpful for you !!