0

I need to show/hide the add-to-cart button based on some custom values. These values are stored in a product attribute. The values are updated at the time of order creation.

I am using sections.xml for this to overcome the cache issue. But I am facing issues while getting the product id in the source map php file.

I am referred the below link to do this. https://aureatelabs.com/magento-2/how-to-use-private-content-or-sections-in-magento-2/

7
  • Bro can you take a look at my question, its not related to your question, i just thought you may know the answer. here is my question magento.stackexchange.com/questions/358034/… Commented Jul 20, 2022 at 16:14
  • @PyarayAfzel please don't spam in comment. Commented Jul 20, 2022 at 17:20
  • @Jancy Abraham Please provide more details because it's not clear how a private content is related to some specific attributes in your product and what is logic to show/hide add to cart button. Do you want to use section logic to load private content and pass current product id for resolve something? Commented Jul 20, 2022 at 17:20
  • @VictorTihonchuk I have created a deal functionality. In that only limited qtys are available. After each order the deal qty is reduced. I want to hide the addtocart button when the deal qty exceeds. I am saving this deal in a separate table with product id, deal qty and deal sale qty. Commented Jul 20, 2022 at 17:33
  • @JancyAbraham If you want to store all those data in client local storage you can use section functionality, but i don't recommend to do in this way (This is overhead with server load and browser as well). Better to create custom controller action to receive actual qty for product and create JS component with AJAX query for receive this data from server for product and render content based on this value. P.S. Magento flush affected product pages cache after each purchase. Commented Jul 20, 2022 at 17:44

1 Answer 1

0

I achieved this with sections.

I am passing the values as json in the CustomSection.php file in app/code/Namespace/Module/CustomerData.

  <?php
    namespace Namespace\Module\CustomerData;
    use Magento\Customer\CustomerData\SectionSourceInterface;
    
    class CustomSection implements SectionSourceInterface
    {
        public function getSectionData()
        {
            return [
                'customdata' => "We are getting data from custom section",
                'deal'=> json_encode([['id'=>'14','display'=>'true'],['id'=>'11','display'=>'false']]),
            ];
        }
    }

Here is the phtml file

<?php $_product = $block->getProduct();
$buttonTitle ="Add To Cart"; ?>

<div class="aureatelabs-custom-section"data-bind="scope: 'section'">
    <p data-bind="text: customsection().customdata"></p>
    <p data-bind="text: customsection().deal"></p>
    <!--ko foreach: JSON.parse(customsection().deal)-->    
        <!--ko if: ($data.id == <?php echo $_product->getId()?> && $data.display == true)-->
        
            <p class="label" data-bind="text: id"></p>
            <button type="submit"
                    title="<?= $block->escapeHtmlAttr($buttonTitle) ?>"
                    class="action primary tocart"
                    id="product-addtocart-button">
                <span><?= $block->escapeHtml($buttonTitle) ?></span>
            </button>
        <!-- /ko -->
    <!-- /ko -->
</div>
<script type="text/x-magento-init">
    {
        "*": {
            "Magento_Ui/js/core/app": {
                "components": {
                    "section": {
                        "component": "Namespace_Module/js/section"
                    }
                }
            }
        }
    }
</script>

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.