0

I have created a custom module and calling that template file from the CMS page as like below.

 {{block class="Vendor\Module\Block\Custom"    template="Vendor_Module::custom.phtml"}}

In templates/custom.phtml

I am fetching the all the products with attribute filters.

I am getting product collection and displaying in my template.

<?php
  $collection = $block->getCustomCollection();

  foreach($collection $product) {
   $productName = $product->getName();
   $productPrice = $product->getPrice();
   $productUrl = $product->getProductUrl();
   $productId = $product->getProductId();
  ?>
   <div class="" >
     <h2><?php echo $productName; ?></h2>
     <p><?php echo $productPrice; ?></p>
     <p><a href="<?php echo // How to redirect this product to custom template with product Data ?>"><?php echo $productName; ?></a></p>
   </div>

 <?php } ?>

Here my question is I need to redirect the Url to my custom template by passing product ID or SKU as arguement to implement add to cart functionality. not to the default product page.

How i can load custom product detail page in this way. Can i get the product data in my custom template here.

Once I get the product data in my custom template, I will fetch the corresponding product information and show it in the page.

Right now it will go to default product detail page. if i provide href as $productUrl

Can anyone help me please. Thanks

0

It is not good to do a redirect in phtml, if you need this in the phtml, this mean that your architecture is not good, the redirection must be done in the controller, I guess your need to send that params in another page ? so you have to do a controller, you get that params inside then you do kindly your redirect.

To redirect in controller:

$this->_redirect('router/controllerfolder/controllername');

With params:

$resultRedirect = $this->resultRedirectFactory->create();
$params = array('param1' => 'value', 'param2' => 'value2');
$resultRedirect->setPath('router/controllerfolder/controllername', ['params' => $params]);
return $resultRedirect;
  • @Prince, how to get that URL inside my href? I am not cleared with your answer actually.. – jafar pinjar Jan 28 at 17:40
  • Once it redirect to custom template, can I get the specific product I'd in it?, can you pls help with code how to get href value for each item of collection like in my question. – jafar pinjar Jan 28 at 17:44
0

Well, Redirecting in phtml is not good approach but for instance lets say you have product id and product sku. Now First getBaseUrl.

<?php
  $collection = $block->getCustomCollection();
  foreach($collection $product) {
   $productName = $product->getName();
  $productPrice = $product->getPrice();
$productUrl = $product->getProductUrl();
$productId = $product->getProductId();
?>
<div class="" >
 <h2><?php echo $productName; ?></h2>
 <p><?php echo $productPrice; ?></p>
 <p><a href="<?php $block->getBaseUrl().'routename/controller/action/'.$productId ?>"><?php echo $productName; ?></a></p>

this is how you can redirect you page with productId.

  • this will redirect to controller with I'd, the resulting page should be loaded with template with some HTML like get product image and display in there, how can I load custom template from the new controller with the id or sku – jafar pinjar Jan 29 at 3:43
  • You got what I am trying here exactly? Once href is clicked, I need to load my custom template with product info of that I'd. – jafar pinjar Jan 29 at 3:58

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.