3

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!

1 Answer 1

3
+25

You can use the SearchInterface to sort things by a certain value.

You will need to edit the Block function getAllItems (if you don't know how to do this i can provide more information , just ask)

    use Magento\Framework\Api\SearchCriteriaInterface
    use Magento\Framework\Api\SortOrder;

        protected $_sortOrder;
        protected $_searchCriteriaInterface;

    public function __construct(
            SortOrder $sortOrder,
            SearchCriteriaInterface $searchCriteriaInterface
    ){
            $this->_sortOrder = $sortOrder;
            $this->_searchCriteriaInterface = $searchCriteriaInterface;
    }

    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;
    }

And wherever your items are getting called you need to adjust it to something like this :

public function getProductList()
{

    $products = $this->_productRepository->getList($this->getSearchCriteria());

    return $products;

}

Hope this helps.

If you need more information i will need the file path that you are adjusting and which block/xml.

9
  • Thanks a lot and please, explain. In "public function getProductList($value)" What is $value?
    – Supersonic
    Commented Oct 24, 2017 at 10:05
  • I editted it. it was a copy paste from something that i use and i use that to set something in the searchcritiria , you can just ignore it Commented Oct 25, 2017 at 0:10
  • You just need the getlist function from whatever you are calling and put the searchcriteria in it Commented Oct 25, 2017 at 0:11
  • Please, explain how to implement it in the block. What block and how should I override in my module to have it work on the 'checkout/cart' page? I do not have '$this->_productRepository->' My ' $products = $block->getQuote()->getAllItems() ' is an array. When I do ' $block->getQuote()->getList($this->getSearchCriteria()) ', I get : Uncaught Error: Call to a member function getList() on array... Thank you a lot!
    – Supersonic
    Commented Oct 25, 2017 at 11:02
  • Like i said you will need to override your block class and not your block template. You will need to create a new function within your new block class. If this all does not make any sense to you then you are new to templating. In that case i will need to know what you have in which location to help you out Commented Oct 25, 2017 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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