How to ignore not avilable products from best seller product collection? The 2019 Stack Overflow Developer Survey Results Are In Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar ManaraHow to get Best Selling Product Collection in Magento 2 using Object ManagerSet custom price of product when adding to cart code not workingMagento 2 : Problem while adding custom button order view page?Magento 2: Add a product to the cart programmaticallyMagento offline custom Payment method with drop down listMagento 2 Create dynamic array From different Model Collection to use in multi select in gridI have created one field using product form field for my price i want save my field value at product creation time from backend magento2Magento 2.3 Can't view module's front end page output?Best seller collection filter magento2Magento2: get latest Best seller collectionMagento 2.2.6: Programatically updating product custom options

Can withdrawing asylum be illegal?

How to read αἱμύλιος or when to aspirate

What aspect of planet Earth must be changed to prevent the industrial revolution?

Are there continuous functions who are the same in an interval but differ in at least one other point?

Python - Fishing Simulator

Can I visit the Trinity College (Cambridge) library and see some of their rare books

For what reasons would an animal species NOT cross a *horizontal* land bridge?

What was the last x86 CPU that did not have the x87 floating-point unit built in?

If I score a critical hit on an 18 or higher, what are my chances of getting a critical hit if I roll 3d20?

Why can't devices on different VLANs, but on the same subnet, communicate?

First use of “packing” as in carrying a gun

Huge performance difference of the command find with and without using %M option to show permissions

Can the DM override racial traits?

Do working physicists consider Newtonian mechanics to be "falsified"?

Is there a writing software that you can sort scenes like slides in PowerPoint?

How do I design a circuit to convert a 100 mV and 50 Hz sine wave to a square wave?

Example of compact Riemannian manifold with only one geodesic.

What information about me do stores get via my credit card?

Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?

Why did Peik Lin say, "I'm not an animal"?

University's motivation for having tenure-track positions

Separating matrix elements by lines

Why are PDP-7-style microprogrammed instructions out of vogue?

Drawing arrows from one table cell reference to another



How to ignore not avilable products from best seller product collection?



The 2019 Stack Overflow Developer Survey Results Are In
Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar ManaraHow to get Best Selling Product Collection in Magento 2 using Object ManagerSet custom price of product when adding to cart code not workingMagento 2 : Problem while adding custom button order view page?Magento 2: Add a product to the cart programmaticallyMagento offline custom Payment method with drop down listMagento 2 Create dynamic array From different Model Collection to use in multi select in gridI have created one field using product form field for my price i want save my field value at product creation time from backend magento2Magento 2.3 Can't view module's front end page output?Best seller collection filter magento2Magento2: get latest Best seller collectionMagento 2.2.6: Programatically updating product custom options



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1
















app/code/Test/Demo/Block/Widget/Bestsellerdproduct.php





namespace TestDemoBlockWidget;
class Bestsellerdproduct extends MagentoFrameworkViewElementTemplate implements MagentoWidgetBlockBlockInterface

protected $_template = 'widget/bestsellerdproduct.phtml';

/**
* Default value for products count that will be shown
*/
const DEFAULT_PRODUCTS_COUNT = 10;
const DEFAULT_IMAGE_WIDTH = 150;
const DEFAULT_IMAGE_HEIGHT = 150;
/**
* Products count
*
* @var int
*/
protected $_productsCount;
/**
* @var MagentoFrameworkAppHttpContext
*/
protected $httpContext;
protected $_resourceFactory;
/**
* Catalog product visibility
*
* @var MagentoCatalogModelProductVisibility
*/
protected $_catalogProductVisibility;

/**
* Product collection factory
*
* @var MagentoCatalogModelResourceModelProductCollectionFactory
*/
protected $_productCollectionFactory;

/**
* Image helper
*
* @var MagentoCatalogHelperImage
*/
protected $_imageHelper;
/**
* @var MagentoCheckoutHelperCart
*/
protected $_cartHelper;
/**
* @param Context $context
* @param MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory
* @param MagentoCatalogModelProductVisibility $catalogProductVisibility
* @param MagentoFrameworkAppHttpContext $httpContext
* @param array $data
*/
public function __construct(
MagentoCatalogBlockProductContext $context,
MagentoReportsModelResourceModelReportCollectionFactory $resourceFactory,
MagentoReportsModelGroupedCollectionFactory $collectionFactory,
MagentoReportsHelperData $reportsData,
array $data = []
)
$this->_resourceFactory = $resourceFactory;
$this->_collectionFactory = $collectionFactory;
$this->_reportsData = $reportsData;
$this->_imageHelper = $context->getImageHelper();
$this->_cartHelper = $context->getCartHelper();
parent::__construct($context, $data);

/**
* Image helper Object
*/
public function imageHelperObj()
return $this->_imageHelper;

/**
* get featured product collection
*/
public function getBestsellerProduct()
$limit = $this->getProductLimit();
$resourceCollection = $this->_resourceFactory->create('MagentoSalesModelResourceModelReportBestsellersCollection');
$resourceCollection->setPageSize($limit);
return $resourceCollection;


/**
* Get the configured limit of products
* @return int
*/
public function getProductLimit()
if($this->getData('productcount')=='')
return DEFAULT_PRODUCTS_COUNT;

