0

I have a requirement in M2 where I need the shareable file link of downloadable product.

  $product = $this->productRepository->getById($productId);

        // Output array
        $output = [];

        // Get the product downloadable links
        $links = $this->linkRepository->getLinksByProduct($product);
        
        // Add each link data to the output array
        foreach ($links as $item) {
            $output[] = $item->getData();
        }

This piece of code is returning the details with relative path, I want the shareable file link which I can share with others and they can also download, is it possible?

2 Answers 2

0

To get shareable file links for downloadable products in Magento 2, you can follow these steps:

  1. Login to your Magento 2 admin panel.
  2. Go to Catalog > Products and select the downloadable product you want get a shareable file link.
  3. Click the Edit button to edit the product.
  4. Scroll down to the Downloadable Information section and click on the Links tab.
  5. Find the link of the file you want to share and click the "Edit" button next to it.
  6. In the "File" tab, you will see a field labeled "Shareable link". Click the "Generate" button next to it.
  7. will generate a unique shareable link. You can copy this link and share it with anyone who needs to download the file.

Please note that shareable links are only valid for the number of downloads and validity period you specified for the downloadable product. Once the limit is reached or the validity period ends, the link will no longer be valid.

2
  • Thanks for the quick response, I want it programatically Commented Mar 21, 2023 at 4:54
  • Hello @Deepak Mankotia, in "downloadable_link" table there is column "link_file" which is used. and for purchased item "downloadable_link_purchased_item" table have same column which have shareable link Commented Mar 21, 2023 at 5:01
0

To generate a shareable link, you can use the following code snippet:

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);

    $links = $product->getTypeInstance()->getLinks($product);

    foreach ($links as $link) {
        $linkUrl = $link->getLinkUrl();
        if ($linkUrl) {
            $shareableLink = $linkUrl . '?__a=1';
            // Use $shareableLink as the shareable link for this downloadable link
        }
    }

In this code, we are first loading the product by its ID, then getting the downloadable links for the product. For each downloadable link, we are retrieving the link URL and appending ?__a=1 to it, which is a query parameter that tells Magento to allow direct file access without logging in.

This should generate a shareable link for each downloadable link, which can be shared with others and allow them to download the file directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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