I have a magento 2.2.9 based site and wondering if its possible to get a product via url by having the sku in the url?

so something like https://www.domain.com/shop/?sku=123

Wanted to know if there is a default way to do this that wouldn't require building any custom module.

  • How do you mean "get the product"? Do you want to fetch the data or do you want to go to the product page itself? In the first case take a look at the Magento API : devdocs.magento.com/guides/v2.3/rest/bk-rest.html Sep 5 '19 at 14:40
  • I mean so if someone wants to share a link to the product with a friend this is specific to configurable products say they want to send there friend a email to the specific product (the large blue shirt with a crew neck) .
    – Jayreis
    Sep 5 '19 at 15:36
  • You can retrieve the value of ?sku by using PHP. Start your code with $yourvariable=$_GET['sku']; Sep 5 '19 at 17:10
  • @Jayreis : have you got the soln? Sep 6 '19 at 18:30
  • No the solutions below all are for creating a module that would then load a custom page with the product info. However I want people to click a link and go to the default product detail page with the specific configuration loaded based on a sku variable thats in the url.
    – Jayreis
    Sep 7 '19 at 3:53

Try below code:

If Url like this https://www.domain.com/shop/?sku=123, then

Using Construct Method:

private $productRepository; 

protected $request;
public function __construct(
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
    \Magento\Framework\App\Request\Http $request,
) {
    $this->productRepository = $productRepository;
    $this->request = $request;

public function loadMyProduct()
    $params = $this->request->getParams(); // all params
    if(is_array($params['sku']) && count($params) > 0){
        $proSku  = isset($params['sku']) ? $params['sku'] : '';
        $product = $this->productRepository->get($proSku);
        return $product;
    return '';

Using Object Manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$request       = $objectManager->get('Magento\Framework\App\Request\Http');
$params        = $request->getParams();
$productRepository = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface');
$product = $productRepository->get($params['sku']);
echo "pro name".$product->getName()." == ".$product->getPrice();

Note: Magento did not recommend the Object Manager Method

I hope it will helps...!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.