/**
  * Retrieve url for add product to cart
  * Will return product view page URL if product has required options
  *
  * @param \Magento\Catalog\Model\Product $product
  * @param array $additional
  * @return string
  */
 public function getAddToCartUrl($product, $additional = [])
 {
     if ($product->getTypeInstance()->hasRequiredOptions($product)) {
         if (!isset($additional['_escape'])) {
             $additional['_escape'] = true;
         }
         if (!isset($additional['_query'])) {
             $additional['_query'] = [];
         }
         $additional['_query']['options'] = 'cart';
         return $this->getProductUrl($product, $additional);
     }
     return $this->_cartHelper->getAddUrl($product, $additional);
 }
Example #2
0
 public function testGetAddUrl()
 {
     $productEntityId = 1;
     $storeId = 1;
     $productMock = $this->getMock('\\Magento\\Catalog\\Model\\Product', ['getEntityId', 'hasUrlDataObject', 'getUrlDataObject', '__wakeup'], [], '', false);
     $productMock->expects($this->any())->method('getEntityId')->will($this->returnValue($productEntityId));
     $productMock->expects($this->any())->method('hasUrlDataObject')->will($this->returnValue(true));
     $productMock->expects($this->any())->method('getUrlDataObject')->will($this->returnValue(new Object(['store_id' => $storeId])));
     $currentUrl = 'http://www.example.com/';
     $this->urlBuilderMock->expects($this->any())->method('getCurrentUrl')->will($this->returnValue($currentUrl));
     $this->requestMock->expects($this->any())->method('getRouteName')->will($this->returnValue('checkout'));
     $this->requestMock->expects($this->any())->method('getControllerName')->will($this->returnValue('cart'));
     $params = [Action::PARAM_NAME_URL_ENCODED => strtr(base64_encode($currentUrl), '+/=', '-_,'), 'product' => $productEntityId, 'custom_param' => 'value', '_scope' => $storeId, '_scope_to_url' => true, 'in_cart' => 1];
     $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('checkout/cart/add', $params);
     $this->helper->getAddUrl($productMock, ['custom_param' => 'value']);
 }
Example #3
0
 /**
  * Retrieve url for direct adding product to cart
  *
  * @param \Magento\Catalog\Model\Product $product
  * @param array $additional
  * @return string
  */
 public function getAddToCartUrl($product, $additional = [])
 {
     return $this->_cartHelper->getAddUrl($product, $additional);
 }