jsonFactory->create(); $sku = trim((string)$this->request->getParam('sku')); if ($sku === '') { return $result->setHttpResponseCode(400)->setData([ 'success' => false, 'message' => __('A product SKU is required.') ]); } try { $storeId = (int)$this->storeManager->getStore()->getId(); $product = $this->productRepository->get($sku, false, $storeId, true); $image = $this->imageFactory->create() ->init($product, 'category_page_grid') ->getUrl(); $regularPrice = (float)$product->getPrice(); $finalPrice = (float)$product->getFinalPrice(); $hasRequiredOptions = $product->getTypeInstance()->hasRequiredOptions($product); return $result->setData([ 'success' => true, 'product' => [ 'id' => (int)$product->getId(), 'sku' => $product->getSku(), 'name' => $product->getName(), 'image' => $image, 'url' => $product->getProductUrl(), 'price' => $this->priceCurrency->format($finalPrice, false), 'old_price' => $regularPrice > $finalPrice ? $this->priceCurrency->format($regularPrice, false) : null, 'add_url' => $this->cartHelper->getAddUrl($product), 'can_add_to_cart' => $product->isSaleable() && !$hasRequiredOptions ] ]); } catch (\Throwable $exception) { return $result->setHttpResponseCode(404)->setData([ 'success' => false, 'message' => __('The product could not be loaded.') ]); } } }