return $this->getData('productcount');

/**
* Get the widht of product image
* @return int
*/
public function getProductimagewidth()
if($this->getData('imagewidth')=='')
return DEFAULT_IMAGE_WIDTH;

return $this->getData('imagewidth');

/**
* Get the height of product image
* @return int
*/
public function getProductimageheight()
if($this->getData('imageheight')=='')
return DEFAULT_IMAGE_HEIGHT;

return $this->getData('imageheight');

/**
* Get the add to cart url
* @return string
*/
public function getAddToCartUrl($product, $additional = [])

return $this->_cartHelper->getAddUrl($product, $additional);

/**
* Return HTML block with price
*
* @param MagentoCatalogModelProduct $product
* @param string $priceType
* @param string $renderZone
* @param array $arguments
* @return string
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function getProductPriceHtml(
MagentoCatalogModelProduct $product,
$priceType = null,
$renderZone = MagentoFrameworkPricingRender::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 MagentoFrameworkPricingRender $priceRender */
$priceRender = $this->getLayout()->getBlock('product.price.render.default');
$price = '';
if ($priceRender)
$price = $priceRender->render(
MagentoCatalogPricingPriceFinalPrice::PRICE_CODE,
$product,
$arguments
);

return $price;





app/code/Test/Demo/view/frontend/templates/widget/bestsellerdproduct.phtml




 <?php
if ($exist = ($this->getBestsellerProduct() && $this->getBestsellerProduct()->getPageSize()))
$fet_prodcollection = $this->getBestsellerProduct();
$productcount = $this->getProductLimit();
$imagewidth = $this->getProductimagewidth();
$imageheight = $this->getProductimageheight();
$mode = 'grid';
$title = __('Best Sellers');
$type = 'widget-bestseller-grid';
$image = 'bestseller_products_content_widget_grid';
$objectManager = MagentoFrameworkAppObjectManager::getInstance();

?>
<?php if ($exist):?>
<div class="block widget block-bestseller-products <?php /* @escapeNotVerified */ echo $mode; ?>">
<div class="block-title">
<strong role="heading" aria-level="2"><?php /* @escapeNotVerified */ echo $title; ?></strong>
</div>
<div class="block-content">
<?php /* @escapeNotVerified */ echo '<!-- ' . $image . '-->' ?>
<div class="products-<?php /* @escapeNotVerified */ echo $mode; ?> <?php /* @escapeNotVerified */ echo $mode; ?>">
<ol class="product-items owl-carousel <?php /* @escapeNotVerified */ echo $type; ?>" id="bestSellers">
<?php $iterator = 1; ?>
<?php foreach ($fet_prodcollection as $item):
$_product = $objectManager->create('MagentoCatalogModelProduct')->load($item->getProductId());
//for avoiding not Not Visible Individually
if($_product->getVisibility() !='1' && $_product->getIsSalable())
?>
<?php /* @escapeNotVerified */ echo($iterator++ == 1) ? '<li class="product-item">' : '</li><li class="product-item">' ?>
<div class="product-item-info">
<?php
$imagewidth='170';
$imageheight ='170';
$image_url = $block->imageHelperObj()->init($_product, 'product_base_image')
->setImageFile($_product->getFile())
->resize($imagewidth,$imageheight)
->keepAspectRatio(true)
->getUrl();
?>
<a href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?>" class="product-item-photo">
<img src="<?php echo $image_url;?>" alt="<?php echo $this->escapeHtml($_product->getName()) ?>" />
</a>
<div class="product-item-details">
<strong class="product-item-name">
<a title="<?php echo $block->escapeHtml($_product->getName()) ?>"
href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?>"
class="product-item-link">
<?php echo $block->escapeHtml($_product->getName()) ?>
</a>
</strong>
<?php
echo $this->getProductPriceHtml($_product, $type);
?>
<div class="product-item-actions">
<div class="actions-primary">
<?php if ($_product->isSaleable()): ?>
<?php if ($_product->getTypeInstance()->hasRequiredOptions($_product)): ?>
<button class="action tocart primary"
data-mage-init='"redirectUrl":"url":"<?php /* @escapeNotVerified */ echo $block->getAddToCartUrl($_product) ?>"'
type="button" title="<?php /* @escapeNotVerified */ echo __('Add to Cart') ?>">
<span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
</button>
<?php else: ?>
<?php
$postDataHelper = $this->helper('MagentoFrameworkDataHelperPostHelper');
$postData = $postDataHelper->getPostData($block->getAddToCartUrl($_product), ['product' => $_product->getId()]);
?>
<button class="action tocart primary" data-post='<?php /* @escapeNotVerified */ echo $postData; ?>' type="button" title="<?php /* @escapeNotVerified */ echo __('Add to Cart') ?>">
<span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
</button>
<?php endif; ?>
<?php else: ?>
<?php if ($_product->getIsSalable()): ?>
<div class="stock available"><span><?php /* @escapeNotVerified */ echo __('In stock') ?></span></div>
<?php else: ?>
<div class="stock unavailable"><span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span></div>
<?php endif; ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php ?>
<?php echo($iterator == count($fet_prodcollection)+1) ? '</li>' : '' ?>
<?php endforeach ?>
</ol>
</div>

