Magento 小部件块中的分页
Pagination in Magento widget block
您好,有什么方法可以在小部件块中进行分页。例如,有一个 id 为 355 的类别。我想在页面中显示该类别的产品。所以我正在使用小部件(以下代码)
{{widget type="Magento\CatalogWidget\Block\Product\ProductsList" show_pager="0" products_count="160" template="Magento_CatalogWidget::product/widget/content/grid.phtml" conditions_encoded="^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,`aggregator`:`all`,`value`:`1`,`new_child`:``^],`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`category_ids`,`operator`:`==`,`value`:`355`^]^]"}}
。但是该产品在该页面中显示的类别中最多有 155 种产品。但是对于 155 产品,页面加载时间太长了。因此,如果有分页,那么加载产品就很容易了。
事实上是,寻呼机是为Magento\CatalogWidget\Block\Product\ProductsList实现的,你只需要使用show_pager="1"激活它 并定义每页显示多少产品 products_per_page="6"(如果您忽略此参数,则默认值为 5)
更新:
我猜你需要添加参数 page_var_name="np" 其中 'np' 是分页参数的名称(你可以在方便时命名), 就像下面这样,这应该可以解决分页问题:
你的代码应该是这样的:
{{widget type="Magento\CatalogWidget\Block\Product\ProductsList" show_pager="1" products_per_page="6" products_count="160" page_var_name="np" template="Magento_CatalogWidget::product/widget/content/grid.phtml" conditions_encoded="^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,`aggregator`:`all`,`value`:`1`,`new_child`:``^],`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`category_ids`,`operator`:`==`,`value`:`355`^]^]"}}
@see : vendor/magento/module-catalog-widget/Block/Product/ProductsList.php
class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implements BlockInterface, IdentityInterface
{
/**
* Default value for products count that will be shown
*/
const DEFAULT_PRODUCTS_COUNT = 10;
/**
* Name of request parameter for page number value
*
* @deprecated
*/
const PAGE_VAR_NAME = 'np';
/**
* Default value for products per page
*/
const DEFAULT_PRODUCTS_PER_PAGE = 5;
/**
* Default value whether show pager or not
*/
const DEFAULT_SHOW_PAGER = false;
...
/**
* 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();
}
您好,有什么方法可以在小部件块中进行分页。例如,有一个 id 为 355 的类别。我想在页面中显示该类别的产品。所以我正在使用小部件(以下代码)
{{widget type="Magento\CatalogWidget\Block\Product\ProductsList" show_pager="0" products_count="160" template="Magento_CatalogWidget::product/widget/content/grid.phtml" conditions_encoded="^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,`aggregator`:`all`,`value`:`1`,`new_child`:``^],`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`category_ids`,`operator`:`==`,`value`:`355`^]^]"}}
。但是该产品在该页面中显示的类别中最多有 155 种产品。但是对于 155 产品,页面加载时间太长了。因此,如果有分页,那么加载产品就很容易了。
事实上是,寻呼机是为Magento\CatalogWidget\Block\Product\ProductsList实现的,你只需要使用show_pager="1"激活它 并定义每页显示多少产品 products_per_page="6"(如果您忽略此参数,则默认值为 5)
更新: 我猜你需要添加参数 page_var_name="np" 其中 'np' 是分页参数的名称(你可以在方便时命名), 就像下面这样,这应该可以解决分页问题:
你的代码应该是这样的:
{{widget type="Magento\CatalogWidget\Block\Product\ProductsList" show_pager="1" products_per_page="6" products_count="160" page_var_name="np" template="Magento_CatalogWidget::product/widget/content/grid.phtml" conditions_encoded="^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,`aggregator`:`all`,`value`:`1`,`new_child`:``^],`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`category_ids`,`operator`:`==`,`value`:`355`^]^]"}}
@see : vendor/magento/module-catalog-widget/Block/Product/ProductsList.php
class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implements BlockInterface, IdentityInterface
{
/**
* Default value for products count that will be shown
*/
const DEFAULT_PRODUCTS_COUNT = 10;
/**
* Name of request parameter for page number value
*
* @deprecated
*/
const PAGE_VAR_NAME = 'np';
/**
* Default value for products per page
*/
const DEFAULT_PRODUCTS_PER_PAGE = 5;
/**
* Default value whether show pager or not
*/
const DEFAULT_SHOW_PAGER = false;
...
/**
* 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();
}