You can install Magento 2 extension easily from any VCS (github or bitbcuket) if the module is packed with proper composer.json file.
For example, in order to install Easy Template Path Hints 2 extension, you just have to run two commands for installation:
composer config repositories.magesycho-magento2-easy-template-path-hints git git@github.com:MagePsycho/magento2-easy-template-path-hints.git
composer require magepsycho/magento2-easy-template-path-hints:dev-master
Explanation:
1> Registering the module git repository
composer config repositories.magesycho-magento2-easy-template-path-hints git git@github.com:MagePsycho/magento2-easy-template-path-hints.git
Composer config repository command syntax looks like:
composer config repositories.<unique-repo-name> <vcs-type> <vcs-url-https-or-sshl>
Composer will register a new repository to composer.json (under "repositories" node). Updated composer.json looks like:
{
"repositories": {
"magesycho-magento2-easy-template-path-hints": {
"type": "git",
"url": "git@github.com:MagePsycho/magento2-easy-template-path-hints.git"
}
}
}
2> Registering the module package itself
composer require magepsycho/magento2-easy-template-path-hints:dev-master
Composer require command syntax:
composer require <vendor>/<package>:dev-<branch>
This will add new dependent package under node “require” as:
{
"name": "magento/magento2ce",
"description": "Magento 2 (Community Edition)",
"type": "project",
"require": {
"magepsycho/magento2-easy-template-path-hints": "dev-master"
}
}
and download the module from the repo.
And if you have extension zip file, just extract it and copy it to the app/code/
folder - as that simple.
Once the Module is installed, you can simply enable it using following commands:
php bin/magento module:enable MagePsycho_Easypathhints --clear-static-content
php bin/magento setup:upgrade
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);
Best Answer
The folder structure is correct for development. However, for a Marketplace upload, you should only package the module directory. Magento 2 extensions will be installed using the Component Manager, which uses Composer under the hood. The Component Manager and Composer will take care of extracting and installing your package to the correct location. Generally, this will be under the vendor/ directory. Based on the information in the registration.php and composer.json's "autoload" section, the module will be installed in the following structure:
If your registration.php and composer.json files are set up correctly, then the Magento application will be able to identify and use that module. Furthermore, Composer will take care of any namespaces you have created and any namespaces you use in your module.
The aim of this tool is to validate your package structure in preparation for an upload to the Marketplace.