Commit 77b5648f by dhn

空购物车及商品详情页添加浏览过的商品列表

parent c3243b2d
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Block;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Catalog\Pricing\Price\FinalPrice;
use Magento\Framework\Pricing\Render;
class ListProduct extends \Magento\Catalog\Block\Product\AbstractProduct
{
protected $_products;
protected $_type = '';
public function setProducts($products) {
$this->_products = $products;
}
public function getProducts() {
return $this->_products;
}
public function setType($type) {
$this->_type = $type;
}
public function getType() {
return $this->_type;
}
/**
* Filter wysiwyg Content
* @param $content
* @return string
* @throws \Exception
*/
public function renderContent($content)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$filterProvider = $objectManager->get('\Magento\Cms\Model\Template\FilterProvider');
return $filterProvider->getBlockFilter()->filter($content);
}
public function getCategoryAds(&$start, $isMobile=false) {
$itemCnt = $this->getRequest()->getParam('itemCnt') ?? 0;
$start = $itemCnt + 1;
$ads = array();
if ('catalog_category_view' == $this->getRequest()->getFullActionName()) {
$category = $this->_coreRegistry->registry('current_category');
if ($category) {
$type = ($isMobile ? 'mobile' : 'pc');
$content = $category->getData('cat_list_ads_' . $type);
if ($content) {
$content = str_replace(',', ',', $content);
$content = explode(PHP_EOL, $content);
foreach ($content as $row) {
$ad = explode(',', $row);
if (count($ad) < 3) {
continue;
}
$ads[trim($ad[0])] = array('image' => trim($this->renderContent($ad[1])), 'link' => trim($ad[2]));
}
}
}
}
return $ads;
}
public function isThreeColumns() {
if ('catalog_category_view' == $this->getRequest()->getFullActionName()) {
$category = $this->_coreRegistry->registry('current_category');
if ($category) {
return $category->getData('is_three_columns');
}
}
return false;
}
public function getProductPrice(Product $product)
{
$priceRender = $this->getPriceRender();
$price = '';
if ($priceRender) {
$price = $priceRender->render(
FinalPrice::PRICE_CODE,
$product,
[
'include_container' => true,
'display_minimal_price' => true,
'zone' => Render::ZONE_ITEM_LIST,
'list_category_page' => true
]
);
}
return $price;
}
protected function getPriceRender()
{
$priceRender = $this->getLayout()->getBlock('product.price.render.default');
if (!$priceRender) {
$priceRender = $this->getLayout()->createBlock(
\Magento\Framework\Pricing\Render::class,
'product.price.render.default',
['data' => ['price_render_handle' => 'catalog_product_prices']]
);
}
return $priceRender;
}
}
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Controller\Product;
use Magento\Framework\App\Action\Context;
class Recently extends \Magento\Framework\App\Action\Action
{
protected $_objectManager;
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManager,
Context $context
) {
$this->_objectManager = $objectManager;
parent::__construct($context);
}
public function execute()
{
$result = ['status' => 'fail'];
$request = $this->getRequest();
if ($request->isPost() && $request->isAjax()) {
$productIds = $request->getParam('ids');
if (substr($productIds, -1) == ',') {
$productIds = substr($productIds, 0, -1);
}
$in = explode(',', $productIds);
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
$productCollection = $this->_objectManager->get('Magento\Catalog\Model\ResourceModel\Product\Collection');
$productCollection->addAttributeToSelect('*')
->addFieldToFilter('status', 1)
->addAttributeToFilter('entity_id', array( 'in' => $in));
$productCollection->getSelect()->order(sprintf("field(e.entity_id,%s)", $productIds));
/** @var \Magento\CatalogInventory\Helper\Stock $stockHelper */
$stockHelper = $this->_objectManager->get('Magento\CatalogInventory\Helper\Stock');
$stockHelper->addInStockFilterToCollection($productCollection);
if (count($productCollection) > 0) {
$result['status'] = 'success';
/** @var Magento\Catalog\Block\ListProduct $block */
$block = $this->_view->getLayout()->createBlock('Magento\Catalog\Block\ListProduct');
$block->setProducts($productCollection);
$block->setType('recently');
$block->setTemplate('Magento_Catalog::product/list-new.phtml');
$result['html'] = $block->toHtml();
}
}
return $this->getResponse()->representJson(json_encode($result));
}
}
\ No newline at end of file
<?php
/**
* @var $block \Magento\Ui\Block\Wrapper
*/
?>
<div class="recently-viewed" style="display: none;">
<?php if ($titleBlockId = $block->getTitleBlockId()): ?>
<?php $titleBlock = $block->getLayout()->createBlock(Magento\Cms\Block\Block::class); ?>
<?php $titleBlock->setBlockId($titleBlockId); ?>
<?= $titleBlock->toHtml() ?>
<?php endif ?>
<div class="content"></div>
</div>
<script>
require(['jquery', 'jquery.owlCarousel'], function ($) {
var isMobile = window.matchMedia("(pointer:coarse)").matches;
var productIds = localStorage.getItem('recently_viewed') || '';
if (productIds != '') {
console.log('productIds',productIds)
$.ajax({
url: '/catalog/product/recently',
data: {ids: productIds},
type: 'post',
dataType: 'json',
success: function (result) {
if (result.status == 'success') {
$('.recently-viewed>.content').html(result.html);
$('.recently-viewed').show();
$('.recently-viewed .owl-carousel').owlCarousel({
nav: true,
dots: false,
navText: [],
margin: isMobile?10:20,
items: isMobile?2:5,
slideBy: isMobile?2:5
});
}
if(isMobile)
{
$('.owl-nav .owl-prev').html('<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M16.67 0l2.83 2.829-9.339 9.175 9.339 9.167-2.83 2.829-12.17-11.996z"></path></svg>')
$('.owl-nav .owl-next').html('<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M5 3l3.057-3 11.943 12-11.943 12-3.057-3 9-9z"></path></svg>')
}
else
{
$('.owl-nav .owl-prev').html('<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M16.67 0l2.83 2.829-9.339 9.175 9.339 9.167-2.83 2.829-12.17-11.996z"></path></svg>')
$('.owl-nav .owl-next').html('<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M5 3l3.057-3 11.943 12-11.943 12-3.057-3 9-9z"></path></svg>')
}
}
});
}
});
</script>
\ No newline at end of file
......@@ -9,5 +9,15 @@
<body>
<referenceBlock name="product.info.description" remove="true" />
<referenceBlock name="product.attributes" remove="true" />
<referenceContainer name="content">
<block class="Magento\Catalog\Block\Product\View" name="product.recently_viewed.collector" template="Magento_Catalog::product/view/recently_viewed/collector.phtml" after="product.info.details"/>
</referenceContainer>
<referenceContainer name="content.aside">
<block class="Magento\Cms\Block\Block" name="checkout.cart.empty.recently" before="catalog.product.related">
<arguments>
<argument name="block_id" xsi:type="string">recently_viewed</argument>
</arguments>
</block>
</referenceContainer>
</body>
</page>
<?php
/**
* Product list template
*
* @var $block \Magento\Catalog\Block\ListProduct
*/
$templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$pr = $objectManager->create('Magento\Catalog\Model\ProductRepository');
$blocklist = $objectManager->get('\Magento\Catalog\Block\ListProduct');
$imageHelper = $objectManager->get( 'Magento\Catalog\Helper\Image' );
$products = $block->getProducts();
$type = $block->getType();
?>
<style>
@media(max-width: 768px){
}
@media(min-width: 768px){
.products-list{
max-width:1600px;
}
.products-list .products_item{
border:1px solid #fff;
}
.products-list .products_item a{
padding:0;
background: none;
color:#000;
font-size: 1rem;
}
.products-list .products_item:hover{
border-radius: ;
}
.products-title h2{
text-transform:uppercase;
font-size: 24px;
font-weight: bold;
color: #000000;
margin: 0px;
padding: 1rem;
text-align: center;
}
.catalog-product-view .products-title h2{
font-size: 16px;
text-transform:capitalize;
}
}
</style>
<div class="products-title">
<h2><?= $type ?> Products</h2>
</div>
<div class="products-list items product-items owl-carousel owl-theme <?= $type ?>" >
<?php
foreach($products as $_product):
$id = $_product->getId();
$product = $pr->getById($id);
if(!$product) continue;
$productImage = $imageHelper->init( $product, 'category_page_list' ) ->getUrl();
?>
<div class="products_item">
<a href="<?php echo $product->getProductUrl()?>" >
<img src="<?php echo $productImage;?>" loading="lazy" />
</a>
<div class="flash-sale-info">
<a class="product-name" href="<?php echo $product->getProductUrl()?>"><?php echo $product->getName();?></a>
<?= /* @noEscape */ $blocklist->getProductPrice($product) ?>
</div>
</div>
<?php
endforeach;
?>
</div>
<?php
/** @var \Magento\Catalog\Block\Product\View $block */
?>
<script type="text/javascript">
require(['jquery'], function ($) {
var id = <?= $block->getProduct()->getId() ?>;
var recentlyViewed = localStorage.getItem('recently_viewed') || '';
if (recentlyViewed != '') {
recentlyViewed = recentlyViewed.replace(id + ',', '');
if (recentlyViewed.split(',').length - 1 == 10) {
recentlyViewed = recentlyViewed.replace(/[\d]*,$/, '')
}
}
recentlyViewed = id + ',' + recentlyViewed;
localStorage.setItem('recently_viewed', recentlyViewed);
});
</script>
......@@ -53,6 +53,12 @@
</argument>
</arguments>
</referenceBlock>
<referenceContainer name="checkout.cart.noitems">
<block class="Magento\Cms\Block\Block" name="checkout.cart.empty.recently">
<arguments>
<argument name="block_id" xsi:type="string">recently_viewed</argument>
</arguments>
</block>
</referenceContainer>
</body>
</page>
......@@ -799,6 +799,9 @@ p.shopbycate-title {
.checkout-cart-index .cart-container .cart.actions {
border-top:none;
}
.checkout-cart-index .recently-viewed {
margin-bottom: 2rem;
}
.checkout-cart-index .cart-container .checkout-methods-items .checkout-now button{
height: 50px;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment