Magento: Preview Products for Non-Registered Users
As a senior developer specializing in e-commerce platforms like Magento, I often encounter unique requirements from clients regarding user authentication and product preview functionalities. In this post, we’ll explore how to implement a feature that allows non-registered users to preview products shared on social media but requires them to log in before adding items to their cart.
Understanding the Requirement
The client wants:
- Social sharing of products using platforms like Facebook.
- A preview mode for non-logged-in users who click on these shared links.
- To restrict access to the “Add to Cart” button, requiring login first.
- The checkout process to remain secured and logged-in.
Evaluating Different Approaches
Before diving into implementation, let’s consider two main approaches:
Approach 1: Disabling Login for Preview Mode
This approach involves:
- Enabling guest checkout globally in Magento Admin (Stores > Configuration > Sales > Checkout Options).
- This allows non-logged users to view products and even add them to the cart.
- However, it doesn’t provide a “preview mode” specifically; all users can see prices and details without logging in.
Approach 2: Customizing Product View Logic
This approach involves:
- Creating a custom module/extension that intercepts the product view logic.
- When a user navigates to a product page, check if they are logged in.
- If not, show a limited preview (like eBay doesâshowing images, basic descriptions).
- Implement a “Login to View Details” or “Add to Cart” button.
Implementing Custom Product Preview
Let’s explore the second approach in more detail. We’ll create a custom module that overrides the product view layout:
-
First, extend the
Magento_Catalogmodule by creating your own module (e.g.,Vendor_ProductPreview). -
In your module, override the
catalog_product_view.xmllayout file located atapp/code/Vendor/ProductPreview/view/frontend/layout/catalog_product_view.xml. -
Add a condition to check if the customer is logged in or not using Magento’s
$block->getCustomerSession()->isLoggedIn()method.
<referenceContainer name="content">
<block class="Magento\Catalog\Block\Product\View" name="product.info" template="Vendor_ProductPreview::product/preview.phtml"
ifnot="customer_session.is_logged_in()"/>
</referenceContainer>
-
Create a new
preview.phtmltemplate in the specified path to display the limited preview content. -
In your preview template, you can show product images and basic descriptions while hiding sensitive information like prices or “Add to Cart” buttons.
Integrating Social Sharing
To enable social sharing of products:
- Use an existing Magento extension for social sharing (like Amasty’s Social Pug) or create a custom one.
- Customize the sharing options in
app/code/Vendor/ProductPreview/view/frontend/requirejs-config.jsby adding dependencies on the social share scripts.
Conclusion
Implementing this feature requires a good understanding of Magento layout XML and customer session management. While it’s technically possible, I would recommend a more straightforward approach:
Recommendation: Allow guest checkout globally while keeping login requirements for the checkout process. This provides the best balance between user experience (previewing products) and security (secured purchase).
If you still need help with customizing Magento or implementing any other specific features, feel free to reach out to our expert team at Laravel Company.