<?php endif;?>
</div>
</div>


enter image description here










share|improve this question



















  • 1





    You can add status, visibility and stock status filter into collection.

    – Kumar M
    Aug 22 '18 at 6:04


















1
















app/code/Test/Demo/Block/Widget/Bestsellerdproduct.php





namespace TestDemoBlockWidget;
class Bestsellerdproduct extends MagentoFrameworkViewElementTemplate implements MagentoWidgetBlockBlockInterface

protected $_template = 'widget/bestsellerdproduct.phtml';

/**
* Default value for products count that will be shown
*/
const DEFAULT_PRODUCTS_COUNT = 10;
const DEFAULT_IMAGE_WIDTH = 150;
const DEFAULT_IMAGE_HEIGHT = 150;
/**
* Products count
*
* @var int
*/
protected $_productsCount;
/**
* @var MagentoFrameworkAppHttpContext
*/
protected $httpContext;
protected $_resourceFactory;
/**
* Catalog product visibility
*
* @var MagentoCatalogModelProductVisibility
*/
protected $_catalogProductVisibility;

/**
* Product collection factory
*
* @var MagentoCatalogModelResourceModelProductCollectionFactory
*/
protected $_productCollectionFactory;

/**
* Image helper
*
* @var MagentoCatalogHelperImage
*/
protected $_imageHelper;
/**
* @var MagentoCheckoutHelperCart
*/
protected $_cartHelper;
/**
* @param Context $context
* @param MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory
* @param MagentoCatalogModelProductVisibility $catalogProductVisibility
* @param MagentoFrameworkAppHttpContext $httpContext
* @param array $data
*/
public function __construct(
MagentoCatalogBlockProductContext $context,
MagentoReportsModelResourceModelReportCollectionFactory $resourceFactory,
MagentoReportsModelGroupedCollectionFactory $collectionFactory,
MagentoReportsHelperData $reportsData,
array $data = []
)
$this->_resourceFactory = $resourceFactory;
$this->_collectionFactory = $collectionFactory;
$this->_reportsData = $reportsData;
$this->_imageHelper = $context->getImageHelper();
$this->_cartHelper = $context->getCartHelper();
parent::__construct($context, $data);

/**
* Image helper Object
*/
public function imageHelperObj()
return $this->_imageHelper;

/**
* get featured product collection
*/
public function getBestsellerProduct()
$limit = $this->getProductLimit();
$resourceCollection = $this->_resourceFactory->create('MagentoSalesModelResourceModelReportBestsellersCollection');
$resourceCollection->setPageSize($limit);
return $resourceCollection;


/**
* Get the configured limit of products
* @return int
*/
public function getProductLimit()
if($this->getData('productcount')=='')
return DEFAULT_PRODUCTS_COUNT;

return $this->getData('productcount');

/**
* Get the widht of product image
* @return int
*/
public function getProductimagewidth()
if($this->getData('imagewidth')=='')
return DEFAULT_IMAGE_WIDTH;

return $this->getData('imagewidth');

/**
* Get the height of product image
* @return int
*/
public function getProductimageheight()
if($this->getData('imageheight')=='')
return DEFAULT_IMAGE_HEIGHT;

return $this->getData('imageheight');

/**
* Get the add to cart url
* @return string
*/
public function getAddToCartUrl($product, $additional = [])

return $this->_cartHelper->getAddUrl($product, $additional);

/**
* Return HTML block with price
*
* @param MagentoCatalogModelProduct $product
* @param string $priceType
* @param string $renderZone
* @param array $arguments
* @return string
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function getProductPriceHtml(
MagentoCatalogModelProduct $product,
$priceType = null,
$renderZone = MagentoFrameworkPricingRender::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 MagentoFrameworkPricingRender $priceRender */
$priceRender = $this->getLayout()->getBlock('product.price.render.default');
$price = '';
if ($priceRender)
$price = $priceRender->render(
MagentoCatalogPricingPriceFinalPrice::PRICE_CODE,
$product,
$arguments
);

return $price;





app/code/Test/Demo/view/frontend/templates/widget/bestsellerdproduct.phtml




 <?php
if ($exist = ($this->getBestsellerProduct() && $this->getBestsellerProduct()->getPageSize()))
$fet_prodcollection = $this->getBestsellerProduct();
$productcount = $this->getProductLimit();
$imagewidth = $this->getProductimagewidth();
$imageheight = $this->getProductimageheight();
$mode = 'grid';
$title = __('Best Sellers');
$type = 'widget-bestseller-grid';
$image = 'bestseller_products_content_widget_grid';
$objectManager = MagentoFrameworkAppObjectManager::getInstance();

