Why sort by price order not working?












0














I am using below script to desc order price collection but it's not working.



This collection is working for a simple product but configurable products not working:



    $productsCollection->addFinalPrice()->addMinimalPrice()->addAttributeToSort('price',"desc");
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
foreach ($productsCollection as $product) {
/*print_r($product->getData());*/
$productModel = $objectManager->create('MagentoCatalogModelProduct');
$product = $productModel->load($product->getEntityId());
$attributeOptions = ;
$getChildId = ;
$configurable = ;
$attributeVariant = '';
if($product->getTypeId() != 'simple' && $product->getTypeId() != 'virtual') {
/*if(!$product Instanceof MagentoCatalogModelProduct){
continue;
}*/
$productAttributeOptions = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
foreach ($productAttributeOptions as $productAttribute) {
$configurable = ['id' => $productAttribute['id'],
'value' => $productAttribute['label']];
$attributeVariant = $productAttribute['label'];
foreach ($productAttribute['values'] as $attribute) {
$attributeOptions[$productAttribute['label']] = [
'id' => $attribute['value_index'],
'value' => $attribute['store_label']];
}
$_configChild = $product->getTypeInstance()->getUsedProducts($product);

$attributeCode = $productAttribute['attribute_code'];
foreach ($_configChild as $child) {
/*print_r($child->getData());*/
$originalPrice = $child->getPrice() == '' ? 0 : $child->getPrice();
$finalPrice = $child->getSpecialPrice() == '' ? 0 : $child->getSpecialPrice();
$percentage = 0;
if ($originalPrice > $finalPrice) {
$percentage = number_format(($originalPrice - $finalPrice) * 100 / $originalPrice,0);
}
$quantityChild = $this->stockManagerInterface->getStockQty($child->getId(), $child->getStore()->getWebsiteId());
$getChildId = ['id' => $child->getId(),
'sku' => $child->getSku(),
'name' => $child->getName(),
'quantity' => $quantityChild == '' ? 0 : $quantityChild,
'base_price' => $child->getPrice() == '' ? 0 : $child->getPrice(),
'price' => $child->getFinalPrice() == '' ? 0 : $child->getFinalPrice(),
'attribute_code' => $child->getData($attributeCode) == '' ? '' : $child->getData($attributeCode),
'image' => $this->getMediaUrl()."catalog/product".$child->getImage(),
'url' => $child->getProductUrl(),
'discount' => $percentage
];
}
}
}
$quantity = (int)$this->stockManagerInterface->getStockQty($product->getId(), 1);
$outOfStock = $product->getData('is_in_stock');
if($product->getTypeId() == 'simple' || ($product->getTypeId() == 'configurable' && count($getChildId) > 0)) {
$originalPrice = $product->getPrice() == '' ? 0 : $product->getPrice();
$finalPrice = $product->getSpecialPrice() == '' ? 0 : $product->getSpecialPrice();
$percentage = 0;
if ($originalPrice > $finalPrice) {
$percentage = number_format(($originalPrice - $finalPrice) * 100 / $originalPrice,0);
}
$currentProduct = ['id' => $product->getId(),
'sku' => $product->getSku(),
'name' => $product->getName(),
'configurable-options' => $configurable,
'attribute-variant' => $attributeVariant,
'quantity' => $quantity>0 ? $quantity : 0,
'base_price' => $product->getPrice() == '' ? 0 : $product->getPrice(),
'price' => $product->getFinalPrice() == '' ? 0 : $product->getFinalPrice(),
'outof-stock' => $outOfStock,
'type' => $product->getTypeId(),
'attribute' => (object)$attributeOptions,
'image' => $this->getMediaUrl()."catalog/product".$product->getImage(),
'url' => $product->getProductUrl(),
'child' => ($getChildId),
'pagination' => ceil($productsCollection->getSize() / 20),
'discount' => $percentage
];
$categoriesArrays[$i] = $currentProduct;

$i++;
}
}


