/** * Getter for customer group id, return default group if not set * * @return int */ public function getCustomerGroupId() { if ($this->customerGroupId === null) { return $this->groupManagement->getAllCustomersGroup()->getId(); } return parent::getCustomerGroupId(); }
/** * @magentoDataFixture Magento/Catalog/Model/Resource/_files/product_simple.php */ public function testAddTierPriceData() { $this->_collection->setFlag('tier_price_added', false); $this->_collection->addIdFilter(2); $this->assertInstanceOf('\\Magento\\Catalog\\Model\\Resource\\Product\\Collection', $this->_collection->addTierPriceData()); $tierPrice = $this->_collection->getFirstItem()->getDataByKey('tier_price'); $this->assertEquals($this->_groupManagement->getNotLoggedInGroup()->getId(), current($tierPrice)['cust_group']); $this->assertEquals($this->_groupManagement->getAllCustomersGroup()->getId(), next($tierPrice)['cust_group']); $this->assertTrue($this->_collection->getFlag('tier_price_added')); }
/** * Render block HTML * * @return string */ public function _toHtml() { if (!$this->getOptions()) { if ($this->_addGroupAllOption) { $this->addOption($this->groupManagement->getAllCustomersGroup()->getId(), __('ALL GROUPS')); } foreach ($this->_getCustomerGroups() as $groupId => $groupLabel) { $this->addOption($groupId, addslashes($groupLabel)); } } return parent::_toHtml(); }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function add($sku, $customerGroupId, $price, $qty) { if (!\Zend_Validate::is($price, 'Float') || $price <= 0 || !\Zend_Validate::is($qty, 'Float') || $qty <= 0) { throw new InputException(__('Please provide valid data')); } $product = $this->productRepository->get($sku, ['edit_mode' => true]); $tierPrices = $product->getData('tier_price'); $websiteIdentifier = 0; $value = $this->config->getValue('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE); if ($value != 0) { $websiteIdentifier = $this->storeManager->getWebsite()->getId(); } $found = false; foreach ($tierPrices as &$item) { if ('all' == $customerGroupId) { $isGroupValid = $item['all_groups'] == 1; } else { $isGroupValid = $item['cust_group'] == $customerGroupId; } if ($isGroupValid && $item['website_id'] == $websiteIdentifier && $item['price_qty'] == $qty) { $item['price'] = $price; $found = true; break; } } if (!$found) { $mappedCustomerGroupId = 'all' == $customerGroupId ? $this->groupManagement->getAllCustomersGroup()->getId() : $this->groupRepository->getById($customerGroupId)->getId(); $tierPrices[] = ['cust_group' => $mappedCustomerGroupId, 'price' => $price, 'website_price' => $price, 'website_id' => $websiteIdentifier, 'price_qty' => $qty]; } $product->setData('tier_price', $tierPrices); $errors = $product->validate(); if (is_array($errors) && count($errors)) { $errorAttributeCodes = implode(', ', array_keys($errors)); throw new InputException(__('Values of following attributes are invalid: %1', $errorAttributeCodes)); } try { $this->productRepository->save($product); } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not save group price')); } return true; }
/** * {@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'; } $cgi = $customerGroupId === 'all' ? $this->groupManagement->getAllCustomersGroup()->getId() : $customerGroupId; $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'])->setCustomerGroupId($cgi); $prices[] = $tierPrice; } } return $prices; }
/** * Can apply tier price * * @param array $currentTierPrice * @param int $prevPriceGroup * @param float|string $prevQty * @return bool */ protected function canApplyTierPrice(array $currentTierPrice, $prevPriceGroup, $prevQty) { $custGroupAllId = (int) $this->groupManagement->getAllCustomersGroup()->getId(); // Tier price can be applied, if: // tier price is for current customer group or is for all groups if ((int) $currentTierPrice['cust_group'] !== $this->customerGroup && (int) $currentTierPrice['cust_group'] !== $custGroupAllId) { return false; } // and tier qty is lower than product qty if ($this->quantity < $currentTierPrice['price_qty']) { return false; } // and tier qty is bigger than previous qty if ($currentTierPrice['price_qty'] < $prevQty) { return false; } // and found tier qty is same as previous tier qty, but current tier group isn't ALL_GROUPS if ($currentTierPrice['price_qty'] == $prevQty && $prevPriceGroup !== $custGroupAllId && $currentTierPrice['cust_group'] === $custGroupAllId) { return false; } return true; }
/** * Add tier price data to loaded items * * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function addTierPriceData() { if ($this->getFlag('tier_price_added')) { return $this; } $tierPrices = []; $productIds = []; foreach ($this->getItems() as $item) { $productIds[] = $item->getId(); $tierPrices[$item->getId()] = []; } if (!$productIds) { return $this; } /** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */ $attribute = $this->getAttribute('tier_price'); $websiteId = 0; if (!$attribute->isScopeGlobal() && null !== $this->getStoreId()) { $websiteId = $this->_storeManager->getStore($this->getStoreId())->getWebsiteId(); } $linkField = $this->getConnection()->getAutoIncrementField($this->getTable('catalog_product_entity')); $connection = $this->getConnection(); $columns = ['price_id' => 'value_id', 'website_id' => 'website_id', 'all_groups' => 'all_groups', 'cust_group' => 'customer_group_id', 'price_qty' => 'qty', 'price' => 'value', 'product_id' => $linkField]; $select = $connection->select()->from($this->getTable('catalog_product_entity_tier_price'), $columns)->where($linkField . ' IN(?)', $productIds)->order([$linkField, 'qty']); if ($websiteId == 0) { $select->where('website_id = ?', $websiteId); } else { $select->where('website_id IN(?)', [0, $websiteId]); } foreach ($connection->fetchAll($select) as $row) { $tierPrices[$row['product_id']][] = ['website_id' => $row['website_id'], 'cust_group' => $row['all_groups'] ? $this->_groupManagement->getAllCustomersGroup()->getId() : $row['cust_group'], 'price_qty' => $row['price_qty'], 'price' => $row['price'], 'website_price' => $row['price']]; } /* @var $backend \Magento\Catalog\Model\Product\Attribute\Backend\Tierprice */ $backend = $attribute->getBackend(); foreach ($this->getItems() as $item) { $data = $tierPrices[$item->getId()]; if (!empty($data) && $websiteId) { $data = $backend->preparePriceData($data, $item->getTypeId(), $websiteId); } $item->setData('tier_price', $data); } $this->setFlag('tier_price_added', true); return $this; }
/** * Get product tier price by qty * * @param float $qty * @param Product $product * @return float|array * @deprecated (MAGETWO-31465) * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function getTierPrice($qty, $product) { $allGroups = $this->_groupManagement->getAllCustomersGroup()->getId(); $prices = $product->getData('tier_price'); if ($prices === null) { $attribute = $product->getResource()->getAttribute('tier_price'); if ($attribute) { $attribute->getBackend()->afterLoad($product); $prices = $product->getData('tier_price'); } } if ($prices === null || !is_array($prices)) { if ($qty !== null) { return $product->getPrice(); } return [['price' => $product->getPrice(), 'website_price' => $product->getPrice(), 'price_qty' => 1, 'cust_group' => $allGroups]]; } $custGroup = $this->_getCustomerGroupId($product); if ($qty) { $prevQty = 1; $prevPrice = $product->getPrice(); $prevGroup = $allGroups; foreach ($prices as $price) { if ($price['cust_group'] != $custGroup && $price['cust_group'] != $allGroups) { // tier not for current customer group nor is for all groups continue; } if ($qty < $price['price_qty']) { // tier is higher than product qty continue; } if ($price['price_qty'] < $prevQty) { // higher tier qty already found continue; } if ($price['price_qty'] == $prevQty && $prevGroup != $allGroups && $price['cust_group'] == $allGroups) { // found tier qty is same as current tier qty but current tier group is ALL_GROUPS continue; } if ($price['website_price'] < $prevPrice) { $prevPrice = $price['website_price']; $prevQty = $price['price_qty']; $prevGroup = $price['cust_group']; } } return $prevPrice; } else { $qtyCache = []; foreach ($prices as $priceKey => $price) { if ($price['cust_group'] != $custGroup && $price['cust_group'] != $allGroups) { unset($prices[$priceKey]); } elseif (isset($qtyCache[$price['price_qty']])) { $priceQty = $qtyCache[$price['price_qty']]; if ($prices[$priceQty]['website_price'] > $price['website_price']) { unset($prices[$priceQty]); $qtyCache[$price['price_qty']] = $priceKey; } else { unset($prices[$priceKey]); } } else { $qtyCache[$price['price_qty']] = $priceKey; } } } return $prices ? $prices : []; }
/** * After Save Attribute manipulation * * @param \Magento\Catalog\Model\Product $object * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function afterSave($object) { $websiteId = $this->_storeManager->getStore($object->getStoreId())->getWebsiteId(); $isGlobal = $this->getAttribute()->isScopeGlobal() || $websiteId == 0; $priceRows = $object->getData($this->getAttribute()->getName()); if ($priceRows === null) { return $this; } $old = []; $new = []; // prepare original data for compare $origPrices = $object->getOrigData($this->getAttribute()->getName()); if (!is_array($origPrices)) { $origPrices = []; } foreach ($origPrices as $data) { if ($data['website_id'] > 0 || $data['website_id'] == '0' && $isGlobal) { $key = join('-', array_merge([$data['website_id'], $data['cust_group']], $this->_getAdditionalUniqueFields($data))); $old[$key] = $data; } } // prepare data for save foreach ($priceRows as $data) { $hasEmptyData = false; foreach ($this->_getAdditionalUniqueFields($data) as $field) { if (empty($field)) { $hasEmptyData = true; break; } } if ($hasEmptyData || !isset($data['cust_group']) || !empty($data['delete'])) { continue; } if ($this->getAttribute()->isScopeGlobal() && $data['website_id'] > 0) { continue; } if (!$isGlobal && (int) $data['website_id'] == 0) { continue; } $key = join('-', array_merge([$data['website_id'], $data['cust_group']], $this->_getAdditionalUniqueFields($data))); $useForAllGroups = $data['cust_group'] == $this->_groupManagement->getAllCustomersGroup()->getId(); $customerGroupId = !$useForAllGroups ? $data['cust_group'] : 0; $new[$key] = array_merge(['website_id' => $data['website_id'], 'all_groups' => $useForAllGroups ? 1 : 0, 'customer_group_id' => $customerGroupId, 'value' => $data['price']], $this->_getAdditionalUniqueFields($data)); } $delete = array_diff_key($old, $new); $insert = array_diff_key($new, $old); $update = array_intersect_key($new, $old); $isChanged = false; $productId = $object->getId(); if (!empty($delete)) { foreach ($delete as $data) { $this->_getResource()->deletePriceData($productId, null, $data['price_id']); $isChanged = true; } } if (!empty($insert)) { foreach ($insert as $data) { $price = new \Magento\Framework\DataObject($data); $price->setEntityId($productId); $this->_getResource()->savePriceData($price); $isChanged = true; } } if (!empty($update)) { foreach ($update as $k => $v) { if ($old[$k]['price'] != $v['value']) { $price = new \Magento\Framework\DataObject(['value_id' => $old[$k]['price_id'], 'value' => $v['value']]); $this->_getResource()->savePriceData($price); $isChanged = true; } } } if ($isChanged) { $valueChangedKey = $this->getAttribute()->getName() . '_changed'; $object->setData($valueChangedKey, 1); } return $this; }
public function testGetAllGroup() { $allGroup = $this->groupManagement->getAllCustomersGroup(); $this->assertEquals(32000, $allGroup->getId()); }
/** * Gets the CUST_GROUP_ALL id * * @return int */ protected function getAllCustomerGroupsId() { // ex: 32000 return $this->_groupManagement->getAllCustomersGroup()->getId(); }
/** * Return the all customer group id * * @return int */ protected function getAllCustomersGroupId() { return $this->groupManagement->getAllCustomersGroup()->getId(); }
/** * Retrieve default value for customer group * * @return int */ public function getDefaultCustomerGroup() { return $this->_groupManagement->getAllCustomersGroup()->getId(); }