Preselect configurable product options by simple p

2020-07-18 08:25发布

问题:

How to show configurable product with preselected options if requested url is for simple product?

For example:

Simple product #1 has:
Color: Red
URL: /simple-red.html

Simple product #2 has:
Color: Green
URL: /simple-green.html

Configurable product has:
URL: /config.html

If user visits /simple-red.html it should be loaded configurable product with pre-selected option Color: Red

If user visits /simple-green.html it should be loaded configurable product with pre-selected option Color: Green

回答1:

Successfully solved the problem:

  1. Extended ProductController, to replace product id of simple product by product id of parent configurable product. Used SO Answer:
    Magento Catalog ProductController rewrite

    Code in custom ProductController:

    ...
    $productId  = (int) $this->getRequest()->getParam('id');
    
    // Get parent configurable product
    $_product = Mage::getModel('catalog/product')->load($productId);
    if ($_product->getTypeId() == "simple") {
        $parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($_product->getId());
    
        // If parent exists
        if (isset($parentIds[0])) {
            $productId = $parentIds[0];
        }
    }
    ...
    
  2. PreSelect configurable product options depending on simple product. Used link to tutorial given by Vishal Sharma

Result (sorry can't post images): Screenshot