0

I have a custom module that checks Pincode, kindly check the screenshots below:- enter image description here

I have created a custom import function and added a table to the database which will hold the value imported by CSV below is the screenshot:-

enter image description here

now I want to fetch this table on the frontend where I check Pincode and if the Pincode exist in the database then it should show the message of delivery available.

please also check the code below

vendor/module/Model/Pincode.php

<?php
namespace Learn\CheckDelivery\Model;

class Pincode extends \Magento\Framework\Model\AbstractModel
{
    public function _construct()
    {
        $this->_init('Learn\CheckDelivery\Model\ResourceModel\Pincode');
    }
}

vendor/module/Model/ResourceModel/Pincode.php

<?php
namespace Learn\CheckDelivery\Model\ResourceModel;

class Pincode extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    public function _construct()
    {
        $this->_init('learn_checkdelivery', 'id');
    }
}

vendor/module/Model/ResourceModel/Pincode/Collection.php

<?php
namespace Learn\CheckDelivery\Model\ResourceModel\Pincode;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection 
{
    protected function _construct()
    {
        $this->_init('Learn\CheckDelivery\Model\Pincode','Learn\CheckDelivery\Model\ResourceModel\Pincode');
    }

    //print_r($datacollection);
}

now I want to know how to achieve this

UPDATE

VENDOR/MODULE/view/frontend/layout/templates/product/view/check_delivery.phtml

<?php $product = $this->getCurrentProduct()  ?>

<?php $helper=$this->helper("Learn\CheckDelivery\Helper\Data");?>
<?php if($product && $product->getCheckDeliveryEnable() != 2 && $helper->getIsActive() == 1): ?>
    <form class="form" id="check-delivery-form" method="post">
       <fieldset class="fieldset">
           <legend class="legend"><span><?php echo __('Check Delivery') ?></span></legend><br>
            <div class="field required">
                <label for="delivery_postcode" class="label"><span><?php echo __('Postcode') ?></span></label>
                <div class="control">
                    <input type="text" name="postcode" id="delivery_postcode" value="" title="<?php echo __('Postcode') ?>" class="input-text" data-validate="{required:true, 'validate-digits':true}">
                </div>
            </div>
            <div class="message"></div>
       </fieldset>
       <div class="actions-toolbar">
            <div class="primary">
                <button type="submit" class="action submit primary action-check-delivery" title="<?php  echo __('Check') ?>"><span><?php echo __('Check') ?></span></button>
            </div>
        </div>  
    </form>

<script>
require([
    'jquery',
    'mage/mage'
], function ($) { 
    var dataForm = $('#check-delivery-form');
    dataForm.mage('validation', {});
    var dataFormMessage = $('#check-delivery-form .message');
    dataFormMessage.removeClass('success').removeClass('error').hide();
    $('.action-check-delivery').on('click',function () {
            var formData = new FormData();
            formData.append('postcode', $('#check-delivery-form input[name="postcode"]').val());
            $.ajax({
                url: '<?php echo $this->getUrl('checkDelivery/postcode/check',['id'=> $product->getId()]) ?>',
                data: formData,
                processData: false,
                contentType: false,
                showLoader: true,
                type: 'POST',
                dataType: 'json',
                success: function (response) {
                    dataFormMessage.removeClass('success').removeClass('error').hide();
                    dataFormMessage.addClass(response.type).html(response.message).show();
                }
            });
            return false;
    });
});
</script>
<?php endif; ?>

Block.php

<?php

namespace Learn\CheckDelivery\Block\Product\View;

use Magento\Framework\Registry;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

class CheckDelivery extends Template
{

    protected $_registry;

    public function __construct(
        Context $context,
        Registry $registry,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_registry = $registry;
    }



    public function getCurrentProduct()
    {
        return $this->_registry->registry('current_product');
    }
}
0

@pramod

you can simply get the collection of your model like : this code you need to write down in your block file.

<?php

namespace Learn\CheckDelivery\Block\Product\View;

use Magento\Framework\Registry;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

class CheckDelivery extends Template
{

    protected $_registry;
    protected $_$pincode;
    public function __construct(
        Context $context,
        Registry $registry,
        \Learn\CheckDelivery\Model\PincodeFactory $pincode,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_registry = $registry;
        $this->_pincode = $pincode;
    }

    public function getPincodeCollection($pincode){
      $collection = $this->_pincode->create();
$collection->getCollection();
$collection->addFiledtoFilter('pincode',$pincode);
$collection->getData();
//you can check the data here like
return $collection;
    }

    public function getCurrentProduct()
    {
        return $this->_registry->registry('current_product');
    }
}

in your phtml :

<script>
require([
    'jquery',
    'mage/mage'
], function ($) { 
    var dataForm = $('#check-delivery-form');
    dataForm.mage('validation', {});
    var dataFormMessage = $('#check-delivery-form .message');
    dataFormMessage.removeClass('success').removeClass('error').hide();
    $('.action-check-delivery').on('click',function () {
            var formData = new FormData();
            formData.append('postcode', $('#check-delivery-form input[name="postcode"]').val());
            var pincode = $('#check-delivery-form input[name="postcode"]').val(); 
            $.ajax({
                url: '<?php echo $this->getUrl('checkDelivery/postcode/check',['postcode'=> pincode]) ?>',
                data: formData,
                processData: false,
                contentType: false,
                showLoader: true,
                type: 'POST',
                dataType: 'json',
                success: function (response) {
                    dataFormMessage.removeClass('success').removeClass('error').hide();
                    dataFormMessage.addClass(response.type).html(response.message).show();
                }
            });
            return false;
    });
});
</script>
| improve this answer | |
  • i think there is problem in phtml file as after using your code pincode check field does not show up – Pramod Feb 10 at 9:19
  • url: '<?php echo $this->getUrl('checkDelivery/postcode/check',['postcode'=> pincode]) ?>', i think there is an issue with line – Pramod Feb 10 at 9:20
  • you need to check that in var pincode you have to got a proper value or not ? if you got a proper value then i must a pass in ajax data – kinjal mehta Feb 10 at 9:34
  • or as per your phtml code <input type="text" name="postcode" id="delivery_postcode" value="" title="<?php echo __('Postcode') ?>" class="input-text" data-validate="{required:true, 'validate-digits':true}"> you need to set var pincode = $('#delivery_postcode').val(); i hope so this will work for you – kinjal mehta Feb 10 at 9:37
0

You need to call the model collection factory into your constructor

protected $yourCollectionFactory;
public function __construct(
    ....
    \[Namespace]\[Module]\Model\ResourceModel\[Entity]\CollectionFactory $yourCollectionFactory,
    ...
)
{
    ...
    $this->yourCollectionFactory = $yourCollectionFactory;
    ...
}

And use in your method like the below example.

public function yourMethod(){
    $collection = $this->yourCollectionFactory->create();
}

I hope it'll help you.

| improve this answer | |
  • am not gettting it. i want to get the collection my phtml file where this check delivery form is coming up – Pramod Feb 10 at 6:30
  • @Pramod First you need to get the collection on your block files method and then call that method on your .phtml file. – Mayank-Dolphin Feb 10 at 6:31
  • do you want me to share my block code also?/ – Pramod Feb 10 at 6:32
  • @Pramod Yes, I'll would be good if you share your block code with us. – Mayank-Dolphin Feb 10 at 6:36
  • sure give me a min sir – Pramod Feb 10 at 6:40

Your Answer

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

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