Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
TierPriceManagement Class Reference
Inheritance diagram for TierPriceManagement:
ProductTierPriceManagementInterface

Public Member Functions

 __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)
 
 add ($sku, $customerGroupId, $price, $qty)
 
 remove ($sku, $customerGroupId, $qty)
 
- Public Member Functions inherited from ProductTierPriceManagementInterface
 getList ($sku, $customerGroupId)
 

Protected Attributes

 $productRepository
 
 $priceFactory
 
 $storeManager
 
 $priceModifier
 
 $config
 
 $groupManagement
 
 $groupRepository
 

Detailed Description

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 20 of file TierPriceManagement.php.

Constructor & Destructor Documentation

◆ __construct()

__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 
)
Parameters
ProductRepositoryInterface$productRepository
\Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory$priceFactory
\Magento\Store\Model\StoreManagerInterface$storeManager
PriceModifier$priceModifier
\Magento\Framework\App\Config\ScopeConfigInterface$config
GroupManagementInterface$groupManagement
GroupRepositoryInterface$groupRepository

Definition at line 66 of file TierPriceManagement.php.

Member Function Documentation

◆ add()

add (   $sku,
  $customerGroupId,
  $price,
  $qty 
)

{Create tier price for product

Parameters
string$sku
string$customerGroupId'all' can be used to specify 'ALL GROUPS'
float$price
float$qty
Returns
boolean
Exceptions
} @SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

Implements ProductTierPriceManagementInterface.

Definition at line 89 of file TierPriceManagement.php.

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  }
__()
Definition: __.php:13
$price
$value
Definition: gender.phtml:16
static is($value, $classBaseName, array $args=array(), $namespaces=array())
Definition: Validate.php:195
$errors
Definition: overview.phtml:9

◆ remove()

remove (   $sku,
  $customerGroupId,
  $qty 
)

{Remove tier price from product

Parameters
string$sku
string$customerGroupId'all' can be used to specify 'ALL GROUPS'
float$qty
Returns
boolean
Exceptions
}

Implements ProductTierPriceManagementInterface.

Definition at line 153 of file TierPriceManagement.php.

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  }
$value
Definition: gender.phtml:16

Field Documentation

◆ $config

$config
protected

Definition at line 45 of file TierPriceManagement.php.

◆ $groupManagement

$groupManagement
protected

Definition at line 50 of file TierPriceManagement.php.

◆ $groupRepository

$groupRepository
protected

Definition at line 55 of file TierPriceManagement.php.

◆ $priceFactory

$priceFactory
protected

Definition at line 30 of file TierPriceManagement.php.

◆ $priceModifier

$priceModifier
protected

Definition at line 40 of file TierPriceManagement.php.

◆ $productRepository

$productRepository
protected

Definition at line 25 of file TierPriceManagement.php.

◆ $storeManager

$storeManager
protected

Definition at line 35 of file TierPriceManagement.php.


The documentation for this class was generated from the following file: