1

I need dynamically change custom attribute on the configurable page when I click to the swatch color attribute. On this purpose I programmatically created custom attributes with options on the admin panel, where I can save for every product his own custom attributes. I figure out how to call my custom attributes on simple products but I can't to do this with configurable product.

I see when I change color attribute - price is changed for every simple product([it's default thing), how to do same but with my custom attributes? I have one idea but I don't know how this one implement. So I know how to get current product id when you clicked on the color attribute(through js code) then I need pass this id data to the controller via ajax. I need to know how exactly to do this, how to pass js data through ajax to controller and then call needed block on the category page every time when I click on the color attribute? ? When I will be know how to do that I get with this id data - custom attribute values and call this template in the needed place. Code below is that what call custom attributes on the configurable product, but it's call all attributes for all simple products included in the configurable product. I need call these attributes for current product every time when I cliked on the color attribute. For example: if I click on the black color (which related to the product with id 123 ) I get custom block with custom attributes without reloading page(like price).color attributes

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
    $product_id = $product->getId();
    $configProduct = $objectManager- 
    >create('Magento\Catalog\Model\Product')->load($product_id);

    $configurable = $configProduct->getTypeId() == 
  \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE;
   $_children = $configProduct->getTypeInstance()- 
   >getUsedProducts($configProduct);
   if ($configurable){
    foreach ($_children as $child){
        $child_badge = $child->getResource()->getAttribute('mycustom_attribute_code')->getFrontend()->getValue($child);
        echo $child_badge;
    }
}
4

0 回答 0