1

I'm new in making modules, considering below code wondering how would be possible to verify if the checkbox has been selected:

my observer.php:

public function execute(\Magento\Framework\Event\Observer $observer) {
        $item=$observer->getEvent()->getData('quote_item');
        $product=$observer->getEvent()->getData('product');
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
            if (???) {
                // do this if checbox is selected
            } else {

                // do this if not selected
        }
}

my viewfile.phtml

<?php

/** @var $block \Magento\Catalog\Block\Product\View */
?>
<?php $_product = $block->getProduct(); ?>
<?php if ($_product->isSaleable()):?>      
        <div id="rent>
           <input id="my_checkbox" type="checkbox" name="vehicle" value="Rent">I want to rent this product        
        </div>
        <script type="text/javascript">
        require([
            'jquery'
        ], function ($) {
            $(document).ready(function () {
                if($('#my_checkbox').prop('checked')) {
                    //checkbox is checked
                } else {
                    //checkbox is NOT checked
                }
            });
        }

        });
    </script>

<?php endif; ?>
2
  • which event are you observing ? Commented Nov 9, 2016 at 9:29
  • If your are observing save event than you can get data from $_POST object. Commented Nov 9, 2016 at 9:32

2 Answers 2

1

You have to check inside observer.php file,

<?php

public function execute(\Magento\Framework\Event\Observer $observer) {
        $item=$observer->getEvent()->getData('quote_item');
        $product=$observer->getEvent()->getData('product');
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );

        if(isset($_POST['vehicle']) && $_POST['vehicle'] == 'Rent') 
        {
            echo "Checkbox value";
        }
        else
        {
            echo "Not selected";
        } 
}
0
0

You can use \Magento\Framework\App\RequestInterface to get the param.

protected $_request;

public function __construct(
    \Magento\Framework\App\RequestInterface $request
)
{
    $this->_request = $request;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $vehicle = $this->_request->getParam('vehicle');
    //Check the condition here

}

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.