2

How to get configurable product page, associate product custom option. ?

Thanks.

4
  • Can you explain what exactly you want to do? Do you want to get url to product? Dec 14, 2017 at 11:46
  • Get custom option of simple product from selected configurable option printpress.mag.dkpmedia.co.uk/index.php/category1/posters.html
    – Manoj Pal
    Dec 14, 2017 at 12:00
  • Your goal is to display simples product's data on configurable product page, right? Dec 14, 2017 at 12:02
  • Yes, also when use in add to cart page, This is i have completed in magento 1 but i don't know how to do in magento 2
    – Manoj Pal
    Dec 14, 2017 at 12:05

1 Answer 1

1

There is no easy way to do that. For get simple product's data on frontend page you can use ajax to load data for one product (via controller) or create function that return all required data for all child's on product page once. Then use js to manipulate it.

For example there is code to get all child's data for configurable:

<?php
namespace Vendor\Catalog\Block\Product\View;

class Description extends \Magento\Catalog\Block\Product\View\Description
{


    /** Function returns attributes of childs products for configurable product
     *
     * @return array
     */
    public function getConfigProductChild()
    {
        $data = [];
        if($this->getProduct() && $this->getProduct()->canConfigure()) {
            $productTypeInstance = $this->getProduct()->getTypeInstance();
            $usedProducts = $productTypeInstance->getUsedProducts($this->getProduct());

            foreach($usedProducts as $product) {
                $attributes = $product->getAttributes();
                $data['description'][$product->getId()] = $product->getDescription();
                foreach ($attributes as $attribute) {
                    if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), [])) {
                        $value = $attribute->getFrontend()->getValue($product);

                        if (!$product->hasData($attribute->getAttributeCode())) {
                            $value = __('N/A');
                        } elseif ((string)$value == '') {
                            $value = __('No');
                        } elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
                            $value = $this->priceCurrency->convertAndFormat($value);
                        }

                        if (($value instanceof Phrase || is_string($value)) && strlen($value)) {
                            $data['attributes'][$product->getId()][$attribute->getAttributeCode()] = [
                                'label' => __($attribute->getStoreLabel()),
                                'value' => $value,
                                'code' => $attribute->getAttributeCode(),
                            ];
                        }
                    }
                }
            }
        }
        return $data;
    }

}

this function:
$productTypeInstance->getUsedProducts($this->getProduct());
returns all simple product related to $this->getProduct().
Then call getConfigProductChild() function in product template on frontend. For example: view/templates/product/view/details.phtml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.