Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RateQuery.php
Go to the documentation of this file.
1 <?php
8 
9 class RateQuery
10 {
14  private $request;
15 
20  public function __construct(
21  \Magento\Quote\Model\Quote\Address\RateRequest $request
22  ) {
23  $this->request = $request;
24  }
25 
30  public function prepareSelect(\Magento\Framework\DB\Select $select)
31  {
32  $select->where(
33  'website_id = :website_id'
34  )->order(
35  ['dest_country_id DESC', 'dest_region_id DESC', 'dest_zip DESC', 'condition_value DESC']
36  )->limit(
37  1
38  );
39 
40  // Render destination condition
41  $orWhere = '(' . implode(
42  ') OR (',
43  [
44  "dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = :postcode",
45  "dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = ''",
46 
47  // Handle asterisk in dest_zip field
48  "dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = '*'",
49  "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = '*'",
50  "dest_country_id = '0' AND dest_region_id = :region_id AND dest_zip = '*'",
51  "dest_country_id = '0' AND dest_region_id = 0 AND dest_zip = '*'",
52  "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = ''",
53  "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = :postcode",
54  "dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = '*'"
55  ]
56  ) . ')';
57  $select->where($orWhere);
58 
59  // Render condition by condition name
60  if (is_array($this->request->getConditionName())) {
61  $orWhere = [];
62  foreach (range(0, count($this->request->getConditionName())) as $conditionNumber) {
63  $bindNameKey = sprintf(':condition_name_%d', $conditionNumber);
64  $bindValueKey = sprintf(':condition_value_%d', $conditionNumber);
65  $orWhere[] = "(condition_name = {$bindNameKey} AND condition_value <= {$bindValueKey})";
66  }
67 
68  if ($orWhere) {
69  $select->where(implode(' OR ', $orWhere));
70  }
71  } else {
72  $select->where('condition_name = :condition_name');
73  $select->where('condition_value <= :condition_value');
74  }
75  return $select;
76  }
77 
81  public function getBindings()
82  {
83  $bind = [
84  ':website_id' => (int)$this->request->getWebsiteId(),
85  ':country_id' => $this->request->getDestCountryId(),
86  ':region_id' => (int)$this->request->getDestRegionId(),
87  ':postcode' => $this->request->getDestPostcode(),
88  ];
89 
90  // Render condition by condition name
91  if (is_array($this->request->getConditionName())) {
92  $i = 0;
93  foreach ($this->request->getConditionName() as $conditionName) {
94  $bindNameKey = sprintf(':condition_name_%d', $i);
95  $bindValueKey = sprintf(':condition_value_%d', $i);
96  $bind[$bindNameKey] = $conditionName;
97  $bind[$bindValueKey] = $this->request->getData($conditionName);
98  $i++;
99  }
100  } else {
101  $bind[':condition_name'] = $this->request->getConditionName();
102  $bind[':condition_value'] = round($this->request->getData($this->request->getConditionName()), 4);
103  }
104 
105  return $bind;
106  }
107 
111  public function getRequest()
112  {
113  return $this->request;
114  }
115 }
__construct(\Magento\Quote\Model\Quote\Address\RateRequest $request)
Definition: RateQuery.php:20
$i
Definition: gallery.phtml:31