If it's not an asc or desc what is the problem here?










share|improve this question
















bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Works fine for me ordering the collection by price.
    – Nikolas
    Nov 24 '17 at 9:49










  • Check my updated question
    – Magento 2
    Nov 24 '17 at 9:50










  • got any solution for this issue? i have the same problem that the price sorting is not properly working for the configurable products.
    – amesh
    Dec 12 '18 at 8:02
















0














I am using below script to desc order price collection but it's not working.



This collection is working for a simple product but configurable products not working:



    $productsCollection->addFinalPrice()->addMinimalPrice()->addAttributeToSort('price',"desc");
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
foreach ($productsCollection as $product) {
/*print_r($product->getData());*/
$productModel = $objectManager->create('MagentoCatalogModelProduct');
$product = $productModel->load($product->getEntityId());
$attributeOptions = ;
$getChildId = ;
$configurable = ;
$attributeVariant = '';
if($product->getTypeId() != 'simple' && $product->getTypeId() != 'virtual') {
/*if(!$product Instanceof MagentoCatalogModelProduct){
continue;
}*/
$productAttributeOptions = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
foreach ($productAttributeOptions as $productAttribute) {
$configurable = ['id' => $productAttribute['id'],
'value' => $productAttribute['label']];
$attributeVariant = $productAttribute['label'];
foreach ($productAttribute['values'] as $attribute) {
$attributeOptions[$productAttribute['label']] = [
'id' => $attribute['value_index'],
'value' => $attribute['store_label']];
}
$_configChild = $product->getTypeInstance()->getUsedProducts($product);

$attributeCode = $productAttribute['attribute_code'];
foreach ($_configChild as $child) {
/*print_r($child->getData());*/
$originalPrice = $child->getPrice() == '' ? 0 : $child->getPrice();
$finalPrice = $child->getSpecialPrice() == '' ? 0 : $child->getSpecialPrice();
$percentage = 0;
if ($originalPrice > $finalPrice) {
$percentage = number_format(($originalPrice - $finalPrice) * 100 / $originalPrice,0);
}
$quantityChild = $this->stockManagerInterface->getStockQty($child->getId(), $child->getStore()->getWebsiteId());
$getChildId = ['id' => $child->getId(),
'sku' => $child->getSku(),
'name' => $child->getName(),
'quantity' => $quantityChild == '' ? 0 : $quantityChild,
'base_price' => $child->getPrice() == '' ? 0 : $child->getPrice(),
'price' => $child->getFinalPrice() == '' ? 0 : $child->getFinalPrice(),
'attribute_code' => $child->getData($attributeCode) == '' ? '' : $child->getData($attributeCode),
'image' => $this->getMediaUrl()."catalog/product".$child->getImage(),
'url' => $child->getProductUrl(),
'discount' => $percentage
];
}
}
}
$quantity = (int)$this->stockManagerInterface->getStockQty($product->getId(), 1);
$outOfStock = $product->getData('is_in_stock');
if($product->getTypeId() == 'simple' || ($product->getTypeId() == 'configurable' && count($getChildId) > 0)) {
$originalPrice = $product->getPrice() == '' ? 0 : $product->getPrice();
$finalPrice = $product->getSpecialPrice() == '' ? 0 : $product->getSpecialPrice();
$percentage = 0;
if ($originalPrice > $finalPrice) {
$percentage = number_format(($originalPrice - $finalPrice) * 100 / $originalPrice,0);
}
$currentProduct = ['id' => $product->getId(),
'sku' => $product->getSku(),
'name' => $product->getName(),
'configurable-options' => $configurable,
'attribute-variant' => $attributeVariant,
'quantity' => $quantity>0 ? $quantity : 0,
'base_price' => $product->getPrice() == '' ? 0 : $product->getPrice(),
'price' => $product->getFinalPrice() == '' ? 0 : $product->getFinalPrice(),
'outof-stock' => $outOfStock,
'type' => $product->getTypeId(),
'attribute' => (object)$attributeOptions,
'image' => $this->getMediaUrl()."catalog/product".$product->getImage(),
'url' => $product->getProductUrl(),
'child' => ($getChildId),
'pagination' => ceil($productsCollection->getSize() / 20),
'discount' => $percentage
];
$categoriesArrays[$i] = $currentProduct;

$i++;
}
}