?>
<?php if ($exist):?>
<div class="block widget block-bestseller-products <?php /* @escapeNotVerified */ echo $mode; ?>">
<div class="block-title">
<strong role="heading" aria-level="2"><?php /* @escapeNotVerified */ echo $title; ?></strong>
</div>
<div class="block-content">
<?php /* @escapeNotVerified */ echo '<!-- ' . $image . '-->' ?>
<div class="products-<?php /* @escapeNotVerified */ echo $mode; ?> <?php /* @escapeNotVerified */ echo $mode; ?>">
<ol class="product-items owl-carousel <?php /* @escapeNotVerified */ echo $type; ?>" id="bestSellers">
<?php $iterator = 1; ?>
<?php foreach ($fet_prodcollection as $item):
$_product = $objectManager->create('MagentoCatalogModelProduct')->load($item->getProductId());
//for avoiding not Not Visible Individually
if($_product->getVisibility() !='1' && $_product->getIsSalable())
?>
<?php /* @escapeNotVerified */ echo($iterator++ == 1) ? '<li class="product-item">' : '</li><li class="product-item">' ?>
<div class="product-item-info">
<?php
$imagewidth='170';
$imageheight ='170';
$image_url = $block->imageHelperObj()->init($_product, 'product_base_image')
->setImageFile($_product->getFile())
->resize($imagewidth,$imageheight)
->keepAspectRatio(true)
->getUrl();
?>
<a href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?>" class="product-item-photo">
<img src="<?php echo $image_url;?>" alt="<?php echo $this->escapeHtml($_product->getName()) ?>" />
</a>
<div class="product-item-details">
<strong class="product-item-name">
<a title="<?php echo $block->escapeHtml($_product->getName()) ?>"
href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?>"
class="product-item-link">
<?php echo $block->escapeHtml($_product->getName()) ?>
</a>
</strong>
<?php
echo $this->getProductPriceHtml($_product, $type);
?>
<div class="product-item-actions">
<div class="actions-primary">
<?php if ($_product->isSaleable()): ?>
<?php if ($_product->getTypeInstance()->hasRequiredOptions($_product)): ?>
<button class="action tocart primary"
data-mage-init='"redirectUrl":"url":"<?php /* @escapeNotVerified */ echo $block->getAddToCartUrl($_product) ?>"'
type="button" title="<?php /* @escapeNotVerified */ echo __('Add to Cart') ?>">
<span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
</button>
<?php else: ?>
<?php
$postDataHelper = $this->helper('MagentoFrameworkDataHelperPostHelper');
$postData = $postDataHelper->getPostData($block->getAddToCartUrl($_product), ['product' => $_product->getId()]);
?>
<button class="action tocart primary" data-post='<?php /* @escapeNotVerified */ echo $postData; ?>' type="button" title="<?php /* @escapeNotVerified */ echo __('Add to Cart') ?>">
<span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
</button>
<?php endif; ?>
<?php else: ?>
<?php if ($_product->getIsSalable()): ?>
<div class="stock available"><span><?php /* @escapeNotVerified */ echo __('In stock') ?></span></div>
<?php else: ?>
<div class="stock unavailable"><span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span></div>
<?php endif; ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php ?>
<?php echo($iterator == count($fet_prodcollection)+1) ? '</li>' : '' ?>
<?php endforeach ?>
</ol>
</div>

<?php endif;?>
</div>
</div>


enter image description here










share|improve this question



















  • 1





    You can add status, visibility and stock status filter into collection.

    – Kumar M
    Aug 22 '18 at 6:04














1












1








1









app/code/Test/Demo/Block/Widget/Bestsellerdproduct.php





namespace TestDemoBlockWidget;
class Bestsellerdproduct extends MagentoFrameworkViewElementTemplate implements MagentoWidgetBlockBlockInterface

protected $_template = 'widget/bestsellerdproduct.phtml';

/**
* Default value for products count that will be shown
*/
const DEFAULT_PRODUCTS_COUNT = 10;
const DEFAULT_IMAGE_WIDTH = 150;
const DEFAULT_IMAGE_HEIGHT = 150;
/**
* Products count
*
* @var int
*/
protected $_productsCount;
/**
* @var MagentoFrameworkAppHttpContext
*/
protected $httpContext;
protected $_resourceFactory;
/**
* Catalog product visibility
*
* @var MagentoCatalogModelProductVisibility
*/
protected $_catalogProductVisibility;

/**
* Product collection factory
*
* @var MagentoCatalogModelResourceModelProductCollectionFactory
*/
protected $_productCollectionFactory;

/**
* Image helper
*
* @var MagentoCatalogHelperImage
*/
protected $_imageHelper;
/**
* @var MagentoCheckoutHelperCart
*/
protected $_cartHelper;
/**
* @param Context $context
* @param MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory
* @param MagentoCatalogModelProductVisibility $catalogProductVisibility
* @param MagentoFrameworkAppHttpContext $httpContext
* @param array $data
*/
public function __construct(
MagentoCatalogBlockProductContext $context,
MagentoReportsModelResourceModelReportCollectionFactory $resourceFactory,
MagentoReportsModelGroupedCollectionFactory $collectionFactory,
MagentoReportsHelperData $reportsData,
array $data = []
)
$this->_resourceFactory = $resourceFactory;
$this->_collectionFactory = $collectionFactory;
$this->_reportsData = $reportsData;
$this->_imageHelper = $context->getImageHelper();
$this->_cartHelper = $context->getCartHelper();
parent::__construct($context, $data);

/**
* Image helper Object
*/
public function imageHelperObj()
return $this->_imageHelper;

