Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TierPriceFactory.php
Go to the documentation of this file.
1 <?php
8 
11 
16 {
22  private $tierPriceFactory;
23 
29  private $tierPricePersistence;
30 
36  private $customerGroupRepository;
37 
43  private $allGroupsValue = 'all groups';
44 
50  private $allGroupsId = 1;
51 
57  private $customerGroupsByCode = [];
58 
68  public function __construct(
69  \Magento\Catalog\Api\Data\TierPriceInterfaceFactory $tierPriceFactory,
70  TierPricePersistence $tierPricePersistence,
71  \Magento\Customer\Api\GroupRepositoryInterface $customerGroupRepository,
72  \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
73  \Magento\Framework\Api\FilterBuilder $filterBuilder
74  ) {
75  $this->tierPriceFactory = $tierPriceFactory;
76  $this->tierPricePersistence = $tierPricePersistence;
77  $this->customerGroupRepository = $customerGroupRepository;
78  $this->searchCriteriaBuilder = $searchCriteriaBuilder;
79  $this->filterBuilder = $filterBuilder;
80  }
81 
89  public function create(array $rawPrice, $sku)
90  {
91  $price = $this->tierPriceFactory->create();
92  $price->setPrice(isset($rawPrice['percentage_value']) ? $rawPrice['percentage_value'] : $rawPrice['value']);
93  $price->setPriceType(
94  isset($rawPrice['percentage_value'])
95  ? TierPriceInterface::PRICE_TYPE_DISCOUNT
96  : TierPriceInterface::PRICE_TYPE_FIXED
97  );
98  $price->setWebsiteId($rawPrice['website_id']);
99  $price->setSku($sku);
100  $price->setCustomerGroup(
101  $rawPrice['all_groups'] == $this->allGroupsId
102  ? $this->allGroupsValue
103  : $this->customerGroupRepository->getById($rawPrice['customer_group_id'])->getCode()
104  );
105  $price->setQuantity($rawPrice['qty']);
106 
107  return $price;
108  }
109 
118  {
119  return [
120  $this->tierPricePersistence->getEntityLinkField() => $id,
121  'all_groups' => $this->retrievePriceForAllGroupsValue($price),
122  'customer_group_id' => $this->retrievePriceForAllGroupsValue($price) === $this->allGroupsId
123  ? 0
124  : $this->retrieveGroupValue(strtolower($price->getCustomerGroup())),
125  'qty' => $price->getQuantity(),
126  'value' => $price->getPriceType() === TierPriceInterface::PRICE_TYPE_FIXED
127  ? $price->getPrice()
128  : 0.00,
129  'percentage_value' => $price->getPriceType() === TierPriceInterface::PRICE_TYPE_DISCOUNT
130  ? $price->getPrice()
131  : null,
132  'website_id' => $price->getWebsiteId()
133  ];
134  }
135 
142  private function retrievePriceForAllGroupsValue(TierPriceInterface $price)
143  {
144  return strcasecmp($price->getCustomerGroup(), $this->allGroupsValue) === 0 ? $this->allGroupsId : 0;
145  }
146 
154  private function retrieveGroupValue($code)
155  {
156  if (!isset($this->customerGroupsByCode[$code])) {
157  $searchCriteria = $this->searchCriteriaBuilder->addFilters(
158  [
159  $this->filterBuilder->setField('customer_group_code')->setValue($code)->create()
160  ]
161  );
162  $items = $this->customerGroupRepository->getList($searchCriteria->create())->getItems();
163  $item = array_shift($items);
164  $this->customerGroupsByCode[strtolower($item->getCode())] = $item->getId();
165  }
166 
167  return $this->customerGroupsByCode[$code];
168  }
169 }
$id
Definition: fieldset.phtml:14
$price
__construct(\Magento\Catalog\Api\Data\TierPriceInterfaceFactory $tierPriceFactory, TierPricePersistence $tierPricePersistence, \Magento\Customer\Api\GroupRepositoryInterface $customerGroupRepository, \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder, \Magento\Framework\Api\FilterBuilder $filterBuilder)
$searchCriteria
$searchCriteriaBuilder
$code
Definition: info.phtml:12
$items