If it's not an asc or desc what is the problem here?










share|improve this question
















bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Works fine for me ordering the collection by price.
    – Nikolas
    Nov 24 '17 at 9:49










  • Check my updated question
    – Magento 2
    Nov 24 '17 at 9:50










  • got any solution for this issue? i have the same problem that the price sorting is not properly working for the configurable products.
    – amesh
    Dec 12 '18 at 8:02














0












0








0







I am using below script to desc order price collection but it's not working.



This collection is working for a simple product but configurable products not working:



    $productsCollection->addFinalPrice()->addMinimalPrice()->addAttributeToSort('price',"desc");
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
foreach ($productsCollection as $product) {
/*print_r($product->getData());*/
$productModel = $objectManager->create('MagentoCatalogModelProduct');
$product = $productModel->load($product->getEntityId());
$attributeOptions = ;
$getChildId = ;
$configurable = ;
$attributeVariant = '';
if($product->getTypeId() != 'simple' && $product->getTypeId() != 'virtual') {
/*if(!$product Instanceof MagentoCatalogModelProduct){
continue;
}*/
$productAttributeOptions = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
foreach ($productAttributeOptions as $productAttribute) {
$configurable = ['id' => $productAttribute['id'],
'value' => $productAttribute['label']];
$attributeVariant = $productAttribute['label'];
foreach ($productAttribute['values'] as $attribute) {
$attributeOptions[$productAttribute['label']] = [
'id' => $attribute['value_index'],
'value' => $attribute['store_label']];
}
$_configChild = $product->getTypeInstance()->getUsedProducts($product);

$attributeCode = $productAttribute['attribute_code'];
foreach ($_configChild as $child) {
/*print_r($child->getData());*/
$originalPrice = $child->getPrice() == '' ? 0 : $child->getPrice();
$finalPrice = $child->getSpecialPrice() == '' ? 0 : $child->getSpecialPrice();
$percentage = 0;
if ($originalPrice > $finalPrice) {
$percentage = number_format(($originalPrice - $finalPrice) * 100 / $originalPrice,0);
}
$quantityChild = $this->stockManagerInterface->getStockQty($child->getId(), $child->getStore()->getWebsiteId());
$getChildId = ['id' => $child->getId(),
'sku' => $child->getSku(),
'name' => $child->getName(),
'quantity' => $quantityChild == '' ? 0 : $quantityChild,
'base_price' => $child->getPrice() == '' ? 0 : $child->getPrice(),
'price' => $child->getFinalPrice() == '' ? 0 : $child->getFinalPrice(),
'attribute_code' => $child->getData($attributeCode) == '' ? '' : $child->getData($attributeCode),
'image' => $this->getMediaUrl()."catalog/product".$child->getImage(),
'url' => $child->getProductUrl(),
'discount' => $percentage
];
}
}
}
$quantity = (int)$this->stockManagerInterface->getStockQty($product->getId(), 1);
$outOfStock = $product->getData('is_in_stock');
if($product->getTypeId() == 'simple' || ($product->getTypeId() == 'configurable' && count($getChildId) > 0)) {
$originalPrice = $product->getPrice() == '' ? 0 : $product->getPrice();
$finalPrice = $product->getSpecialPrice() == '' ? 0 : $product->getSpecialPrice();
$percentage = 0;
if ($originalPrice > $finalPrice) {
$percentage = number_format(($originalPrice - $finalPrice) * 100 / $originalPrice,0);
}
$currentProduct = ['id' => $product->getId(),
'sku' => $product->getSku(),
'name' => $product->getName(),
'configurable-options' => $configurable,
'attribute-variant' => $attributeVariant,
'quantity' => $quantity>0 ? $quantity : 0,
'base_price' => $product->getPrice() == '' ? 0 : $product->getPrice(),
'price' => $product->getFinalPrice() == '' ? 0 : $product->getFinalPrice(),
'outof-stock' => $outOfStock,
'type' => $product->getTypeId(),
'attribute' => (object)$attributeOptions,
'image' => $this->getMediaUrl()."catalog/product".$product->getImage(),
'url' => $product->getProductUrl(),
'child' => ($getChildId),
'pagination' => ceil($productsCollection->getSize() / 20),
'discount' => $percentage
];
$categoriesArrays[$i] = $currentProduct;

$i++;
}
}


