Show phtml file, only when button is clicked
-
04-10-2020 - |
Question
I want to create a quickview option with my crosssells. When I click on the quickview button, I want a .phtml
file to appear (custom/quickview.phtml
), and display with product information such as name, id, image etc..
This is the situation:
- I have 3 crosssell products on the cart page
- Inside the foreach loop that generates the products, I want a button that calls the
quickview.phtml
file - I want to pass
php
variables, such as product ID, product name etc.
How do I do this? I cant find out how to do it.
Solution
I assume that you want the quickview.phtml to appear in a lightbox without leaving the current page.
To do this, you will need to change the button action to an ajax call with a custom controller action to return the new dynamic HTML.
There are a number of existing extensions out there, and I highly recommend that you use one of them instead of creating your own solution. To do this right, it could easily take dozens of hours to implement (especially if you have any configurable or bundled products).
Take a look here:
https://www.magentocommerce.com/magento-connect/catalogsearch/result/?q=product+quick+view&pl=0
I have used both the Amasty and Belvg ones in the past for various projects.
OTHER TIPS
you can just pass the product id in a controller action and all the rest of the things can be obtained by id
yoursite.com/index.php/quickview/index/view/id
You need to create controller, block and view(phtml) to achieve this. Use below code in .phtml
<a href="<?php echo $block->getUrl('<module_name>/quickview/view', ['id'=>$id])?>">
<button class="button" title="<?php echo __('Confirm Order') ?>" type="button">
<span><span><span><?php echo __('Confirm Order') ?></span></span></span>
</button>
</a>
In controller
public function execute()
{
$id = $this->getRequest()->getParam('id');
// Your further code
}