1

I have created the custom module to get product sales count in product list page as well as product view page.

How can i get particular product sale count in my source file?

I have updated my code below please check

Helper class:

<?php

namespace [Vendorname]\[ModuleName]\Helper;   
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterfac
     */
    protected $_scopeConfig;
    protected $_reportCollectionFactory;

    CONST ENABLE = '[Vendorname]\[ModuleName]/general/enable';
    CONST MINIMUM_COUNT = '[Vendorname]\[ModuleName]/general/va_minimum_count';
    CONST TEXT_ALIGN = '[Vendorname]\[ModuleName]/general/text_align';
    CONST PLP_BACKGROUNDCOLOR = '[Vendorname]\[ModuleName]/product_list_page/plp_backgroundcolor';
    CONST PLP_TEXTCOLOR = '[Vendorname]\[ModuleName]/product_list_page/plp_textcolor';
    CONST BORDER_RADIOUS = '[Vendorname]\[ModuleName]/product_view_page/va_proview_cart_radius';
    CONST PADDING_TOP_BOTTOM = '[Vendorname]\[ModuleName]/product_view_page/va_proview_cart_padding_tb';
    CONST PADDING_LEFT_RIGHT = '[Vendorname]\[ModuleName]/product_view_page/va_proview_cart_padding_lr';
    CONST FONT_SIZE = '[Vendorname]\[ModuleName]/product_view_page/va_proview_cart_font_size';
    CONST SALES_COUNT_TEXT = '[Vendorname]\[ModuleName]/product_view_page/va_proview_text';
    CONST PRODUCT_VIEW_BACKGROUND = '[Vendorname]\[ModuleName]/product_view_page/va_proview_background_color';
    CONST PRODUCT_VIEW_TEXT_COLOR = '[Vendorname]\[ModuleName]/product_view_page/va_proview_text_color';

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Reports\Model\ResourceModel\Product\Sold\CollectionFactory $reportCollectionFactory
    )
    {
        $this->_reportCollectionFactory = $reportCollectionFactory;
        parent::__construct($context);

        $this->_scopeConfig = $scopeConfig;
    }

    public function getEnable()
    {
        return $this->_scopeConfig->getValue(self::ENABLE);
    }

    public function getMinimumCount()
    {
        return $this->_scopeConfig->getValue(self::MINIMUM_COUNT);
    }

    public function getTextAlign()
    {
        return $this->_scopeConfig->getValue(self::TEXT_ALIGN);
    }

    public function getPlpBackgroundcolor()
    {
        return $this->_scopeConfig->getValue(self::PLP_BACKGROUNDCOLOR);
    }

    public function getPlpTextcolor()
    {
        return $this->_scopeConfig->getValue(self::PLP_TEXTCOLOR);
    }

    public function getBorderRadious()
    {
        return $this->_scopeConfig->getValue(self::BORDER_RADIOUS);
    }

    public function getPaddingTopbottom()
    {
        return $this->_scopeConfig->getValue(self::PADDING_TOP_BOTTOM);
    }

    public function getPaddingLeftright()
    {
        return $this->_scopeConfig->getValue(self::PADDING_LEFT_RIGHT);
    }

    public function getFontSize()
    {
        return $this->_scopeConfig->getValue(self::FONT_SIZE);
    }

    Public function getSalesCounttext()
    {
        return $this->_scopeConfig->getValue(self::SALES_COUNT_TEXT);
    }

    Public function getPviewBackground()
    {
        return $this->_scopeConfig->getValue(self::PRODUCT_VIEW_BACKGROUND);
    }

    Public function getPviewTextcolor()
    {
        return $this->_scopeConfig->getValue(self::PRODUCT_VIEW_TEXT_COLOR);
    }

    public function getSoldQtyByProductId($producID = null)
    {
        $SoldProducts = $this->_reportCollectionFactory->create();
        $SoldProdudctCOl = $SoldProducts->addOrderedQty()->addAttributeToFilter('product_id', $producID);
        /* If does have any product id 
         * then return false
         */
        if (!$SoldProdudctCOl->count()):
            return false;
        endif;
        echo $SoldProdudctCOl->getSelect()->__toString();

        $product = $SoldProdudctCOl
            ->getFirstItem();

        return (int)$product->getData('ordered_qty');
    }
}

Block:

<?php

namespace [Vendorname]\[ModuleName]\Block\Catalog\Product;
class [ModuleName] extends \Magento\Framework\View\Element\Template
{
    /**
     * @var \[Vendorname]\[ModuleName]\Helper\Data
     */
    protected $_helper;

