How to Configure Product Custom Options in Magento 2
When you add custom options to a product in Magento 2, you offer your customers the flexibility to select product options based on their individual preferences, rather than being limited to predefined product attributes. This adds an extra layer of customization and enhances the shopping experience.
With this feature, you can create versatile product options like custom cart price rules, dynamic category rules, and much more. It allows you to cater to specific customer needs, making their buying experience more personalized and engaging.
If your goal is to provide a more tailored shopping experience, adding custom product options is one of the most effective strategies to implement in your Magento store.
Get Product Custom Options Programmatically in Magento 2
- Step 1: Add Object Manager – Inject the Object Manager into your code to access Magento's components and manage product objects dynamically.
- Step 2: Generate the Product – Retrieve the product object based on the product ID to modify the desired product with custom options.
- Step 3: Custom Options – Add custom options to the product, allowing customers to select from multiple configurations or personalizations when purchasing.
- Step 4: Configurable Options – Implement configurable options for products with multiple variations, such as size or color, enabling customers to select from predefined choices.
- Step 5: Grouped Childs – Manage grouped products by adding child products sold together as a collection or bundle.
- Step 6: Bundle Selection – Set up bundle products by defining product selections, giving customers flexibility in creating their own product bundles.
Step 1: Create an object manager
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
Step 2: Generate the product
Generate the product that needs to be assigned custom options.
$product = $_objectManager->get('\Magento\Catalog\Model\Product')->load($id);
Step 3: Custom options
$customOptions = $_objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($product);
Step 4: Configurable Options
$productTypeInstance = $_objectManager->get('Magento\ConfigurableProduct\Model\Product\Type\Configurable'); $productAttributeOptions = $productTypeInstance->getConfigurableAttributesAsArray($product);
Step 5: Grouped Childs
$typeInstance = $_objectManager->get('Magento\GroupedProduct\Model\Product\Type\Grouped'); $childs = $typeInstance->getAssociatedProducts($product);
Step 6: Bundle selection
$store_id = $_storeManager->getStore()->getId(); $options = $_objectManager->get('Magento\Bundle\Model\Option') ->getResourceCollection() ->setProductIdFilter($product->getId()) ->setPositionOrder(); $options->joinValues($store_id); $typeInstance = $_objectManager->get('Magento\Bundle\Model\Product\Type'); $selections = $typeInstance->getSelectionsCollection($typeInstance->getOptionsIds($product), $product);
When completing all, this means the product options are active on your Magento 2 store.
How to Configure Magento 2 Custom Options from the Backend?
- Navigate to Catalog > Products and select the product you wish to customize.
- Alternatively, you can create a new product by clicking the Add New Product button.
- Scroll down to locate the Customizable Options section and click on Add Option.
- Enter a name for the option and select the type.
- To make the option mandatory, check the Required box.
- Click on Add Value and complete the following fields:
- Title
- Price
- Price Type
- SKU
- Once you're done, save your settings.
- This is how it will appear on the product page.
After configuring the custom options for your product, be sure to save the changes to apply them. This will ensure that your customers can view and interact with the new options you’ve created.
Once saved, the custom options will be visible on the product page. This allows customers to choose from the options you've set, such as size, color, or other specifications, enhancing their shopping experience.
By offering these customizable features, you make it easier for customers to find exactly what they need, potentially increasing sales and customer satisfaction.