-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
738 additions
and
2 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,270 @@ | ||
<?php | ||
|
||
namespace Xigen\Special\Block\Widget; | ||
|
||
/** | ||
* Special Widget class | ||
*/ | ||
class Special extends \Magento\Catalog\Block\Product\NewProduct implements \Magento\Widget\Block\BlockInterface | ||
{ | ||
/** | ||
* Default value whether show pager or not | ||
*/ | ||
const DEFAULT_SHOW_PAGER = false; | ||
|
||
/** | ||
* Default value for products per page | ||
*/ | ||
const DEFAULT_PRODUCTS_PER_PAGE = 5; | ||
|
||
/** | ||
* Name of request parameter for page number value | ||
* @deprecated | ||
*/ | ||
const PAGE_VAR_NAME = 'fp'; | ||
|
||
/** | ||
* Instance of pager block | ||
* @var \Magento\Catalog\Block\Product\Widget\Html\Pager | ||
*/ | ||
protected $_pager; | ||
|
||
/** | ||
* @var \Magento\Framework\Serialize\Serializer\Json | ||
*/ | ||
private $serializer; | ||
|
||
/** | ||
* Special constructor. | ||
* @param \Magento\Catalog\Block\Product\Context $context | ||
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory | ||
* @param \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility | ||
* @param \Magento\Framework\App\Http\Context $httpContext | ||
* @param array $data | ||
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer | ||
*/ | ||
public function __construct( | ||
\Magento\Catalog\Block\Product\Context $context, | ||
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, | ||
\Magento\Catalog\Model\Product\Visibility $catalogProductVisibility, | ||
\Magento\Framework\App\Http\Context $httpContext, | ||
array $data = [], | ||
\Magento\Framework\Serialize\Serializer\Json $serializer = null | ||
) { | ||
parent::__construct( | ||
$context, | ||
$productCollectionFactory, | ||
$catalogProductVisibility, | ||
$httpContext, | ||
$data | ||
); | ||
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() | ||
->get(\Magento\Framework\Serialize\Serializer\Json::class); | ||
} | ||
|
||
/** | ||
* Product collection initialize process | ||
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection|Object|\Magento\Framework\Data\Collection | ||
*/ | ||
protected function _getProductCollection() | ||
{ | ||
return $this->_getSpeciaProductsCollection(); | ||
} | ||
|
||
/** | ||
* Prepare collection for recent product list | ||
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection|Object|\Magento\Framework\Data\Collection | ||
*/ | ||
protected function _getSpeciaProductsCollection() | ||
{ | ||
/** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */ | ||
$collection = $this->_productCollectionFactory->create(); | ||
$collection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds()); | ||
|
||
$date = new \Zend_Date(); | ||
|
||
$collection = $this->_addProductAttributesAndPrices($collection) | ||
->addStoreFilter() | ||
->addAttributeToFilter( | ||
'special_from_date', | ||
[ | ||
'or' => [ | ||
0 => [ | ||
'date' => true, | ||
'to' => $date->get('YYYY-MM-dd') . ' 23:59:59' | ||
], | ||
1 => [ | ||
'is' => new \Zend_Db_Expr('null') | ||
], | ||
] | ||
], | ||
'left' | ||
) | ||
->addAttributeToFilter( | ||
'special_to_date', | ||
[ | ||
'or' => [ | ||
0 => [ | ||
'date' => true, | ||
'from' => $date->get('YYYY-MM-dd') . ' 00:00:00' | ||
], | ||
1 => [ | ||
'is' => new \Zend_Db_Expr('null') | ||
], | ||
] | ||
], | ||
'left' | ||
) | ||
->addAttributeToFilter('special_price', ['gt' => '0.1']) | ||
->addAttributeToFilter('price', ['gt' => '0.1']) | ||
->addAttributeToFilter('special_price', ['lt' => new \Zend_Db_Expr('at_price.value')]) | ||
->addAttributeToSort('created_at', 'desc') | ||
->setPageSize($this->getPageSize()) | ||
->setCurPage($this->getCurrentPage()); | ||
|
||
return $collection; | ||
} | ||
|
||
/** | ||
* Get number of current page based on query value | ||
* @return int | ||
*/ | ||
public function getCurrentPage() | ||
{ | ||
return abs((int) $this->getRequest()->getParam($this->getData('page_var_name'))); | ||
} | ||
|
||
/** | ||
* Get key pieces for caching block content | ||
* @return array | ||
*/ | ||
public function getCacheKeyInfo() | ||
{ | ||
return array_merge( | ||
parent::getCacheKeyInfo(), | ||
[ | ||
$this->getProductsPerPage(), | ||
(int) $this->getRequest()->getParam($this->getData('page_var_name'), 1), | ||
$this->serializer->serialize($this->getRequest()->getParams()) | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Retrieve how many products should be displayed | ||
* @return int | ||
*/ | ||
public function getProductsCount() | ||
{ | ||
if (!$this->hasData('products_count')) { | ||
return parent::getProductsCount(); | ||
} | ||
return $this->getData('products_count'); | ||
} | ||
|
||
/** | ||
* Retrieve how many products should be displayed | ||
* @return int | ||
*/ | ||
public function getProductsPerPage() | ||
{ | ||
if (!$this->hasData('products_per_page')) { | ||
$this->setData('products_per_page', self::DEFAULT_PRODUCTS_PER_PAGE); | ||
} | ||
return $this->getData('products_per_page'); | ||
} | ||
|
||
/** | ||
* Return flag whether pager need to be shown or not | ||
* @return bool | ||
*/ | ||
public function showPager() | ||
{ | ||
if (!$this->hasData('show_pager')) { | ||
$this->setData('show_pager', self::DEFAULT_SHOW_PAGER); | ||
} | ||
return (bool) $this->getData('show_pager'); | ||
} | ||
|
||
/** | ||
* Retrieve how many products should be displayed on page | ||
* @return int | ||
*/ | ||
protected function getPageSize() | ||
{ | ||
return $this->showPager() ? $this->getProductsPerPage() : $this->getProductsCount(); | ||
} | ||
|
||
/** | ||
* Render pagination HTML | ||
* @return string | ||
*/ | ||
public function getPagerHtml() | ||
{ | ||
if ($this->showPager()) { | ||
if (!$this->_pager) { | ||
$this->_pager = $this->getLayout()->createBlock( | ||
\Magento\Catalog\Block\Product\Widget\Html\Pager::class, | ||
'widget.special.product.list.pager' | ||
); | ||
|
||
$this->_pager->setUseContainer(true) | ||
->setShowAmounts(true) | ||
->setShowPerPage(false) | ||
->setPageVarName($this->getData('page_var_name')) | ||
->setLimit($this->getProductsPerPage()) | ||
->setTotalLimit($this->getProductsCount()) | ||
->setCollection($this->getProductCollection()); | ||
} | ||
if ($this->_pager instanceof \Magento\Framework\View\Element\AbstractBlock) { | ||
return $this->_pager->toHtml(); | ||
} | ||
} | ||
return ''; | ||
} | ||
|
||
/** | ||
* Return HTML block with price | ||
* @param \Magento\Catalog\Model\Product $product | ||
* @param string $priceType | ||
* @param string $renderZone | ||
* @param array $arguments | ||
* @return string | ||
* @SuppressWarnings(PHPMD.NPathComplexity) | ||
*/ | ||
public function getProductPriceHtml( | ||
\Magento\Catalog\Model\Product $product, | ||
$priceType = null, | ||
$renderZone = \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST, | ||
array $arguments = [] | ||
) { | ||
if (!isset($arguments['zone'])) { | ||
$arguments['zone'] = $renderZone; | ||
} | ||
$arguments['zone'] = isset($arguments['zone']) | ||
? $arguments['zone'] | ||
: $renderZone; | ||
$arguments['price_id'] = isset($arguments['price_id']) | ||
? $arguments['price_id'] | ||
: 'old-price-' . $product->getId() . '-' . $priceType; | ||
$arguments['include_container'] = isset($arguments['include_container']) | ||
? $arguments['include_container'] | ||
: true; | ||
$arguments['display_minimal_price'] = isset($arguments['display_minimal_price']) | ||
? $arguments['display_minimal_price'] | ||
: true; | ||
|
||
/** @var \Magento\Framework\Pricing\Render $priceRender */ | ||
$priceRender = $this->getLayout()->getBlock('product.price.render.default'); | ||
|
||
$price = ''; | ||
if ($priceRender) { | ||
$price = $priceRender->render( | ||
\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE, | ||
$product, | ||
$arguments | ||
); | ||
} | ||
return $price; | ||
} | ||
} |
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="Xigen_Special" setup_version="1.1.1"> | ||
<module name="Xigen_Special" setup_version="1.1.2"> | ||
</module> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?xml version="1.0" ?> | ||
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd"> | ||
<widget class="Xigen\Special\Block\Widget\Special" id="xigen_special_special"> | ||
<label>Special Products List</label> | ||
<description>List of products that are set as special Products</description> | ||
<parameters> | ||
<parameter name="show_pager" xsi:type="select" visible="true" source_model="Magento\Config\Model\Config\Source\Yesno"> | ||
<label translate="true">Display Page Control</label> | ||
</parameter> | ||
<parameter name="products_per_page" xsi:type="text" required="true" visible="true"> | ||
<label translate="true">Number of Products per Page</label> | ||
<depends> | ||
<parameter name="show_pager" value="1"/> | ||
</depends> | ||
<value>5</value> | ||
</parameter> | ||
<parameter name="products_count" xsi:type="text" required="true" visible="true"> | ||
<label translate="true">Number of Products to Display</label> | ||
<value>10</value> | ||
</parameter> | ||
<parameter name="template" xsi:type="select" required="true" visible="true"> | ||
<label translate="true">Template</label> | ||
<options> | ||
<option name="default" value="product/widget/special/content/special_grid.phtml" selected="true"> | ||
<label translate="true">Special Products Grid Template</label> | ||
</option> | ||
<option name="list" value="product/widget/special/content/special_list.phtml"> | ||
<label translate="true">Special Products List Template</label> | ||
</option> | ||
<option name="list_default" | ||
value="product/widget/special/column/special_default_list.phtml"> | ||
<label translate="true">Special Products Images and Names Template</label> | ||
</option> | ||
<option name="list_names" | ||
value="product/widget/special/column/special_names_list.phtml"> | ||
<label translate="true">Special Products Names Only Template</label> | ||
</option> | ||
<option name="list_images" | ||
value="product/widget/special/column/special_images_list.phtml"> | ||
<label translate="true">Special Products Images Only Template</label> | ||
</option> | ||
</options> | ||
</parameter> | ||
<parameter name="cache_lifetime" xsi:type="text" visible="true"> | ||
<label translate="true">Cache Lifetime (Seconds)</label> | ||
<description translate="true">86400 by default, if not set. To refresh instantly, clear the Blocks HTML Output cache.</description> | ||
</parameter> | ||
</parameters> | ||
<containers> | ||
<container name="sidebar.main"> | ||
<template name="default" value="list_default"/> | ||
<template name="names_only" value="list_names"/> | ||
<template name="images_only" value="list_images"/> | ||
</container> | ||
<container name="content"> | ||
<template name="grid" value="default"/> | ||
<template name="list" value="list"/> | ||
</container> | ||
<container name="sidebar.additional"> | ||
<template name="default" value="list_default"/> | ||
<template name="names_only" value="list_names"/> | ||
<template name="images_only" value="list_images"/> | ||
</container> | ||
</containers> | ||
</widget> | ||
</widgets> |
Empty file.
Empty file.
Empty file.
Oops, something went wrong.