/** * testGetTierPrices * testSetTierPrices * * @dataProvider pricesDataProvider */ public function testTierPrices($priceScope, $expectedWebsiteId) { // establish the behavior of the mocks $this->scopeConfigMock->expects($this->any())->method('getValue')->will($this->returnValue($priceScope)); $this->websiteMock->expects($this->any())->method('getId')->will($this->returnValue($expectedWebsiteId)); $this->tpFactory->expects($this->any())->method('create')->will($this->returnCallback(function () { return $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\Product\\TierPrice'); })); // create sample TierPrice objects that would be coming from a REST call $tp1 = $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\Product\\TierPrice'); $tp1->setValue(10); $tp1->setCustomerGroupId(1); $tp1->setQty(11); $tp2 = $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\Product\\TierPrice'); $tp2->setValue(20); $tp2->setCustomerGroupId(2); $tp2->setQty(22); $tps = [$tp1, $tp2]; // force the product to have null tier prices $this->product->setData($this::KEY_TIER_PRICE, null); $this->assertNull($this->product->getData($this::KEY_TIER_PRICE)); // set the product with the TierPrice objects $this->model->setTierPrices($this->product, $tps); // test the data actually set on the product $tpArray = $this->product->getData($this::KEY_TIER_PRICE); $this->assertNotNull($tpArray); $this->assertTrue(is_array($tpArray)); $this->assertEquals(sizeof($tps), sizeof($tpArray)); for ($i = 0; $i < sizeof($tps); $i++) { $tpData = $tpArray[$i]; $this->assertEquals($expectedWebsiteId, $tpData['website_id'], 'Website Id does not match'); $this->assertEquals($tps[$i]->getValue(), $tpData['price'], 'Price/Value does not match'); $this->assertEquals($tps[$i]->getValue(), $tpData['website_price'], 'WebsitePrice/Value does not match'); $this->assertEquals($tps[$i]->getCustomerGroupId(), $tpData['cust_group'], 'Customer group Id does not match'); $this->assertEquals($tps[$i]->getQty(), $tpData['price_qty'], 'Qty does not match'); } // test with the data retrieved as a REST object $tpRests = $this->model->getTierPrices($this->product); $this->assertNotNull($tpRests); $this->assertTrue(is_array($tpRests)); $this->assertEquals(sizeof($tps), sizeof($tpRests)); for ($i = 0; $i < sizeof($tps); $i++) { $this->assertEquals($tps[$i]->getValue(), $tpRests[$i]->getValue(), 'REST: Price/Value does not match'); $this->assertEquals($tps[$i]->getCustomerGroupId(), $tpRests[$i]->getCustomerGroupId(), 'REST: Customer group Id does not match'); $this->assertEquals($tps[$i]->getQty(), $tpRests[$i]->getQty(), 'REST: Qty does not match'); } }
/** * {@inheritdoc} */ public function getList($sku, $customerGroupId) { $product = $this->productRepository->get($sku, ['edit_mode' => true]); $priceKey = 'website_price'; $value = $this->config->getValue('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE); if ($value == 0) { $priceKey = 'price'; } $prices = []; foreach ($product->getData('tier_price') as $price) { if (is_numeric($customerGroupId) && intval($price['cust_group']) === intval($customerGroupId) || $customerGroupId === 'all' && $price['all_groups']) { /** @var \Magento\Catalog\Api\Data\ProductTierPriceInterface $tierPrice */ $tierPrice = $this->priceFactory->create(); $tierPrice->setValue($price[$priceKey])->setQty($price['price_qty']); $prices[] = $tierPrice; } } return $prices; }
/** * Gets list of product tier prices * * @param Product $product * @return \Magento\Catalog\Api\Data\ProductTierPriceInterface[] */ public function getTierPrices($product) { $prices = []; $tierPrices = $this->getExistingPrices($product, 'tier_price'); foreach ($tierPrices as $price) { /** @var \Magento\Catalog\Api\Data\ProductTierPriceInterface $tierPrice */ $tierPrice = $this->tierPriceFactory->create(); $tierPrice->setCustomerGroupId($price['cust_group']); if (array_key_exists('website_price', $price)) { $value = $price['website_price']; } else { $value = $price['price']; } $tierPrice->setValue($value); $tierPrice->setQty($price['price_qty']); $prices[] = $tierPrice; } return $prices; }