    /**
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $_objectManager;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        array $data = [],
        \[Vendorname]\[ModuleName]\Helper\Data $helper,
        \Magento\Framework\ObjectManagerInterface $objectManager
    )
    {
        parent::__construct($context, $data);

        $this->_helper
            = $helper;
        $this->_objectManager = $objectManager;
    }

    public function getMinimumCount()
    {


        return $this->_helper->getMinimumCount();
    }

    public function getTextAlign()
    {
        return $this->_helper->getTextAlign();
    }

    public function getPlpBackgroundcolor()
    {
        return $this->_helper->getPlpBackgroundcolor();

    }

    public function getPlpTextcolor()
    {
        return $this->_helper->getPlpTextcolor();
    }

    public function getBorderRadious()
    {
        return $this->_helper->getBorderRadious();
    }

    public function getPaddingTopbottom()
    {
        return $this->_helper->getPaddingTopbottom();
    }

    public function getPaddingLeftright()
    {
        return $this->_helper->getPaddingLeftright();
    }

    public function getFontSize()
    {
        return $this->_helper->getFontSize();
    }

    Public function getSalesCounttext()
    {
        return $this->_helper->getSalesCounttext();
    }

    Public function getPviewBackground()
    {
        return $this->_helper->getPviewBackground();
    }

    Public function getPviewTextcolor()
    {
        return $this->_helper->getPviewTextcolor();
    }

    Public function getSaleCount()
    {

        //$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        //$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
        //$id = $product->getId();
        $productId = 1;

        return $this->_helper->getSoldQtyByProductId($productId);

        //echo $product->getName();

    }

    protected function _toHtml()
    {
        if ($this->_helper->getEnable()) {
            return parent::_toHtml();
        } else {
            return '';
        }
    }

    public function getCollection()
    {
        $model = $this->_objectManager->create('[Vendorname]\[ModuleName]\Model\[File_name]');
        $collection = $model->getCollection();

        return $collection;
    }

}

view.phtml:

<div class="vendorname-Modulename-block" style="text-align:
<?= $this->getTextAlign() ?>;
    background-color:<?= $this->getPviewBackground() ?>;color:
<?= $this->getPviewTextcolor() ?>;font-size:
<?= $this->getFontSize() ?>px;border-radius:
<?= $this->getBorderRadious() ?>px; padding:
<?= $this->getPaddingTopbottom() ?>px
<?= $this->getPaddingLeftright() ?>px; width:70%;">
    <h3 class="label"><?= $this->getSaleCount() ?>
        <?= $this->getSalesCounttext() ?></h3>
</div>

Here everything working fine like colors, etc., but i cannot able to get sales count here.

When i use $producID == null in public function getSoldQtyByProductId() i got below error message :

Parse error: syntax error, unexpected '==' (T_IS_EQUAL), expecting ')' in magento2\app\code[Vendorname][ModuleName]\Helper\Data.php on line 84

  • what is logic behind count of sales of a products? – Amit Bera Jun 29 '16 at 7:47
  • i want to get particular product sale count ,and want to display that count to category list page and product view page – paramuduraisamy Jun 29 '16 at 8:10
  • same like what we have used in magento 1 Mage::getModel('sales/order')->getCollection() – paramuduraisamy Jun 29 '16 at 10:40
  • waiting i have checking and get back to you – Amit Bera Jun 29 '16 at 10:41
4

For this case,you need to create module which has a Helper.

At this helper class have a function getSoldQtyByProductId($productid).,which will give you no of sold products basic of order placed.

Helper class:

<?php
namespace [Vendorname]\[ModuleName]\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    protected $_reportCollectionFactory;    
     public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Reports\Model\ResourceModel\Product\Sold\CollectionFactory  $reportCollectionFactory
    ) {
    $this->_reportCollectionFactory = $reportCollectionFactory;
        parent::__construct($context);
    }

    public function getSoldQtyByProductId($producID=null){
        $SoldProducts= $this->_reportCollectionFactory->create();
        $SoldProdudctCOl=$SoldProducts->addOrderedQty()->addAttributeToFilter('product_id', $producID);
        /* If does have any product id 
         * then return false
         */
        if(!$SoldProdudctCOl->count()):
                    return false;
        endif;
        echo $SoldProdudctCOl->getSelect()->__toString();

        $product = $SoldProdudctCOl
                        ->getFirstItem();

        return (int)$product->getData('ordered_qty');
    }
}

Now get Qty by below code:

$_helper = $this->_helper = $this->helper('[Vendorname]\[ModuleName]\Helper\Data');
$_helper->getSoldQtyByProductId($productId);
  • hello Amit..i have update my code above, kindly check it. – paramuduraisamy Jun 30 '16 at 6:40
  • still i cannot able to get sales count. – paramuduraisamy Jun 30 '16 at 6:41
  • syntax mistake getSoldQtyByProductId($producID==null) should be getSoldQtyByProductId($producID=null) – Amit Bera Jun 30 '16 at 7:59
  • yes Amit i used like getSoldQtyByProductId($producID=null) but i did not get any count – paramuduraisamy Jun 30 '16 at 9:03
  • 2
    i got it Thank you "$SoldProdudctCOl=$SoldProducts->addOrderedQty()->addAttributeToFilter('product_id', $producID)->addAttributeToFilter('state', 'complete');" – paramuduraisamy Jul 11 '16 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.