If it's not an asc or desc what is the problem here?










share|improve this question















I am using below script to desc order price collection but it's not working.



This collection is working for a simple product but configurable products not working:



    $productsCollection->addFinalPrice()->addMinimalPrice()->addAttributeToSort('price',"desc");
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
foreach ($productsCollection as $product) {
/*print_r($product->getData());*/
$productModel = $objectManager->create('MagentoCatalogModelProduct');
$product = $productModel->load($product->getEntityId());
$attributeOptions = ;
$getChildId = ;
$configurable = ;
$attributeVariant = '';
if($product->getTypeId() != 'simple' && $product->getTypeId() != 'virtual') {
/*if(!$product Instanceof MagentoCatalogModelProduct){
continue;
}*/
$productAttributeOptions = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
foreach ($productAttributeOptions as $productAttribute) {
$configurable = ['id' => $productAttribute['id'],
'value' => $productAttribute['label']];
$attributeVariant = $productAttribute['label'];
foreach ($productAttribute['values'] as $attribute) {
$attributeOptions[$productAttribute['label']] = [
'id' => $attribute['value_index'],
'value' => $attribute['store_label']];
}
$_configChild = $product->getTypeInstance()->getUsedProducts($product);

$attributeCode = $productAttribute['attribute_code'];
foreach ($_configChild as $child) {
/*print_r($child->getData());*/
$originalPrice = $child->getPrice() == '' ? 0 : $child->getPrice();
$finalPrice = $child->getSpecialPrice() == '' ? 0 : $child->getSpecialPrice();
$percentage = 0;
if ($originalPrice > $finalPrice) {
$percentage = number_format(($originalPrice - $finalPrice) * 100 / $originalPrice,0);
}
$quantityChild = $this->stockManagerInterface->getStockQty($child->getId(), $child->getStore()->getWebsiteId());
$getChildId = ['id' => $child->getId(),
'sku' => $child->getSku(),
'name' => $child->getName(),
'quantity' => $quantityChild == '' ? 0 : $quantityChild,
'base_price' => $child->getPrice() == '' ? 0 : $child->getPrice(),
'price' => $child->getFinalPrice() == '' ? 0 : $child->getFinalPrice(),
'attribute_code' => $child->getData($attributeCode) == '' ? '' : $child->getData($attributeCode),
'image' => $this->getMediaUrl()."catalog/product".$child->getImage(),
'url' => $child->getProductUrl(),
'discount' => $percentage
];
}
}
}
$quantity = (int)$this->stockManagerInterface->getStockQty($product->getId(), 1);
$outOfStock = $product->getData('is_in_stock');
if($product->getTypeId() == 'simple' || ($product->getTypeId() == 'configurable' && count($getChildId) > 0)) {
$originalPrice = $product->getPrice() == '' ? 0 : $product->getPrice();
$finalPrice = $product->getSpecialPrice() == '' ? 0 : $product->getSpecialPrice();
$percentage = 0;
if ($originalPrice > $finalPrice) {
$percentage = number_format(($originalPrice - $finalPrice) * 100 / $originalPrice,0);
}
$currentProduct = ['id' => $product->getId(),
'sku' => $product->getSku(),
'name' => $product->getName(),
'configurable-options' => $configurable,
'attribute-variant' => $attributeVariant,
'quantity' => $quantity>0 ? $quantity : 0,
'base_price' => $product->getPrice() == '' ? 0 : $product->getPrice(),
'price' => $product->getFinalPrice() == '' ? 0 : $product->getFinalPrice(),
'outof-stock' => $outOfStock,
'type' => $product->getTypeId(),
'attribute' => (object)$attributeOptions,
'image' => $this->getMediaUrl()."catalog/product".$product->getImage(),
'url' => $product->getProductUrl(),
'child' => ($getChildId),
'pagination' => ceil($productsCollection->getSize() / 20),
'discount' => $percentage
];
$categoriesArrays[$i] = $currentProduct;

$i++;
}
}


