javascript - 在 Fotorama - Magento 2 产品页面添加自定义视频
问题描述
我正在尝试将自定义视频添加到产品页面上的 Fotorama 滑块,我已在此路径上的服务器存储中上传该视频:pub/media/videos
.
我已经尝试过这段代码,但没有像我预期的那样工作......
<script type="text/javascript">
require(['jquery'], function ($) {
$(document).on('gallery:loaded', function () {
var $fotorama = jQuery('div.gallery-placeholder > div.fotorama');
var fotorama = $fotorama.data('fotorama');
$fotorama.on('fotorama:load', function fotorama_onLoad(e, fotorama, extra) {
if (extra.frame.type === 'iframe') {
extra.frame.$stageFrame.html('<iframe align="middle" type="text/html" width="100%" height="100%" src="' + extra.frame.src + '" frameborder="0" scrolling="no" allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen></iframe>');
}
});
fotorama.push({
thumb: 'pub/media/videos/product/video1.mp4',
'src': 'pub/media/videos/product/video1.mp4',
type: 'iframe',
caption: '<set your caption>'
});
});
});
</script>
解决方案
通过 JavaScript 添加
let video = {
thumbnail: thumbnail,
url: url
};
$.when(getGallery()).then(function (result) {
result.fotorama.data.push({
videoUrl: video.url,
src: video.url,
img: video.thumbnail,
isMain: false,
thumb: video.thumbnail,
type: 'video'
});
}, function (error) {
console.log(error);
});
getGallery: function () {
let dfd = $.Deferred();
let timer = setInterval(function () {
let gallery = $('[data-gallery-role=gallery-placeholder]').data('gallery');
if (typeof gallery != 'undefined') {
clearInterval(timer);
dfd.resolve(gallery);
}
}, 500);
return dfd.promise();
}
使用 PHP 通过模块添加
文件 app/code/My/Module/etc/di.xml
<?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\Block\Product\View\Gallery">
<plugin name="insertProductVideo" type="My\Module\Plugin\Gallery\Gallery" sortOrder="5"/>
</type>
</config>
文件 app/code/My/Module/Plugin/Gallery/Gallery.php
<?php
namespace My\Module\Plugin\Gallery;
class Gallery
{
public function afterGetGalleryImagesJson(ImageGallery $subject, $result)
{
$product = $subject->getProduct();
if ($product->getVideoPath()) {
$imagesSettings = \Zend_Json::decode($result);
$imagesSettings[] = [
'caption' => '',
'position' => count($imagesSettings) -1,
'isMain' => false,
'type' => 'video',
'videoUrl' => $product->getVideoPath(), // path to product video like /pub/media/video/800x800_8bit_MP4_H264_quality50.mp4
'src' => $product->getVideoPath(), // path to product video like /pub/media/video/800x800_8bit_MP4_H264_quality50.mp4
'img' => $product->getPlaceholder(), // path to video placeholder image like /pub/media/placeholder/800x800_placeholder.png
'thumb' => $product->getPlaceholder(), // path to video placeholder image like /pub/media/placeholder/800x800_placeholder.png
];
$result = \Zend_Json::encode($imagesSettings);
}
return $result;
}
}
推荐阅读
- tomcat - 在 Tomcat 的 servlet 上编码俄语和中文文本的问题
- azure - 将带有特殊参数的变量传递给 ARM 模板时出错
- ios - Xcode 11:使用 dyld 进行“pod install”后应用程序在启动时崩溃:未加载库:@rpath/
- java - 如何为仅使用必需列映射的实体选择特定列
- ios - 如何将 SKShapeNode 约束到 SpriteKit 中的设备边缘?
- ruby-on-rails - 有没有办法在 ruby 中使用 collect 执行 if-else 并更新数组
- json - 使用 Application/JSON 将 PDF 表单字段 (Acrobat) 提交到 HTTP Post
- c# - 对 asp.net C# 代码的等效 CURL 请求是什么
- python - re.sub() 在不匹配时给出 Nameerror
- php - Laravel - makeVisible 不会使隐藏属性可见