1

I have overrided the core(history.phtml corin module-sales) phtml file and created the custom link using popup modal to update the custom status as cancel initiated. And it works only for the last order id. If I click another id to update, again it updated for only the last id. But it does not get the current id to update. How to get the current id before model loads? Image for reference. please provide me a solution. enter image description here

history.phtml

<?php $_orders = $block->getOrders(); ?>
<?= $block->getChildHtml('info') ?>
<?php if ($_orders && count($_orders)): ?>
    <div class="table-wrapper orders-history">
        <table class="data table table-order-items history" id="my-orders-table">
            <caption class="table-caption"><?= /* @escapeNotVerified */ __('Orders') ?></caption>
            <thead>
                <tr>
                    <th scope="col" class="col id"><?= /* @escapeNotVerified */ __('Order #') ?></th>
                    <th scope="col" class="col date"><?= /* @escapeNotVerified */ __('Date') ?></th>
                    <?= /* @noEscape */ $block->getChildHtml('extra.column.header') ?>
                    <th scope="col" class="col shipping"><?= /* @escapeNotVerified */ __('Ship To') ?></th>
                    <th scope="col" class="col total"><?= /* @escapeNotVerified */ __('Order Total') ?></th>
                    <th scope="col" class="col status"><?= /* @escapeNotVerified */ __('Status') ?></th>
                    <th scope="col" class="col actions"><?= /* @escapeNotVerified */ __('Action') ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($_orders as $_order): ?>
                    <tr>
                        <td data-th="<?= $block->escapeHtml(__('Order #')) ?>" class="col id"><?= /* @escapeNotVerified */ $_order->getRealOrderId() ?></td>
                        <td data-th="<?= $block->escapeHtml(__('Date')) ?>" class="col date"><?= /* @escapeNotVerified */ $block->formatDate($_order->getCreatedAt()) ?></td>
                        <?php $extra = $block->getChildBlock('extra.container'); ?>
                        <?php if ($extra): ?>
                            <?php $extra->setOrder($_order); ?>
                            <?= /* @noEscape */ $extra->getChildHtml() ?>
                        <?php endif; ?>
                        <td data-th="<?= $block->escapeHtml(__('Ship To')) ?>" class="col shipping"><?= $_order->getShippingAddress() ? $block->escapeHtml($_order->getShippingAddress()->getName()) : '&nbsp;' ?></td>
                        <td data-th="<?= $block->escapeHtml(__('Order Total')) ?>" class="col total"><?= /* @escapeNotVerified */ $_order->formatPrice($_order->getGrandTotal()) ?></td>
                        <td data-th="<?= $block->escapeHtml(__('Status')) ?>" class="col status"><?= /* @escapeNotVerified */ $_order->getStatusLabel() ?></td>
                        <td data-th="<?= $block->escapeHtml(__('Actions')) ?>" class="col actions">
                            <a href="<?= /* @escapeNotVerified */ $block->getViewUrl($_order) ?>" class="action view">
                                <span><?= /* @escapeNotVerified */ __('View Order') ?></span>
                            </a>
                            <?php if ($this->helper('Magento\Sales\Helper\Reorder')->canReorder($_order->getEntityId())) : ?>
                                <a href="#" data-post='<?php
                                /* @escapeNotVerified */ echo
                                        $this->helper(\Magento\Framework\Data\Helper\PostHelper::class)
                                        ->getPostData($block->getReorderUrl($_order))
                                ?>' class="action order">
                                    <span><?= /* @escapeNotVerified */ __('Reorder') ?></span>
                                </a>
                            <?php endif ?>
                            <div> **I have added my custom link here**
                                <a href="#" id="click" class="click-me" data-id="<?= $_order->getEntityId(); ?>" >Cancel</a>

                            </div>
                       </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php if ($block->getPagerHtml()): ?>
        <div class="order-products-toolbar toolbar bottom"><?= $block->getPagerHtml() ?></div>
    <?php endif ?>
<?php else: ?>
    <div class="message info empty"><span><?= /* @escapeNotVerified */ __('You have placed no orders.') ?></span></div>
<?php endif ?>


<div id="popup-modal" style="display:none;">
    <form action="<?php echo $block->getBaseUrl() . 'oxsales/index/index'; ?> " method="post" id="form-validate" enctype="multipart/form-data" autocomplete="off" data-mage-init='{"validation":{}}' data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>">
        <div id="result"></div>
        <div>   <label>Reason:</label><textarea id="reason" rows="4" cols="60" name="reason"></textarea></div>

    </form>
</div>
<script type="text/x-magento-init">
    {
    "*": {
    "cancelreasonScript": {}
    }
    }
</script>

js file

require(['jquery', 'mage/url', 'Magento_Ui/js/modal/modal'], function (jQuery, url, modal) {

        jQuery(document).ready(function () {

            var options = {
                type: 'popup',
                responsive: true,
                innerScroll: true,
                title: 'Reason for Cancelled the item',

                buttons: [{
                        text: jQuery.mage.__('submit'),
                        class: '',
                        click: function () {
                            var cancelreason = jQuery("#reason").val();

                            var orderId = jQuery('#click').data('id');
                             var cancelurl = url.build("oxsales/index/index/");
                            jQuery.ajax({
                                url: cancelurl,
                                type: 'POST',
                                data: {'id': orderId, 'cancelreason': cancelreason},
                                success: function (response) {

                                    jQuery("#result").html("Cancel initiated for your request");
                                },
                                error: function (response) {
                                    jQuery("#result").html("Error");
                                }
                            });
                            var close = this;
                            setTimeout(function () {

                                close.closeModal();
                            }, 3000);
                        }
                    }]
            };

            jQuery(".click-me").on('click', function () {
               var popup = modal(options, jQuery('#popup-modal'));
                jQuery('#reason').val('');

                jQuery("#result").html('');

                jQuery("#popup-modal").modal("openModal");

            });
        }
        );

});                                  

1 Answer 1

0

Move whole var options block into onclick callback and inside use

$(this).data('id');

to get order's id;

3
  • No it is not working. still it gets the last order id 10
    – Jaisa
    Commented Nov 29, 2017 at 9:30
  • how to get the current id before model loads
    – Jaisa
    Commented Nov 29, 2017 at 9:36
  • yes, I got it through click event, but how to pass it, to popup modal?
    – Jaisa
    Commented Nov 29, 2017 at 9:53

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.