If it's not an asc or desc what is the problem here?







magento2 product-collection sorting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 23 '18 at 21:44









Rafael Corrêa Gomes

4,24222962




4,24222962










asked Nov 22 '17 at 10:46









Magento 2

2,38662770




2,38662770





bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • Works fine for me ordering the collection by price.
    – Nikolas
    Nov 24 '17 at 9:49










  • Check my updated question
    – Magento 2
    Nov 24 '17 at 9:50










  • got any solution for this issue? i have the same problem that the price sorting is not properly working for the configurable products.
    – amesh
    Dec 12 '18 at 8:02


















  • Works fine for me ordering the collection by price.
    – Nikolas
    Nov 24 '17 at 9:49










  • Check my updated question
    – Magento 2
    Nov 24 '17 at 9:50










  • got any solution for this issue? i have the same problem that the price sorting is not properly working for the configurable products.
    – amesh
    Dec 12 '18 at 8:02
















Works fine for me ordering the collection by price.
– Nikolas
Nov 24 '17 at 9:49




Works fine for me ordering the collection by price.
– Nikolas
Nov 24 '17 at 9:49












Check my updated question
– Magento 2
Nov 24 '17 at 9:50




Check my updated question
– Magento 2
Nov 24 '17 at 9:50












got any solution for this issue? i have the same problem that the price sorting is not properly working for the configurable products.
– amesh
Dec 12 '18 at 8:02




got any solution for this issue? i have the same problem that the price sorting is not properly working for the configurable products.
– amesh
Dec 12 '18 at 8:02










1 Answer
1






active

oldest

votes


















0














For Me below code works fine



 $productsCollection->setOrder('price','ASC'); //DESC for decending 





share|improve this answer





















  • I tried this also simple product is working but not configurable product
    – Magento 2
    Nov 24 '17 at 10:12











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f202708%2fwhy-sort-by-price-order-not-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














For Me below code works fine



 $productsCollection->setOrder('price','ASC'); //DESC for decending 





share|improve this answer





















  • I tried this also simple product is working but not configurable product
    – Magento 2
    Nov 24 '17 at 10:12
















0














For Me below code works fine



 $productsCollection->setOrder('price','ASC'); //DESC for decending 





share|improve this answer





















  • I tried this also simple product is working but not configurable product
    – Magento 2
    Nov 24 '17 at 10:12














0












0








0






For Me below code works fine



 $productsCollection->setOrder('price','ASC'); //DESC for decending 





share|improve this answer












For Me below code works fine



 $productsCollection->setOrder('price','ASC'); //DESC for decending 






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '17 at 10:09









Murtuza Zabuawala

12.3k73260




12.3k73260












  • I tried this also simple product is working but not configurable product
    – Magento 2
    Nov 24 '17 at 10:12


















  • I tried this also simple product is working but not configurable product
    – Magento 2
    Nov 24 '17 at 10:12
















I tried this also simple product is working but not configurable product
– Magento 2
Nov 24 '17 at 10:12




I tried this also simple product is working but not configurable product
– Magento 2
Nov 24 '17 at 10:12


















draft saved

draft discarded




















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f202708%2fwhy-sort-by-price-order-not-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

An IMO inspired problem

Management

Investment