I have configurable products with virtual variations by color and size.
In the cart, I am trying to arrange them in the order of priority, by configurable parent, then by color, then by size.
In my ovverriden cart template, I am calling the cart items like this :
if ($block->getItemsCount()) {
if($cartItems = $block->getQuote()->getAllItems()){
if($confId = $product->getId()){
$configQty = $v->getQty();
if($product){
/* the logic */
}
}
}
}
In the admin "Stores >> Configuratin >> Sales >> Checkout >> Configurable Product Image" I have activated "Product Thumbnail Itself", to have the individual image Swatches of each virtual Variation.
Could you tell me what I should do in order to get the products collection sorted by 'color', please?
Here is the code that I have in a separate file at the root of my project:
<?php
require __DIR__ . '/app/bootstrap.php';
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SortOrder;
class TestApp
extends \Magento\Framework\App\Http
implements Magento\Framework\Api\SearchCriteriaInterface, \Magento\Framework\AppInterface{
public function __construct(
\Magento\Framework\App\State $state,
SortOrder $sortOrder,
SearchCriteriaInterface $searchCriteriaInterface
){
$state->setAreaCode('frontend'); // or 'adminhtml', depending on your needs
$this->_sortOrder = $sortOrder;
$this->_searchCriteriaInterface = $searchCriteriaInterface;
}
public function getCurrentPage(){}
public function setCurrentPage($currentPage){}
public function getFilterGroups(){}
public function setFilterGroups(array $filterGroups = NULL){}
public function getSortOrders(){}
public function setSortOrders(array $sortOrders = NULL){}
public function getPageSize(){}
public function setPageSize($currentPage){}
protected function getSearchCriteria(){
$searchcriteriaint = $this->_searchCriteriaInterface;
$searchcriteriaint->setSortOrders(array($this->getSortOrder()));
return $searchcriteriaint;
}
protected function getSortOrder(){
$sortorder = $this->_sortOrder;
$sortorder->setField('color')->setDirection(SortOrder::SORT_ASC);
return $sortorder;
}
public function launch(){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');
// // get quote items collection
// $itemsCollection = $cart->getQuote()->getItemsCollection();
// // get array of all items what can be display directly
$items = $cart->getQuote()->getAllVisibleItems($this->getSearchCriteria());
foreach($items as $key => $item) {
$productId = $item->getProductId();
$_product = $item->getProduct();
$product = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
echo 'Type: '.$item->getProductType().'<br/>';
echo 'ID: '.$item->getProductId().'<br/>';
echo 'Name: '.$item->getName().'<br/>';
echo 'Sku: '.$item->getSku().'<br/>';
echo 'Quantity: '.$item->getQty().'<br/>';
echo 'Price: '.$item->getPrice().'<br/>';
echo "<br />";
}
// return $this->_response;
}
public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
{
return false;
}
}
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('TestApp');
$bootstrap->run($app);
Don't hesitate to point out good vs bad practice.
Many thanks!