/**
* get featured product collection
*/
public function getBestsellerProduct()
$limit = $this->getProductLimit();
$resourceCollection = $this->_resourceFactory->create('MagentoSalesModelResourceModelReportBestsellersCollection');
$resourceCollection->setPageSize($limit);
return $resourceCollection;


/**
* Get the configured limit of products
* @return int
*/
public function getProductLimit()
if($this->getData('productcount')=='')
return DEFAULT_PRODUCTS_COUNT;

return $this->getData('productcount');

/**
* Get the widht of product image
* @return int
*/
public function getProductimagewidth()
if($this->getData('imagewidth')=='')
return DEFAULT_IMAGE_WIDTH;

return $this->getData('imagewidth');

/**
* Get the height of product image
* @return int
*/
public function getProductimageheight()
if($this->getData('imageheight')=='')
return DEFAULT_IMAGE_HEIGHT;

return $this->getData('imageheight');

/**
* Get the add to cart url
* @return string
*/
public function getAddToCartUrl($product, $additional = [])

return $this->_cartHelper->getAddUrl($product, $additional);

/**
* Return HTML block with price
*
* @param MagentoCatalogModelProduct $product
* @param string $priceType
* @param string $renderZone
* @param array $arguments
* @return string
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function getProductPriceHtml(
MagentoCatalogModelProduct $product,
$priceType = null,
$renderZone = MagentoFrameworkPricingRender::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 MagentoFrameworkPricingRender $priceRender */
$priceRender = $this->getLayout()->getBlock('product.price.render.default');
$price = '';
if ($priceRender)
$price = $priceRender->render(
MagentoCatalogPricingPriceFinalPrice::PRICE_CODE,
$product,
$arguments
);

return $price;





app/code/Test/Demo/view/frontend/templates/widget/bestsellerdproduct.phtml




 <?php
if ($exist = ($this->getBestsellerProduct() && $this->getBestsellerProduct()->getPageSize()))
$fet_prodcollection = $this->getBestsellerProduct();
$productcount = $this->getProductLimit();
$imagewidth = $this->getProductimagewidth();
$imageheight = $this->getProductimageheight();
$mode = 'grid';
$title = __('Best Sellers');
$type = 'widget-bestseller-grid';
$image = 'bestseller_products_content_widget_grid';
$objectManager = MagentoFrameworkAppObjectManager::getInstance();

?>
<?php if ($exist):?>
<div class="block widget block-bestseller-products <?php /* @escapeNotVerified */ echo $mode; ?>">
<div class="block-title">
<strong role="heading" aria-level="2"><?php /* @escapeNotVerified */ echo $title; ?></strong>
</div>
<div class="block-content">
<?php /* @escapeNotVerified */ echo '<!-- ' . $image . '-->' ?>
<div class="products-<?php /* @escapeNotVerified */ echo $mode; ?> <?php /* @escapeNotVerified */ echo $mode; ?>">
<ol class="product-items owl-carousel <?php /* @escapeNotVerified */ echo $type; ?>" id="bestSellers">
<?php $iterator = 1; ?>
<?php foreach ($fet_prodcollection as $item):
$_product = $objectManager->create('MagentoCatalogModelProduct')->load($item->getProductId());
//for avoiding not Not Visible Individually
if($_product->getVisibility() !='1' && $_product->getIsSalable())
?>
<?php /* @escapeNotVerified */ echo($iterator++ == 1) ? '<li class="product-item">' : '</li><li class="product-item">' ?>
<div class="product-item-info">
<?php
$imagewidth='170';
$imageheight ='170';
$image_url = $block->imageHelperObj()->init($_product, 'product_base_image')
->setImageFile($_product->getFile())
->resize($imagewidth,$imageheight)
->keepAspectRatio(true)
->getUrl();
?>
<a href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?>" class="product-item-photo">
<img src="<?php echo $image_url;?>" alt="<?php echo $this->escapeHtml($_product->getName()) ?>" />
</a>
<div class="product-item-details">
<strong class="product-item-name">
<a title="<?php echo $block->escapeHtml($_product->getName()) ?>"
href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?>"
class="product-item-link">
<?php echo $block->escapeHtml($_product->getName()) ?>
</a>
</strong>
<?php
echo $this->getProductPriceHtml($_product, $type);
?>
<div class="product-item-actions">
<div class="actions-primary">
<?php if ($_product->isSaleable()): ?>
<?php if ($_product->getTypeInstance()->hasRequiredOptions($_product)): ?>
<button class="action tocart primary"
data-mage-init='"redirectUrl":"url":"<?php /* @escapeNotVerified */ echo $block->getAddToCartUrl($_product) ?>"'
type="button" title="<?php /* @escapeNotVerified */ echo __('Add to Cart') ?>">
<span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
</button>
<?php else: ?>
<?php
$postDataHelper = $this->helper('MagentoFrameworkDataHelperPostHelper');
$postData = $postDataHelper->getPostData($block->getAddToCartUrl($_product), ['product' => $_product->getId()]);
?>
<button class="action tocart primary" data-post='<?php /* @escapeNotVerified */ echo $postData; ?>' type="button" title="<?php /* @escapeNotVerified */ echo __('Add to Cart') ?>">
<span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
</button>
<?php endif; ?>
<?php else: ?>
<?php if ($_product->getIsSalable()): ?>
<div class="stock available"><span><?php /* @escapeNotVerified */ echo __('In stock') ?></span></div>
<?php else: ?>
<div class="stock unavailable"><span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span></div>
<?php endif; ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php ?>
<?php echo($iterator == count($fet_prodcollection)+1) ? '</li>' : '' ?>
<?php endforeach ?>
</ol>
</div>

