/** * {@inheritdoc} */ public function getList($sku, $websiteId = null) { $product = $this->productRepository->get($sku, 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('group_price') as $price) { /** @var \Magento\Catalog\Api\Data\ProductGroupPriceInterface $groupPrice */ $groupPrice = $this->groupPriceFactory->create(); $groupPrice->setCustomerGroupId($price['all_groups'] ? 'all' : $price['cust_group'])->setValue($price[$priceKey]); $prices[] = $groupPrice; } return $prices; }
/** * Gets list of product group prices * * @param Product $product * @return \Magento\Catalog\Api\Data\ProductGroupPriceInterface[] */ public function getGroupPrices($product) { $prices = []; $groupPrices = $this->getExistingPrices($product, 'group_price'); foreach ($groupPrices as $price) { /** @var \Magento\Catalog\Api\Data\ProductGroupPriceInterface $groupPrice */ $groupPrice = $this->groupPriceFactory->create(); $groupPrice->setCustomerGroupId($price['cust_group']); if (array_key_exists('website_price', $price)) { $value = $price['website_price']; } else { $value = $price['price']; } $groupPrice->setValue($value); $prices[] = $groupPrice; } return $prices; }
/** * testGetGroupPrices * testSetGroupPrices * * @dataProvider pricesDataProvider */ public function testGroupPrices($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->gpFactory->expects($this->any())->method('create')->will($this->returnCallback(function () { return $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\Product\\GroupPrice'); })); // create sample GroupPrice objects that would be coming from a REST call $gp1 = $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\Product\\GroupPrice'); $gp1->setValue(10); $gp1->setCustomerGroupId(1); $gp2 = $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\Product\\GroupPrice'); $gp2->setValue(20); $gp2->setCustomerGroupId(2); $gps = [$gp1, $gp2]; // force the product to have null group prices $this->product->setData($this::KEY_GROUP_PRICE, null); $this->assertNull($this->product->getData($this::KEY_GROUP_PRICE)); // set the product with the GroupPrice objects $this->model->setGroupPrices($this->product, $gps); // test the data actually set on the product $gpArray = $this->product->getData($this::KEY_GROUP_PRICE); $this->assertNotNull($gpArray); $this->assertTrue(is_array($gpArray)); $this->assertEquals(sizeof($gps), sizeof($gpArray)); for ($i = 0; $i < sizeof($gps); $i++) { $gpData = $gpArray[$i]; $this->assertEquals($expectedWebsiteId, $gpData['website_id'], 'Website Id does not match'); $this->assertEquals($gps[$i]->getValue(), $gpData['price'], 'Price/Value does not match'); $this->assertEquals($gps[$i]->getValue(), $gpData['website_price'], 'WebsitePrice/Value does not match'); $this->assertEquals($gps[$i]->getCustomerGroupId(), $gpData['cust_group'], 'Customer group Id does not match'); } // test with the data retrieved as a REST object $gpRests = $this->model->getGroupPrices($this->product); $this->assertNotNull($gpRests); $this->assertTrue(is_array($gpRests)); $this->assertEquals(sizeof($gps), sizeof($gpRests)); for ($i = 0; $i < sizeof($gps); $i++) { $this->assertEquals($gps[$i]->getValue(), $gpRests[$i]->getValue(), 'REST: Price/Value does not match'); $this->assertEquals($gps[$i]->getCustomerGroupId(), $gpRests[$i]->getCustomerGroupId(), 'REST: Customer group Id does not match'); } }