Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TierPriceManagement.php
Go to the documentation of this file.
1 <?php
9 
16 
21 {
25  protected $productRepository;
26 
30  protected $priceFactory;
31 
35  protected $storeManager;
36 
40  protected $priceModifier;
41 
45  protected $config;
46 
50  protected $groupManagement;
51 
55  protected $groupRepository;
56 
66  public function __construct(
68  \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $priceFactory,
74  ) {
75  $this->productRepository = $productRepository;
76  $this->priceFactory = $priceFactory;
77  $this->storeManager = $storeManager;
78  $this->priceModifier = $priceModifier;
79  $this->config = $config;
80  $this->groupManagement = $groupManagement;
81  $this->groupRepository = $groupRepository;
82  }
83 
89  public function add($sku, $customerGroupId, $price, $qty)
90  {
91  if (!\Zend_Validate::is($price, 'Float') || $price <= 0 || !\Zend_Validate::is($qty, 'Float') || $qty <= 0) {
92  throw new InputException(__('The data was invalid. Verify the data and try again.'));
93  }
94  $product = $this->productRepository->get($sku, ['edit_mode' => true]);
95  $tierPrices = $product->getData('tier_price');
96  $websiteIdentifier = 0;
97  $value = $this->config->getValue('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE);
98  if ($value != 0) {
99  $websiteIdentifier = $this->storeManager->getWebsite()->getId();
100  }
101  $found = false;
102 
103  foreach ($tierPrices as &$item) {
104  if ('all' == $customerGroupId) {
105  $isGroupValid = ($item['all_groups'] == 1);
106  } else {
107  $isGroupValid = ($item['cust_group'] == $customerGroupId);
108  }
109 
110  if ($isGroupValid && $item['website_id'] == $websiteIdentifier && $item['price_qty'] == $qty) {
111  $item['price'] = $price;
112  $found = true;
113  break;
114  }
115  }
116  if (!$found) {
117  $mappedCustomerGroupId = 'all' == $customerGroupId
118  ? $this->groupManagement->getAllCustomersGroup()->getId()
119  : $this->groupRepository->getById($customerGroupId)->getId();
120 
121  $tierPrices[] = [
122  'cust_group' => $mappedCustomerGroupId,
123  'price' => $price,
124  'website_price' => $price,
125  'website_id' => $websiteIdentifier,
126  'price_qty' => $qty,
127  ];
128  }
129 
130  $product->setData('tier_price', $tierPrices);
131  $errors = $product->validate();
132  if (is_array($errors) && count($errors)) {
133  $errorAttributeCodes = implode(', ', array_keys($errors));
134  throw new InputException(
135  __('Values in the %1 attributes are invalid. Verify the values and try again.', $errorAttributeCodes)
136  );
137  }
138  try {
139  $this->productRepository->save($product);
140  } catch (\Exception $e) {
141  if ($e instanceof TemporaryStateExceptionInterface) {
142  // temporary state exception must be already localized
143  throw $e;
144  }
145  throw new CouldNotSaveException(__("The group price couldn't be saved."));
146  }
147  return true;
148  }
149 
153  public function remove($sku, $customerGroupId, $qty)
154  {
155  $product = $this->productRepository->get($sku, ['edit_mode' => true]);
156  $websiteIdentifier = 0;
157  $value = $this->config->getValue('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE);
158  if ($value != 0) {
159  $websiteIdentifier = $this->storeManager->getWebsite()->getId();
160  }
161  $this->priceModifier->removeTierPrice($product, $customerGroupId, $qty, $websiteIdentifier);
162  return true;
163  }
164 
168  public function getList($sku, $customerGroupId)
169  {
170  $product = $this->productRepository->get($sku, ['edit_mode' => true]);
171 
172  $priceKey = 'website_price';
173  $value = $this->config->getValue('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE);
174  if ($value == 0) {
175  $priceKey = 'price';
176  }
177 
178  $cgi = ($customerGroupId === 'all'
179  ? $this->groupManagement->getAllCustomersGroup()->getId()
180  : $customerGroupId);
181 
182  $prices = [];
183  foreach ($product->getData('tier_price') as $price) {
184  if ((is_numeric($customerGroupId) && intval($price['cust_group']) === intval($customerGroupId))
185  || ($customerGroupId === 'all' && $price['all_groups'])
186  ) {
188  $tierPrice = $this->priceFactory->create();
189  $tierPrice->setValue($price[$priceKey])
190  ->setQty($price['price_qty'])
191  ->setCustomerGroupId($cgi);
192  $prices[] = $tierPrice;
193  }
194  }
195  return $prices;
196  }
197 }
__()
Definition: __.php:13
$price
$value
Definition: gender.phtml:16
add($sku, $customerGroupId, $price, $qty)
static is($value, $classBaseName, array $args=array(), $namespaces=array())
Definition: Validate.php:195
__construct(ProductRepositoryInterface $productRepository, \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $priceFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\Product\PriceModifier $priceModifier, \Magento\Framework\App\Config\ScopeConfigInterface $config, GroupManagementInterface $groupManagement, GroupRepositoryInterface $groupRepository)
$errors
Definition: overview.phtml:9