I want to add Qty box in related products. It works when I add any related product but I need to add multiple product at a time so what should I do ?
Magento 2.1.0 – How to Add Multiple Products to Cart from Related Products Grid
magento2.1.0
Related Solutions
I created admin grid which has join of two custom tables. you cannot do this by using virtual type in di.xml, so you need to follow these steps and update your
etc/di.xml,
Model/Resource/Modulename/Collection.php add join in this file,
Model/Resource/Modulename/Grid/Collection.php,
IN your etc/di.xml
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<arguments>
<argument name="collections" xsi:type="array">
<item name="namespace_modulename_listing_data_source" xsi:type="string">Namespace\Modulename\Model\Resource\Modulename\Grid\Collection</item>
</argument>
</arguments>
</type>
<type name="Namespace\Modulename\Model\Resource\Modulename\Grid\Collection">
<arguments>
<argument name="mainTable" xsi:type="string">tablename</argument>
<argument name="eventPrefix" xsi:type="string">namespace_modulename_grid_collection</argument>
<argument name="eventObject" xsi:type="string">namespace_grid_collection</argument>
<argument name="resourceModel" xsi:type="string">Namespace\Modulename\Model\Resource\Modulename</argument>
</arguments>
</type>
IN your Model/Resource/Modulename/Collection.php
<?php
namespace Namespace\Modulename\Model\Resource\Modulename;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
class Collection extends AbstractCollection
{
/**
* Define model & resource model
*/
const YOUR_TABLE = 'tablename';
public function __construct(
\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
) {
$this->_init(
'Namespace\Modulename\Model\Modulename',
'Namespace\Modulename\Model\Resource\Modulename'
);
parent::__construct(
$entityFactory, $logger, $fetchStrategy, $eventManager, $connection,
$resource
);
$this->storeManager = $storeManager;
}
protected function _initSelect()
{
parent::_initSelect();
$this->getSelect()->joinLeft(
['secondTable' => $this->getTable('tablename')],
'main_table.columnname = secondTable.columnname',
['columnname1','columnname2','columnname3']
);
}
}
?>
IN your Model/Resource/Modulename/Grid/Collection.php
<?php
namespace Namespace\Modulename\Model\Resource\Modulename\Grid;
use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\Search\AggregationInterface;
use Namespace\Modulename\Model\Resource\Modulename\Collection as ModulenameCollection;
/**
* Class Collection
* Collection for displaying grid
*/
class Collection extends ModulenameCollection implements SearchResultInterface
{
/**
* Resource initialization
* @return $this
*/
public function __construct(
\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Store\Model\StoreManagerInterface $storeManager,
$mainTable,
$eventPrefix,
$eventObject,
$resourceModel,
$model = 'Magento\Framework\View\Element\UiComponent\DataProvider\Document',
$connection = null,
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
) {
parent::__construct(
$entityFactory,
$logger,
$fetchStrategy,
$eventManager,
$storeManager,
$connection,
$resource
);
$this->_eventPrefix = $eventPrefix;
$this->_eventObject = $eventObject;
$this->_init($model, $resourceModel);
$this->setMainTable($mainTable);
}
/**
* @return AggregationInterface
*/
public function getAggregations()
{
return $this->aggregations;
}
/**
* @param AggregationInterface $aggregations
*
* @return $this
*/
public function setAggregations($aggregations)
{
$this->aggregations = $aggregations;
}
/**
* Get search criteria.
*
* @return \Magento\Framework\Api\SearchCriteriaInterface|null
*/
public function getSearchCriteria()
{
return null;
}
/**
* Set search criteria.
*
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
*
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setSearchCriteria(
\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria = null
) {
return $this;
}
/**
* Get total count.
*
* @return int
*/
public function getTotalCount()
{
return $this->getSize();
}
/**
* Set total count.
*
* @param int $totalCount
*
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setTotalCount($totalCount)
{
return $this;
}
/**
* Set items list.
*
* @param \Magento\Framework\Api\ExtensibleDataInterface[] $items
*
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setItems(array $items = null)
{
return $this;
}
}
?>
Hope that helps.
By default this functionality already exist in M2. You can check following file
1. Magento/Wishlist/view/frontend/layout/checkout_cart_item_renderers.xml 2. Magento/Wishlist/view/frontend/templates/cart/item/renderer/actions/move_to_wishlist.phtml
In template file, there is an condition like
<?php if ($block->isAllowInCart() && $block->isProductVisibleInSiteVisibility()): ?>
If this condition is true then you can able to view 'Move to Wishlist' under product image that means
Simple thought is if you login then you can able to view this button in cart page.
[Updated]
Following two function make the 'Move to Wishlist' button visible.
#Magento/Wishlist/Block/Cart/Item/Renderer/Actions/MoveToWishlist.php /** * Check whether "add to wishlist" button is allowed in cart * * @return bool */ public function isAllowInCart() { return $this->wishlistHelper->isAllowInCart(); } #Magento/Checkout/Block/Cart/Item/Renderer/Actions/Generic.php /** * Check if product is visible in site visibility * * @return bool * @codeCoverageIgnore */ public function isProductVisibleInSiteVisibility() { return $this->getItem()->getProduct()->isVisibleInSiteVisibility(); }
First logic implement in helper class.
#Magento/Wishlist/Helper/Data.php /** * Check is allow wishlist action in shopping cart * * @return bool */ public function isAllowInCart() { return $this->isAllow() && $this->getCustomer(); }
Here $this->isAllow() return true if 'wishlist' module output is active(menas this module is not disabled from Admin->Stores->Configuration->Advanced) and and active this module too. $this->getCustomer() return true if login.
#Magento/Checkout/Block/Cart/Item/Renderer/Actions/Generic.php /** * Check if product is visible in site visibility * * @return bool * @codeCoverageIgnore */ public function isProductVisibleInSiteVisibility() { return $this->getItem()->getProduct()->isVisibleInSiteVisibility(); }
Best Answer
1 :- In related product you only need to give
$showCart = true;
value in your template file then you will be able to add to cart related products.2 :-The default catalog template file name is items.phtml.
3 :- But if you want to add Qty box then you need to add form there and need to use same action.Please use this code and customize as per your need and let me know if you have any query.