<?php endif;?>
</div>
</div>


enter image description here










share|improve this question

















app/code/Test/Demo/Block/Widget/Bestsellerdproduct.php





namespace TestDemoBlockWidget;
class Bestsellerdproduct extends MagentoFrameworkViewElementTemplate implements MagentoWidgetBlockBlockInterface

protected $_template = 'widget/bestsellerdproduct.phtml';

/**
* Default value for products count that will be shown
*/
const DEFAULT_PRODUCTS_COUNT = 10;
const DEFAULT_IMAGE_WIDTH = 150;
const DEFAULT_IMAGE_HEIGHT = 150;
/**
* Products count
*
* @var int
*/
protected $_productsCount;
/**
* @var MagentoFrameworkAppHttpContext
*/
protected $httpContext;
protected $_resourceFactory;
/**
* Catalog product visibility
*
* @var MagentoCatalogModelProductVisibility
*/
protected $_catalogProductVisibility;

/**
* Product collection factory
*
* @var MagentoCatalogModelResourceModelProductCollectionFactory
*/
protected $_productCollectionFactory;

/**
* Image helper
*
* @var MagentoCatalogHelperImage
*/
protected $_imageHelper;
/**
* @var MagentoCheckoutHelperCart
*/
protected $_cartHelper;
/**
* @param Context $context
* @param MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory
* @param MagentoCatalogModelProductVisibility $catalogProductVisibility
* @param MagentoFrameworkAppHttpContext $httpContext
* @param array $data
*/
public function __construct(
MagentoCatalogBlockProductContext $context,
MagentoReportsModelResourceModelReportCollectionFactory $resourceFactory,
MagentoReportsModelGroupedCollectionFactory $collectionFactory,
MagentoReportsHelperData $reportsData,
array $data = []
)
$this->_resourceFactory = $resourceFactory;
$this->_collectionFactory = $collectionFactory;
$this->_reportsData = $reportsData;
$this->_imageHelper = $context->getImageHelper();
$this->_cartHelper = $context->getCartHelper();
parent::__construct($context, $data);

/**
* Image helper Object
*/
public function imageHelperObj()
return $this->_imageHelper;

/**
* get featured product collection
*/
public function getBestsellerProduct()
$limit = $this->getProductLimit();
$resourceCollection = $this->_resourceFactory->create('MagentoSalesModelResourceModelReportBestsellersCollection');
$resourceCollection->setPageSize($limit);
return $resourceCollection;


/**
* Get the configured limit of products
* @return int
*/
public function getProductLimit()
if($this->getData('productcount')=='')
return DEFAULT_PRODUCTS_COUNT;

return $this->getData('productcount');

/**
* Get the widht of product image
* @return int
*/
public function getProductimagewidth()
if($this->getData('imagewidth')=='')
return DEFAULT_IMAGE_WIDTH;

return $this->getData('imagewidth');

/**
* Get the height of product image
* @return int
*/
public function getProductimageheight()
if($this->getData('imageheight')=='')
return DEFAULT_IMAGE_HEIGHT;

return $this->getData('imageheight');

/**
* Get the add to cart url
* @return string
*/
public function getAddToCartUrl($product, $additional = [])

return $this->_cartHelper->getAddUrl($product, $additional);

/**
* Return HTML block with price
*
* @param MagentoCatalogModelProduct $product
* @param string $priceType
* @param string $renderZone
* @param array $arguments
* @return string
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function getProductPriceHtml(
MagentoCatalogModelProduct $product,
$priceType = null,
$renderZone = MagentoFrameworkPricingRender::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 MagentoFrameworkPricingRender $priceRender */
$priceRender = $this->getLayout()->getBlock('product.price.render.default');
$price = '';
if ($priceRender)
$price = $priceRender->render(
MagentoCatalogPricingPriceFinalPrice::PRICE_CODE,
$product,
$arguments
);

return $price;





app/code/Test/Demo/view/frontend/templates/widget/bestsellerdproduct.phtml




 <?php
if ($exist = ($this->getBestsellerProduct() && $this->getBestsellerProduct()->getPageSize()))
$fet_prodcollection = $this->getBestsellerProduct();
$productcount = $this->getProductLimit();
$imagewidth = $this->getProductimagewidth();
$imageheight = $this->getProductimageheight();
$mode = 'grid';
$title = __('Best Sellers');
$type = 'widget-bestseller-grid';
$image = 'bestseller_products_content_widget_grid';
$objectManager = MagentoFrameworkAppObjectManager::getInstance();

