/** * {@inheritdoc} * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function set($productSku, \Magento\Catalog\Service\V1\Data\Product\GroupPrice $price) { $customerGroup = $this->customerGroupService->getGroup($price->getCustomerGroupId()); $product = $this->productRepository->get($productSku, true); $groupPrices = $product->getData('group_price'); $websiteId = 0; if ($this->config->getValue('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE) != 0) { $websiteId = $this->storeManager->getWebsite()->getId(); } $found = false; foreach ($groupPrices as &$currentPrice) { if (intval($currentPrice['cust_group']) === $price->getCustomerGroupId() && intval($currentPrice['website_id']) === intval($websiteId)) { $currentPrice['price'] = $price->getValue(); $found = true; break; } } if (!$found) { $groupPrices[] = array('cust_group' => $customerGroup->getId(), 'website_id' => $websiteId, 'price' => $price->getValue()); } $product->setData('group_price', $groupPrices); $errors = $product->validate(); if (is_array($errors) && count($errors)) { $errorAttributeCodes = implode(', ', array_keys($errors)); throw new InputException(sprintf('Values of following attributes are invalid: %s', $errorAttributeCodes)); } try { $product->save(); } catch (\Exception $e) { throw new CouldNotSaveException('Could not save group price'); } return true; }
/** * @param int $groupId * @return \Magento\Customer\Service\V1\Data\CustomerGroup|null */ private function getGroup($groupId) { try { $group = $this->_groupService->getGroup($groupId); } catch (NoSuchEntityException $e) { $group = null; } return $group; }
/** * Return name of the customer group. * * @return string */ public function getCustomerGroupName() { if ($this->getOrder()) { $customerGroupId = $this->getOrder()->getCustomerGroupId(); if (!is_null($customerGroupId)) { return $this->_groupService->getGroup($customerGroupId)->getCode(); } } return ''; }
/** * Retrieve the header text, either editing an existing group or creating a new one. * * @return string */ public function getHeaderText() { $groupId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_GROUP_ID); if (is_null($groupId)) { return __('New Customer Group'); } else { $group = $this->_groupService->getGroup($groupId); return __('Edit Customer Group "%1"', $this->escapeHtml($group->getCode())); } }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function set($productSku, $customerGroupId, \Magento\Catalog\Service\V1\Data\Product\TierPrice $price) { $product = $this->productRepository->get($productSku, true); $customerGroup = $this->customerGroupService->getGroup($customerGroupId); $tierPrices = $product->getData('tier_price'); $websiteId = 0; if ($this->config->getValue('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE) != 0) { $websiteId = $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'] == $websiteId && $item['price_qty'] == $price->getQty()) { $item['price'] = $price->getValue(); $found = true; break; } } if (!$found) { $mappedCustomerGroupId = 'all' == $customerGroupId ? \Magento\Customer\Service\V1\CustomerGroupServiceInterface::CUST_GROUP_ALL : $customerGroup->getId(); $tierPrices[] = array('cust_group' => $mappedCustomerGroupId, 'price' => $price->getValue(), 'website_price' => $price->getValue(), 'website_id' => $websiteId, 'price_qty' => $price->getQty()); } $product->setData('tier_price', $tierPrices); $errors = $product->validate(); if (is_array($errors) && count($errors)) { $errorAttributeCodes = implode(', ', array_keys($errors)); throw new InputException(sprintf('Values of following attributes are invalid: %s', $errorAttributeCodes)); } try { $product->save(); } catch (\Exception $e) { throw new CouldNotSaveException('Could not save group price'); } return true; }
/** * Prepare form for render * * @return void */ protected function _prepareLayout() { parent::_prepareLayout(); /** @var \Magento\Framework\Data\Form $form */ $form = $this->_formFactory->create(); $groupId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_GROUP_ID); /** @var \Magento\Customer\Service\V1\Data\CustomerGroup $customerGroup */ if (is_null($groupId)) { $customerGroup = $this->_groupBuilder->create(); } else { $customerGroup = $this->_groupService->getGroup($groupId); } $fieldset = $form->addFieldset('base_fieldset', array('legend' => __('Group Information'))); $validateClass = sprintf('required-entry validate-length maximum-length-%d', \Magento\Customer\Service\V1\CustomerGroupServiceInterface::GROUP_CODE_MAX_LENGTH); $name = $fieldset->addField('customer_group_code', 'text', array('name' => 'code', 'label' => __('Group Name'), 'title' => __('Group Name'), 'note' => __('Maximum length must be less then %1 symbols', \Magento\Customer\Service\V1\CustomerGroupServiceInterface::GROUP_CODE_MAX_LENGTH), 'class' => $validateClass, 'required' => true)); if ($customerGroup->getId() == 0 && $customerGroup->getCode()) { $name->setDisabled(true); } $fieldset->addField('tax_class_id', 'select', array('name' => 'tax_class', 'label' => __('Tax Class'), 'title' => __('Tax Class'), 'class' => 'required-entry', 'required' => true, 'values' => $this->_taxCustomer->toOptionArray(false))); if (!is_null($customerGroup->getId())) { // If edit add id $form->addField('id', 'hidden', array('name' => 'id', 'value' => $customerGroup->getId())); } if ($this->_backendSession->getCustomerGroupData()) { $form->addValues($this->_backendSession->getCustomerGroupData()); $this->_backendSession->setCustomerGroupData(null); } else { // TODO: need to figure out how the DATA can work with forms $form->addValues(array('id' => $customerGroup->getId(), 'customer_group_code' => $customerGroup->getCode(), 'tax_class_id' => $customerGroup->getTaxClassId())); } $form->setUseContainer(true); $form->setId('edit_form'); $form->setAction($this->getUrl('customer/*/save')); $form->setMethod('post'); $this->setForm($form); }
public function testUpdateGroup() { $builder = $this->_objectManager->create('\\Magento\\Customer\\Service\\V1\\Data\\CustomerGroupBuilder'); $group = $builder->setId(null)->setCode('New Group')->setTaxClassId(3)->create(); $groupId = $this->_groupService->saveGroup($group); $this->assertNotNull($groupId); $newGroup = $this->_groupService->getGroup($groupId); $this->assertEquals($groupId, $newGroup->getId()); $this->assertEquals($group->getCode(), $newGroup->getCode()); $this->assertEquals($group->getTaxClassId(), $newGroup->getTaxClassId()); $updates = $builder->setId($groupId)->setCode('Updated Group')->setTaxClassId(3)->create(); $newId = $this->_groupService->saveGroup($updates); $this->assertEquals($newId, $groupId); $updatedGroup = $this->_groupService->getGroup($groupId); $this->assertEquals($updates->getCode(), $updatedGroup->getCode()); $this->assertEquals($updates->getTaxClassId(), $updatedGroup->getTaxClassId()); }
/** * Get customer tax class ID. * * @return string */ public function getCustomerTaxClassId() { /** * tax class can vary at any time. so instead of using the value from session, * we need to retrieve from db every time to get the correct tax class */ //if (!$this->getData('customer_group_id') && !$this->getData('customer_tax_class_id')) { $groupId = $this->getCustomerGroupId(); if (!is_null($groupId)) { $taxClassId = $this->_customerGroupService->getGroup($this->getCustomerGroupId())->getTaxClassId(); $this->setCustomerTaxClassId($taxClassId); } return $this->getData('customer_tax_class_id'); }
/** * @magentoDataFixture Magento/Customer/_files/customer.php */ public function testGetGroupName() { $groupName = $this->_groupService->getGroup($this->_loadCustomer()->getGroupId())->getCode(); $this->assertEquals($groupName, $this->_block->getGroupName()); }
/** * Add account data to quote * * @param array $accountData * @return $this */ public function setAccountData($accountData) { $customer = $this->getQuote()->getCustomerData(); $form = $this->_createCustomerForm($customer); // emulate request $request = $form->prepareRequest($accountData); $data = $form->extractData($request); $data = $form->restoreData($data); $customer = $this->_customerBuilder->mergeDataObjectWithArray($customer, $data); $this->getQuote()->updateCustomerData($customer); $data = array(); $customerData = \Magento\Framework\Service\ExtensibleDataObjectConverter::toFlatArray($customer); foreach ($form->getAttributes() as $attribute) { $code = sprintf('customer_%s', $attribute->getAttributeCode()); $data[$code] = isset($customerData[$attribute->getAttributeCode()]) ? $customerData[$attribute->getAttributeCode()] : null; } if (isset($data['customer_group_id'])) { $customerGroup = $this->_customerGroupService->getGroup($data['customer_group_id']); $data['customer_tax_class_id'] = $customerGroup->getTaxClassId(); $this->setRecollect(true); } $this->getQuote()->addData($data); return $this; }
/** * Get request object with information necessary for getting tax rate * * Request object contain: * country_id (->getCountryId()) * region_id (->getRegionId()) * postcode (->getPostcode()) * customer_class_id (->getCustomerClassId()) * store (->getStore()) * * @param null|bool|\Magento\Framework\Object|\Magento\Customer\Service\V1\Data\Address $shippingAddress * @param null|bool|\Magento\Framework\Object|\Magento\Customer\Service\V1\Data\Address $billingAddress * @param null|int $customerTaxClass * @param null|int|\Magento\Store\Model\Store $store * @param int $customerId * @return \Magento\Framework\Object */ public function getRateRequest($shippingAddress = null, $billingAddress = null, $customerTaxClass = null, $store = null, $customerId = null) { if ($shippingAddress === false && $billingAddress === false && $customerTaxClass === false) { return $this->getRateOriginRequest($store); } $address = new \Magento\Framework\Object(); $basedOn = $this->_scopeConfig->getValue(\Magento\Tax\Model\Config::CONFIG_XML_PATH_BASED_ON, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store); if ($shippingAddress === false && $basedOn == 'shipping' || $billingAddress === false && $basedOn == 'billing') { $basedOn = 'default'; } else { if ((is_null($billingAddress) || !$billingAddress->getCountryId()) && $basedOn == 'billing' || (is_null($shippingAddress) || !$shippingAddress->getCountryId()) && $basedOn == 'shipping') { if ($customerId) { try { $defaultBilling = $this->_addressService->getDefaultBillingAddress($customerId); } catch (NoSuchEntityException $e) { } try { $defaultShipping = $this->_addressService->getDefaultShippingAddress($customerId); } catch (NoSuchEntityException $e) { } if ($basedOn == 'billing' && isset($defaultBilling) && $defaultBilling->getCountryId()) { $billingAddress = $defaultBilling; } elseif ($basedOn == 'shipping' && isset($defaultShipping) && $defaultShipping->getCountryId()) { $shippingAddress = $defaultShipping; } else { $basedOn = 'default'; } } else { $basedOn = 'default'; } } } switch ($basedOn) { case 'billing': $address = $billingAddress; break; case 'shipping': $address = $shippingAddress; break; case 'origin': $address = $this->getRateOriginRequest($store); break; case 'default': $address->setCountryId($this->_scopeConfig->getValue(\Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store))->setRegionId($this->_scopeConfig->getValue(\Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store))->setPostcode($this->_scopeConfig->getValue(\Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_POSTCODE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store)); break; default: break; } if (is_null($customerTaxClass) || $customerTaxClass === false) { if ($customerId) { $customerData = $this->customerAccountService->getCustomer($customerId); $customerTaxClass = $this->_groupService->getGroup($customerData->getGroupId())->getTaxClassId(); } else { $customerTaxClass = $this->_groupService->getGroup(GroupServiceInterface::NOT_LOGGED_IN_ID)->getTaxClassId(); } } $request = new \Magento\Framework\Object(); //TODO: Address is not completely refactored to use Data objects if ($address->getRegion() instanceof RegionDataObject) { $regionId = $address->getRegion()->getRegionId(); } else { $regionId = $address->getRegionId(); } $request->setCountryId($address->getCountryId())->setRegionId($regionId)->setPostcode($address->getPostcode())->setStore($store)->setCustomerClassId($customerTaxClass); return $request; }
/** * Retrieve customer tax class identifier * * @return int */ public function getTaxClassId() { if (!$this->getData('tax_class_id')) { $groupTaxClassId = $this->_groupService->getGroup($this->getGroupId())->getTaxClassId(); $this->setData('tax_class_id', $groupTaxClassId); } return $this->getData('tax_class_id'); }