Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TaxRateManagement.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Tax\Model;
8 
14 
16 {
21 
25  protected $filterBuilder;
26 
30  protected $taxRuleRepository;
31 
35  protected $taxRateRepository;
36 
43  public function __construct(
48  ) {
49  $this->taxRuleRepository = $taxRuleRepository;
50  $this->taxRateRepository = $taxRateRepository;
51  $this->filterBuilder = $filterBuilder;
52  $this->searchCriteriaBuilder = $searchCriteriaBuilder;
53  }
54 
58  public function getRatesByCustomerAndProductTaxClassId($customerTaxClassId, $productTaxClassId)
59  {
60  $this->searchCriteriaBuilder->addFilters(
61  [
62  $this->filterBuilder
63  ->setField('customer_tax_class_ids')
64  ->setValue([$customerTaxClassId])
65  ->create(),
66  ]
67  );
68 
69  $this->searchCriteriaBuilder->addFilters(
70  [
71  $this->filterBuilder
72  ->setField('product_tax_class_ids')
73  ->setValue([$productTaxClassId])
74  ->create(),
75  ]
76  );
77 
78  $searchResults = $this->taxRuleRepository->getList($this->searchCriteriaBuilder->create());
79  $taxRules = $searchResults->getItems();
80  $rates = [];
81  foreach ($taxRules as $taxRule) {
82  $rateIds = $taxRule->getTaxRateIds();
83  if (!empty($rateIds)) {
84  foreach ($rateIds as $rateId) {
85  $rates[] = $this->taxRateRepository->get($rateId);
86  }
87  }
88  }
89  return $rates;
90  }
91 }
$rates
Definition: tax.phtml:35
$taxRule
Definition: tax_rule.php:35
getRatesByCustomerAndProductTaxClassId($customerTaxClassId, $productTaxClassId)
__construct(TaxRuleRepositoryInterface $taxRuleRepository, TaxRateRepositoryInterface $taxRateRepository, FilterBuilder $filterBuilder, SearchCriteriaBuilder $searchCriteriaBuilder)