?>
<?php if ($exist):?>
<div class="block widget block-bestseller-products <?php /* @escapeNotVerified */ echo $mode; ?>">
<div class="block-title">
<strong role="heading" aria-level="2"><?php /* @escapeNotVerified */ echo $title; ?></strong>
</div>
<div class="block-content">
<?php /* @escapeNotVerified */ echo '<!-- ' . $image . '-->' ?>
<div class="products-<?php /* @escapeNotVerified */ echo $mode; ?> <?php /* @escapeNotVerified */ echo $mode; ?>">
<ol class="product-items owl-carousel <?php /* @escapeNotVerified */ echo $type; ?>" id="bestSellers">
<?php $iterator = 1; ?>
<?php foreach ($fet_prodcollection as $item):
$_product = $objectManager->create('MagentoCatalogModelProduct')->load($item->getProductId());
//for avoiding not Not Visible Individually
if($_product->getVisibility() !='1' && $_product->getIsSalable())
?>
<?php /* @escapeNotVerified */ echo($iterator++ == 1) ? '<li class="product-item">' : '</li><li class="product-item">' ?>
<div class="product-item-info">
<?php
$imagewidth='170';
$imageheight ='170';
$image_url = $block->imageHelperObj()->init($_product, 'product_base_image')
->setImageFile($_product->getFile())
->resize($imagewidth,$imageheight)
->keepAspectRatio(true)
->getUrl();
?>
<a href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?>" class="product-item-photo">
<img src="<?php echo $image_url;?>" alt="<?php echo $this->escapeHtml($_product->getName()) ?>" />
</a>
<div class="product-item-details">
<strong class="product-item-name">
<a title="<?php echo $block->escapeHtml($_product->getName()) ?>"
href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?>"
class="product-item-link">
<?php echo $block->escapeHtml($_product->getName()) ?>
</a>
</strong>
<?php
echo $this->getProductPriceHtml($_product, $type);
?>
<div class="product-item-actions">
<div class="actions-primary">
<?php if ($_product->isSaleable()): ?>
<?php if ($_product->getTypeInstance()->hasRequiredOptions($_product)): ?>
<button class="action tocart primary"
data-mage-init='"redirectUrl":"url":"<?php /* @escapeNotVerified */ echo $block->getAddToCartUrl($_product) ?>"'
type="button" title="<?php /* @escapeNotVerified */ echo __('Add to Cart') ?>">
<span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
</button>
<?php else: ?>
<?php
$postDataHelper = $this->helper('MagentoFrameworkDataHelperPostHelper');
$postData = $postDataHelper->getPostData($block->getAddToCartUrl($_product), ['product' => $_product->getId()]);
?>
<button class="action tocart primary" data-post='<?php /* @escapeNotVerified */ echo $postData; ?>' type="button" title="<?php /* @escapeNotVerified */ echo __('Add to Cart') ?>">
<span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
</button>
<?php endif; ?>
<?php else: ?>
<?php if ($_product->getIsSalable()): ?>
<div class="stock available"><span><?php /* @escapeNotVerified */ echo __('In stock') ?></span></div>
<?php else: ?>
<div class="stock unavailable"><span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span></div>
<?php endif; ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php ?>
<?php echo($iterator == count($fet_prodcollection)+1) ? '</li>' : '' ?>
<?php endforeach ?>
</ol>
</div>

<?php endif;?>
</div>
</div>


enter image description here







magento2 product collection best-seller






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 9 at 9:40







Nagaraju Kasa

















asked Aug 22 '18 at 5:45









Nagaraju KasaNagaraju Kasa

2,79321642




2,79321642







  • 1





    You can add status, visibility and stock status filter into collection.

    – Kumar M
    Aug 22 '18 at 6:04













  • 1





    You can add status, visibility and stock status filter into collection.

    – Kumar M
    Aug 22 '18 at 6:04








1




1





You can add status, visibility and stock status filter into collection.

– Kumar M
Aug 22 '18 at 6:04






You can add status, visibility and stock status filter into collection.

– Kumar M
Aug 22 '18 at 6:04











1 Answer
1






active

oldest

votes


















1














Its good to join the stock filter in your collection for better performance.
However seeing your code you already have any answer for it.



$_product->getIsSalable()


Check this at top before creating your product div.






share|improve this answer























  • and more point @priyank in the collection we are getting not visible individual production how to avoid ?

    – Nagaraju Kasa
    Aug 22 '18 at 6:20











  • Hi @priyank one small help from the collection we are getting empty records (products not available) how to avoid please advise?

    – Nagaraju Kasa
    Apr 9 at 6:25












  • Hi @priyank i would like to exclude empty records and want to display out of stock from the collection could you please advise.

    – Nagaraju Kasa
    Apr 9 at 8:40











  • @NagarajuKasa what do you mean by empty records?

    – Priyank
    Apr 9 at 8:52











  • empty records means deleted products earlier. shall i update my block and template code here so that u will get an idea what i am doing currently.

    – Nagaraju Kasa
    Apr 9 at 9:30











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f239155%2fhow-to-ignore-not-avilable-products-from-best-seller-product-collection%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Its good to join the stock filter in your collection for better performance.
However seeing your code you already have any answer for it.



$_product->getIsSalable()


Check this at top before creating your product div.






