Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CartItem.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Mtf\Client\Locator;
10 
16 {
22  protected $edit = '.action-edit';
23 
29  protected $removeItem = '.action-delete';
30 
36  protected $bundleOptions = './/dl[contains(@class, "item-options")]/dd[%d]/span[@class="price"][%d]';
37 
43  protected $wishlistButton = '.towishlist';
44 
50  protected $name = '.product-item-name a';
51 
57  public function getProductName()
58  {
59  return $this->_rootElement->find($this->productName)->getText();
60  }
61 
67  public function getPrice()
68  {
69  $cartProductPrice = $this->_rootElement->find($this->price, Locator::SELECTOR_XPATH);
70  return $cartProductPrice->isVisible()
71  ? str_replace(',', '', $this->escapeCurrency($cartProductPrice->getText()))
72  : null;
73  }
74 
80  public function getPriceInclTax()
81  {
82  $cartProductPrice = $this->_rootElement->find($this->priceInclTax, Locator::SELECTOR_XPATH);
83  return $cartProductPrice->isVisible()
84  ? str_replace(',', '', $this->escapeCurrency($cartProductPrice->getText()))
85  : null;
86  }
87 
93  public function getPriceExclTax()
94  {
95  $cartProductPrice = $this->_rootElement->find($this->priceExclTax, Locator::SELECTOR_XPATH);
96  return $cartProductPrice->isVisible()
97  ? str_replace(',', '', $this->escapeCurrency($cartProductPrice->getText()))
98  : null;
99  }
100 
107  public function setQty($qty)
108  {
109  $this->_rootElement->find($this->qty, Locator::SELECTOR_XPATH)->setValue($qty);
110  }
111 
117  public function getQty()
118  {
119  return $this->_rootElement->find($this->qty, Locator::SELECTOR_XPATH)->getValue();
120  }
121 
127  public function getSubtotalPrice()
128  {
129  $cartProductPrice = $this->_rootElement->find($this->subtotalPrice);
130  return $cartProductPrice->isVisible()
131  ? str_replace(',', '', $this->escapeCurrency($cartProductPrice->getText()))
132  : null;
133  }
134 
140  public function getSubtotalPriceExclTax()
141  {
142  $cartProductPrice = $this->_rootElement->find($this->subTotalPriceExclTax);
143  return $cartProductPrice->isVisible()
144  ? str_replace(',', '', $this->escapeCurrency($cartProductPrice->getText()))
145  : null;
146  }
147 
153  public function getSubtotalPriceInclTax()
154  {
155  $cartProductPrice = $this->_rootElement->find($this->subTotalPriceInclTax);
156  return $cartProductPrice->isVisible()
157  ? str_replace(',', '', $this->escapeCurrency($cartProductPrice->getText()))
158  : null;
159  }
160 
166  public function getOptions()
167  {
168  $optionsBlock = $this->_rootElement->find($this->optionsBlock, Locator::SELECTOR_XPATH);
169  $options = [];
170 
171  if ($optionsBlock->isVisible()) {
172  $titles = $optionsBlock->getElements('./dt', Locator::SELECTOR_XPATH);
173  $values = $optionsBlock->getElements('./dd', Locator::SELECTOR_XPATH);
174 
175  foreach ($titles as $key => $title) {
176  $value = $values[$key]->getText();
177  $options[] = [
178  'title' => $title->getText(),
179  'value' => $this->escapeCurrencyForOption($value),
180  ];
181  }
182  }
183 
184  return $options;
185  }
186 
192  public function getOptionsName()
193  {
194  $optionsName = $this->_rootElement->find($this->optionsBlock . '//dt', Locator::SELECTOR_XPATH);
195  if (!$optionsName->isVisible()) {
196  return '';
197  }
198  return $optionsName->getText();
199  }
200 
206  public function getOptionsValue()
207  {
208  $optionsValue = $this->_rootElement->find($this->optionsBlock . '//dd', Locator::SELECTOR_XPATH);
209  if (!$optionsValue->isVisible()) {
210  return '';
211  }
212  return $optionsValue->getText();
213  }
214 
223  public function getPriceBundleOptions($index, $itemIndex = 1, $currency = '$')
224  {
225  $formatPrice = sprintf($this->bundleOptions, $index, $itemIndex);
226  return trim($this->_rootElement->find($formatPrice, Locator::SELECTOR_XPATH)->getText(), $currency);
227  }
228 
234  public function getName()
235  {
236  return $this->_rootElement->find($this->name, Locator::SELECTOR_CSS)->getText();
237  }
238 
244  public function edit()
245  {
246  $this->_rootElement->find($this->edit)->click();
247  }
248 
254  public function removeItem()
255  {
256  $this->_rootElement->find($this->removeItem)->click();
257  }
258 
265  protected function escapeCurrencyForOption($label)
266  {
267  return preg_replace('/^(\d+) x (\w+) \W([\d\.,]+)$/', '$1 x $2 $3', $label);
268  }
269 
275  public function moveToWishlist()
276  {
277  $this->_rootElement->find($this->wishlistButton)->click();
278  }
279 
285  public function isEditButtonVisible()
286  {
287  return $this->_rootElement->find($this->edit)->isVisible();
288  }
289 }
$title
Definition: default.phtml:14
$values
Definition: options.phtml:88
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
getPriceBundleOptions($index, $itemIndex=1, $currency='$')
Definition: CartItem.php:223
$index
Definition: list.phtml:44