Loading Configurable Products with Pre-selected Options
In Magento, loading a configurable product with pre-selected options based on the simple product URL is a common requirement for enhancing user experience. This allows customers to directly access the configured version of a product with their preferred attributes already selected.
Understanding the Scenario
Let’s consider an example where you have two simple products:
- Simple Product #1: Color = Red, URL = /simple-red.html
- Simple Product #2: Color = Green, URL = /simple-green.html
You also have a configurable product that includes both these simple products as its options.
The Goal: Pre-select Options Based on the URL
The objective is to load the configurable product when users navigate to the simple product URLs. During this process, the selected attribute should match the one in the current URL.
For instance:
- When a user visits /simple-red.html, the configurable product should be loaded with Color pre-selected as Red.
- When a user visits /simple-green.html, the configurable product should be loaded with Color pre-selected as Green.
Implementing this Feature
Step 1: Identify the Simple Product ID
The first step involves identifying which simple product corresponds to the URL being accessed. In Magento, you can achieve this by parsing the current URL and extracting the relevant part (e.g., “simple-red” or “simple-green”).
$url = $this->getRequest()->getRequestUri();
preg_match('/simple-(.*).html/', $url, $matches);
$optionValue = $matches[1];
Step 2: Map Simple Product ID to Configurable Product
Next, you need to map the simple product ID to its corresponding configurable product. This can be accomplished by querying the database for the parent configurable product that contains the identified simple product.
$simpleProductId = /* your logic here */;
$configurableProduct = $this->productCollectionFactory->create()
->addFieldToFilter('entity_id', $simpleProductId)
->getFirstItem();
Step 3: Pre-select the Appropriate Option
Now that you have identified the configurable product and the simple product being accessed, it’s time to pre-select the corresponding option.
For this step, you’ll need to find the super attribute ID associated with the color. Then, set the configured options array with the selected attribute.
$superAttributeId = /* your logic here */;
$selectedOptions = [
'attribute_code' => 'color', // assuming the color is a configurable attribute
'value_index' => $optionValue,
];
$this->productRepository->save($configurableProduct, true);
Step 4: Load and Display the Configurable Product
Finally, you need to load the configured product with the pre-selected options and display it on the page.
$loadedProduct = $this->productRepository->findOneById($configurableProduct->getId());
$this->view->loadLayout();
$this->view->renderLayout();
Conclusion
Loading a configurable product with preselected options based on the simple product URL is a powerful feature for enhancing user experience and reducing friction in the purchasing process. By following the steps outlined above, you can achieve this functionality while keeping your code clean and efficient.
Remember to test this implementation thoroughly to ensure it handles edge cases correctly and provides a seamless customer experience.
For more advanced customizations and performance optimizations, consider exploring the Magento 2 development guides available on Laravel Company.