How to get configurable product page, associate product custom option. ?
Thanks.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityHow to get configurable product page, associate product custom option. ?
Thanks.
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.