0

I have a configurable product, which has 4 child products. I want to show the child products to the end customer's product view page, based on a specific attribute value in the child products. (e.g 'Hide child from product page' set to 'true'). So if one of those 4 product has this attribute value set as true, then only 3 options will be visible on the product page, despite of that product being in stock, and enabled.

Disabling the child product does exactly this , but I am looking for a possibility of doing this without disabling it.

2 Answers 2

0

Try below code as per my understanding I add below code. hide-from-product-page replace your attribute code.

<?php
/** @var \Magento\Catalog\Block\Product\View\Type\Configurable $block */

$_product    = $block->getProduct();
$_attributes = $block->getAllowAttributes();
$_jsonConfig = $block->getJsonConfig();
$_rendered   = [];
?>

<?php if ($_product->isSaleable() && count($_attributes)) :?>
    <div class="field configurable required">
        <label class="label" for="attribute<?php /* @escapeNotVerified */ echo $block->getHtmlId() ?>">
            <span><?php /* @escapeNotVerified */ echo __('Options') ?></span>
        </label>
        <div class="control">
            <select name="super_attribute[<?php /* @escapeNotVerified */ echo $block->getProduct()->getId() ?>]" id="attribute<?php /* @escapeNotVerified */ echo $block->getHtmlId() ?>" class="super-attribute-select" data-mage-init='{"Magento_ConfigurableProduct/js/configurable":{"attributes":{"<?php /* @escapeNotVerified */ echo $block->getProduct()->getId() ?>":{"selected":"<?php /* @escapeNotVerified */ echo $block->escapeHtml(json_encode($block->getSelectedAttributes())) ?>","template":"<?php /* @escapeNotVerified */ echo $block->escapeHtml($_template) ?>","prices":<?php /* @escapeNotVerified */ echo $block->getJsonConfig()->getConfigurablePriceList() ?>}}}'>

            <?php foreach ($_attributes as $_attribute) : ?>
                <option value=""><?php /* @escapeNotVerified */ echo __('Choose an Option...') ?></option>
                <?php foreach ($_attribute->getOptions() as $_option) : ?>
                    <?php $_selectionPriceValue = $_product->getPriceInfo()->getPrice('option_value')->getValue(); ?>
                    <option data-calculator-type="<?php /* @escapeNotVerified */ echo $_option->getData('calculator_type') ?>" value="<?php /* @escapeNotVerified */ echo $_option->getValueIndex() ?>"<?php echo $block->getIsDisabled() ? ' disabled' : '' ?>>
                        <?php echo $block->escapeHtml($_option->getLabel()) ?>
                        <?php echo $block->getProductPrice($_selectionPriceValue, true) ?>
                    </option>
                <?php endforeach; ?>
            <?php endforeach; ?>
            </select>
        </div>
    </div>
<?php endif; ?>

<script>
require(['jquery'], function($) {
    $(document).ready(function() {
        // Get the selected options
        var selectedOptions = [];
        $('.super-attribute-select').each(function() {
            selectedOptions.push($(this).val());
        });

        // Hide options based on attribute value
        $('.super-attribute-select').each(function() {
            var optionSelect = $(this);
            optionSelect.find('option').each(function() {
                var optionValue = $(this).val();
                var optionText = $(this).text();
                var hideOption = false;

                // Check if the option's corresponding product has the attribute set to true
                // Adjust the condition based on your attribute code and value
                if (optionValue !== '' && selectedOptions.indexOf(optionValue) === -1) {
                    var product = optionSelect.closest('.product-info-main').find('.product-id-' + optionValue);
                    if (product.length && product.data('hide-from-product-page') === true) {
                        hideOption = true;
                    }
                }

                if (hideOption) {
                    optionSelect.find('option[value="' + optionValue + '"]').hide();
                } else {
                    optionSelect.find('option[value="' + optionValue + '"]').show();
                }
            });
        });
    });
});
</script>

Let me know if any queries.

Thanks

3
  • Just to confirm , this is for the page module-configurable-product/view/frontend/templates/product/view/type/options/configurable.phtml ? Feb 22 at 7:06
  • yes, you need to add code there. Feb 22 at 7:10
  • This worked! Thank you so much. Feb 22 at 12:15
0

You can try below solution for the same:

Create a Plugin

Create a file in your custom module, for example:

app/code/YourVendor/YourModule/etc/frontend/di.xml:

Here is the code for di.xml file:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\ConfigurableProduct\Model\Product\Type\Configurable">
        <plugin name="hide_child_products" type="YourVendor\YourModule\Plugin\HideChildProducts" sortOrder="1"/>
    </type>
</config>

Create the Plugin Class:

<?php

namespace YourVendor\YourModule\Plugin;

class HideChildProducts
{
    public function afterGetUsedProducts(\Magento\ConfigurableProduct\Model\Product\Type\Configurable $subject, $result)
    {
        // // Implement your custom condition here
        foreach ($result as $key => $product) {

            $productId = $product->getId();
            if ($this->shouldHideChild($productId)) {
                unset($result[$key]);
            }
        }
        return $result;
    }

    // Implement your custom condition function
    private function shouldHideChild($productId)
    {
        // I have used static condition here. You can modify the condition as per your requirements.
        if ($productId == 2025) {
            return true;
        }
        return false;
    }
}

Please replace YourVendor and YourModule with your actual vendor and module names.

Clear Cache:

bin/magento cache:clean

I hope this resolves the issue. If you encounter any further challenges or have additional questions, feel free to ask!

1
  • Thank you for your response. Based on the details I shared on my question, this is indeed a perfect solution. But I am using a marketplace module, where the product owner (who is a customer in magento with seperate customer group) can enable / disable the product. For this, the owner has to see the disabled product , so he/she can enable it in future. Feb 22 at 7:19

Your Answer

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

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