Skip to content

Commit

Permalink
#7768: Adding 'is_saleable' attribute to sort of product collection c…
Browse files Browse the repository at this point in the history
…auses exception and adding 'is_salable' has no effect.
  • Loading branch information
nmalevanec committed Dec 18, 2017
1 parent 3c59bd5 commit 6267566
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1670,7 +1670,7 @@ public function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC)

return $this;
} elseif ($attribute == 'is_saleable') {
$this->getSelect()->order("is_saleable " . $dir);
$this->getSelect()->order("is_salable " . $dir);
return $this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,40 @@ public function testGetProductsWithTierPrice()
$this->assertEquals(50, $tierPrices[2]->getExtensionAttributes()->getPercentageValue());
$this->assertEquals(5, $tierPrices[2]->getValue());
}

/**
* Test addAttributeToSort() with attribute 'is_saleable' works properly on frontend.
*
* @dataProvider addAttributeToSortDataProvider
* @magentoDataFixture Magento/Catalog/_files/multiple_products_with_non_saleable_product.php
* @magentoConfigFixture current_store cataloginventory/options/show_out_of_stock 1
* @magentoAppIsolation enabled
* @magentoAppArea frontend
*/
public function testAddAttributeToSort(string $productSku, string $order)
{
/** @var Collection $productCollection */
$this->collection->addAttributeToSort('is_saleable', $order);
self::assertEquals(2, $this->collection->count());
self::assertSame($productSku, $this->collection->getFirstItem()->getSku());
}

/**
* Provide test data for testAddAttributeToSort().
*
* @return array
*/
public function addAttributeToSortDataProvider()
{
return [
[
'product_sku' => 'simple_saleable',
'order' => Collection::SORT_ORDER_DESC,
],
[
'product_sku' => 'simple_not_saleable',
'order' => Collection::SORT_ORDER_ASC,
]
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

/** @var $product \Magento\Catalog\Model\Product */
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
$product->isObjectNew(true);
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
->setAttributeSetId(4)
->setName('Simple Product')
->setSku('simple_saleable')
->setTaxClassId('none')
->setDescription('description')
->setShortDescription('short description')
->setOptionsContainer('container1')
->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_IN_CART)
->setPrice(10)
->setWeight(1)
->setMetaTitle('meta title')
->setMetaKeyword('meta keyword')
->setMetaDescription('meta description')
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
->setWebsiteIds([1])
->setCateroryIds([])
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
->save();

$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
$product->isObjectNew(true);
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
->setAttributeSetId(4)
->setName('Simple Product2')
->setSku('simple_not_saleable')
->setTaxClassId('none')
->setDescription('description')
->setShortDescription('short description')
->setOptionsContainer('container1')
->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_ON_GESTURE)
->setPrice(20)
->setWeight(1)
->setMetaTitle('meta title')
->setMetaKeyword('meta keyword')
->setMetaDescription('meta description')
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_IN_CATALOG)
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
->setWebsiteIds([1])
->setCateroryIds([])
->setStockData(['use_config_manage_stock' => 1, 'qty' => 50, 'is_qty_decimal' => 0, 'is_in_stock' => 0])
->save();
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

/** @var \Magento\Framework\Registry $registry */
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);

$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);

/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);

foreach (['simple_saleable', 'simple_not_saleable'] as $sku) {
try {
$product = $productRepository->get($sku, false, null, true);
$productRepository->delete($product);
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
//Product already removed
}
}

$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);

0 comments on commit 6267566

Please sign in to comment.