share|improve this answer























  • and more point @priyank in the collection we are getting not visible individual production how to avoid ?

    – Nagaraju Kasa
    Aug 22 '18 at 6:20











  • Hi @priyank one small help from the collection we are getting empty records (products not available) how to avoid please advise?

    – Nagaraju Kasa
    Apr 9 at 6:25












  • Hi @priyank i would like to exclude empty records and want to display out of stock from the collection could you please advise.

    – Nagaraju Kasa
    Apr 9 at 8:40











  • @NagarajuKasa what do you mean by empty records?

    – Priyank
    Apr 9 at 8:52











  • empty records means deleted products earlier. shall i update my block and template code here so that u will get an idea what i am doing currently.

    – Nagaraju Kasa
    Apr 9 at 9:30















1














Its good to join the stock filter in your collection for better performance.
However seeing your code you already have any answer for it.



$_product->getIsSalable()


Check this at top before creating your product div.






share|improve this answer























  • and more point @priyank in the collection we are getting not visible individual production how to avoid ?

    – Nagaraju Kasa
    Aug 22 '18 at 6:20











  • Hi @priyank one small help from the collection we are getting empty records (products not available) how to avoid please advise?

    – Nagaraju Kasa
    Apr 9 at 6:25












  • Hi @priyank i would like to exclude empty records and want to display out of stock from the collection could you please advise.

    – Nagaraju Kasa
    Apr 9 at 8:40











  • @NagarajuKasa what do you mean by empty records?

    – Priyank
    Apr 9 at 8:52











  • empty records means deleted products earlier. shall i update my block and template code here so that u will get an idea what i am doing currently.

    – Nagaraju Kasa
    Apr 9 at 9:30













1












1








1







Its good to join the stock filter in your collection for better performance.
However seeing your code you already have any answer for it.



$_product->getIsSalable()


Check this at top before creating your product div.






share|improve this answer













Its good to join the stock filter in your collection for better performance.
However seeing your code you already have any answer for it.



$_product->getIsSalable()


Check this at top before creating your product div.







share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 22 '18 at 6:08









PriyankPriyank

5,37542253




5,37542253












  • and more point @priyank in the collection we are getting not visible individual production how to avoid ?

    – Nagaraju Kasa
    Aug 22 '18 at 6:20











  • Hi @priyank one small help from the collection we are getting empty records (products not available) how to avoid please advise?

    – Nagaraju Kasa
    Apr 9 at 6:25












  • Hi @priyank i would like to exclude empty records and want to display out of stock from the collection could you please advise.

    – Nagaraju Kasa
    Apr 9 at 8:40











  • @NagarajuKasa what do you mean by empty records?

    – Priyank
    Apr 9 at 8:52











  • empty records means deleted products earlier. shall i update my block and template code here so that u will get an idea what i am doing currently.

    – Nagaraju Kasa
    Apr 9 at 9:30

















  • and more point @priyank in the collection we are getting not visible individual production how to avoid ?

    – Nagaraju Kasa
    Aug 22 '18 at 6:20











  • Hi @priyank one small help from the collection we are getting empty records (products not available) how to avoid please advise?

    – Nagaraju Kasa
    Apr 9 at 6:25












  • Hi @priyank i would like to exclude empty records and want to display out of stock from the collection could you please advise.

    – Nagaraju Kasa
    Apr 9 at 8:40











  • @NagarajuKasa what do you mean by empty records?

    – Priyank
    Apr 9 at 8:52











  • empty records means deleted products earlier. shall i update my block and template code here so that u will get an idea what i am doing currently.

    – Nagaraju Kasa
    Apr 9 at 9:30
















and more point @priyank in the collection we are getting not visible individual production how to avoid ?

– Nagaraju Kasa
Aug 22 '18 at 6:20





and more point @priyank in the collection we are getting not visible individual production how to avoid ?

– Nagaraju Kasa
Aug 22 '18 at 6:20













Hi @priyank one small help from the collection we are getting empty records (products not available) how to avoid please advise?

– Nagaraju Kasa
Apr 9 at 6:25






Hi @priyank one small help from the collection we are getting empty records (products not available) how to avoid please advise?

– Nagaraju Kasa
Apr 9 at 6:25














Hi @priyank i would like to exclude empty records and want to display out of stock from the collection could you please advise.

– Nagaraju Kasa
Apr 9 at 8:40





Hi @priyank i would like to exclude empty records and want to display out of stock from the collection could you please advise.

– Nagaraju Kasa
Apr 9 at 8:40













@NagarajuKasa what do you mean by empty records?

– Priyank
Apr 9 at 8:52





@NagarajuKasa what do you mean by empty records?

– Priyank
Apr 9 at 8:52













empty records means deleted products earlier. shall i update my block and template code here so that u will get an idea what i am doing currently.

– Nagaraju Kasa
Apr 9 at 9:30





empty records means deleted products earlier. shall i update my block and template code here so that u will get an idea what i am doing currently.

– Nagaraju Kasa
Apr 9 at 9:30

















draft saved

draft discarded
















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f239155%2fhow-to-ignore-not-avilable-products-from-best-seller-product-collection%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Sum ergo cogito? 1 nng

419 nièngy_Soadمي 19bal1.5o_g

Queiggey Chernihivv 9NnOo i Zw X QqKk LpB