1

I'm new in magento 2. I want to show popup after click add to cart button so I override add to cart button. And then I tried to get items added by this code but it does not work. Please help me.

CartPopup.php

<?php
namespace Work\Event\Block;
use Magento\Checkout\Block\Cart\AbstractCart;

class CartPopup extends AbstractCart
{
   protected $_cartHelper;
   protected $_cart;
   protected $_checkoutSession;
   protected $_customerSession;
   protected $product;

   public function __construct(
       \Magento\Framework\View\Element\Template\Context $context,
       \Magento\Customer\Model\Session $customerSession,
       \Magento\Checkout\Model\Session $checkoutSession,
       \Magento\Checkout\Helper\Cart $cartHelper,
       \Magento\Checkout\Model\Cart $cart,
       \Magento\Catalog\Model\Product $product,
       array $data = []
   )
   {
       $this->_checkoutSession = $checkoutSession;
       $this->_customerSession = $customerSession;
       $this->_cartHelper = $cartHelper;
       $this->_cart = $cart;
       $this->product = $product;

       parent::__construct($context, $customerSession, $checkoutSession, $data);
   }

   public function getProductIds()
   {
       $productId = $this->product->getId();
//        $cart = $this->_checkoutSession->getQuote()->getAllVisibleItems();
//        $cart = $this->_cartHelper->getQuote()->getItemsCount();
       $cart = $this->_cart->getQuote()->getAllVisibleItems();
       $itemsIds = [];
//        var_dump($cart);die;
       foreach ($cart as $cartItem) {
           array_push($itemsIds, $cartItem->getProduct()->getId());
       }
       return (in_array($productId, $itemsIds));
   }
}

detail.phtml

<div id="popup-modal" >
<?php
    $blockObj= $block->getLayout()->createBlock('Work\Event\Block\CartPopup');
        echo $blockObj->getProductIds();

?>

catalog_product_view.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="product.info.addtocart">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">Work_Event::cart_popup.phtml</argument>
        </action>
    </referenceBlock>
    <referenceBlock name="product.info.addtocart.additional">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">Work_Event::cart_popup.phtml</argument>
        </action>
    </referenceBlock>
</body>
</page>

cart_popup.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var $block \Magento\Catalog\Block\Product\View */
?>
<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php if ($_product->isSaleable()): ?>
<div class="box-tocart">
    <div class="fieldset">
        <?php if ($block->shouldRenderQuantity()): ?>
            <div class="field qty">
                <label class="label" for="qty"><span><?= /* @escapeNotVerified */ __('Qty') ?></span></label>
                <div class="control">
                    <input type="number"
                           name="qty"
                           id="qty"
                           value="<?= /* @escapeNotVerified */ $block->getProductDefaultQty() * 1 ?>"
                           title="<?= /* @escapeNotVerified */ __('Qty') ?>"
                           class="input-text qty"
                           data-validate="<?= $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
                    />
                </div>
            </div>
        <?php endif; ?>
        <div class="actions">
            <button type="submit"
                    title="<?= /* @escapeNotVerified */ $buttonTitle ?>"
                    class="action primary tocart"
                    id="product-addtocart-button">
                <span><?= /* @escapeNotVerified */ $buttonTitle ?></span>
            </button>
            <?= $block->getChildHtml('', true) ?>
        </div>
    </div>
</div>
<?php endif; ?>
<?php if ($block->isRedirectToCartEnabled()) : ?>
<script type="text/x-magento-init">
{
    "#product_addtocart_form": {
        "Magento_Catalog/product/view/validation": {
            "radioCheckboxClosest": ".nested"
        }
    }
}
</script>
<?php else : ?>
<script type="text/x-magento-init">
{
    "#product_addtocart_form": {
        "Magento_Catalog/js/validate-product": {}
    }
}
</script>
<?php endif; ?>

<div id="popup-modal"  >
<?php include ($block->getTemplateFile('Work_Event::detail.phtml')) ?>
</div>

<script>
require(
    [
        'jquery',
        'Magento_Ui/js/modal/modal'
    ],
    function(
        $,
        modal
    ) {
        var options = {
            type: 'popup',
            responsive: true,
            title: 'Your Cart',
            innerScroll: true,
            buttons: [{
                text: $.mage.__('Clear'),
                class: '',
                click: function () {
                    this.closeModal();
                }
            },
                {
                    text: $.mage.__('Go to check out'),
                    class: 'go-to-check-out',
                    click: function () {
                        this.closeModal();
                    }
                }
            ]
        };

        var popup = modal(options, $('#popup-modal'));
        $("#product-addtocart-button").on('click',function(){
            $("#popup-modal").modal("openModal");
        });

    }
);</script>
  • Can you update the layout xml as well? – Sukumar Gorai Jul 17 '18 at 7:25
  • Did you popup work or not? – Khoa TruongDinh Jul 17 '18 at 7:29
  • @SukumarGorai i updated – Thanh TN Jul 17 '18 at 7:32
  • @KhoaTruongDinh popup is working. but i can't show items added in cart – Thanh TN Jul 17 '18 at 7:42
  • return (in_array($productId, $itemsIds)); This code will return only true and false value as you are checking in_array function this returns boolean. What do you want there? – Sukumar Gorai Jul 17 '18 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.