I have made a function to display all customers on a modal popup. This is working fine. The problem is it loads customers even the link to the modal popup is not clicked. Which means this feature is slowing my page load time.
Is it possible to first load all customers when Modal popup opens?
This is my .phtml
<?php
$customerBlock = $block->getLayout()->createBlock('Ecomwise\Salesagents\Block\Account\Yourcustomers');
?>
<?php $customers = $customerBlock->getCollection(); ?>
<?php $customer_identity_take_over_permission = $customerBlock->getCustomerIdentityTakeOverPermission(); ?>
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerRepository = $objectManager->get('Magento\Customer\Api\CustomerRepositoryInterface');
?>
<div class="my-account copy-cart-customers" id="yourcustomers">
<div class="salesagents-pager"><?php //echo $customerBlock->getPagerHtml(); ?></div>
<?php if ($customers->getSize() > 0) { ?>
<table class="data-table copy-cart-customers-table" id="your-customers-table">
<col width="5%" />
<col width="35%" />
<col width="25%" />
<col width="35%" />
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>-</th>
<th>-</th>
<th>email</th>
<th></th>
</tr>
</thead>
<tbody>
<?php $_odd = ''; ?>
<?php foreach ($customers as $customer) { ?>
<tr>
<td>
<?php //echo "#" . $customer->getEntityId(); ?>
<?php
$customer_rep = $customerRepository->getById($customer->getId());
?>
</td>
<td>
<?php
$billingID = $customer->getDefaultBilling();
$address = $objectManager->create('Magento\Customer\Model\Address')->load($billingID);
$customer_companyname = "";
$customer_city = "";
if($address->getData()) :
$customer_address = $address->getData();
$customer_companyname = $customer_address['company'];
$customer_city = $customer_address['city'];
endif;
?>
<?php echo $customer_companyname; ?>
</td>
<td><?php echo $customer->getFirstname(); ?></td>
<td><?= $customer_city; ?></td>
<td>
<?php
$customer_email = ($customer_rep->getCustomAttribute('customer_email')) ? $customer_rep->getCustomAttribute('customer_email')->getValue() : "";
if($customer_email) {
echo $customer_email . " ";
}else{
echo $customer->getEmail();
}
?>
</td>
<td class="last">
<?php //if ($customer_identity_take_over_permission) { ?>
<button class="button copycart_button" title="<?php echo __('Copy cart'); ?>" type="button">
<span>
<span><a id="<?php echo $customer->getEntityId();?>" href="<?php echo $this->getUrl('copycart/copycart/copy',['_query' => ['customerid' => $customer->getEntityId()]]);?>"><?php echo __('Copy cart to this customer'); ?></a></span>
</span>
</button>
<?php //} ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } else { ?>
<p><?php echo __('You have no customers.'); ?></p>
<?php } ?>
</div>
<script>
require(
[
'jquery',
'Magento_Ui/js/modal/modal'
],
function(
$,
modal
) {
$('.hidden').hide();
var options = {
type: 'popup',
responsive: true,
innerScroll: true,
modalClass: 'copy-cart-customers-modal',
title: $.mage.__('Choose a customer to copy basket to'),//'Vælg sortiment',
buttons: [{
text: $.mage.__('Close'),
class: '',
click: function () {
this.closeModal();
}
}]
};
var popup = modal(options, $('#yourcustomers'));
$(".duplicate").on('click',function(){
$('#yourcustomers').modal('openModal');
cartid = $(this).children('span').prop("id");
});
$(".copycart_button").on('click',function(){
customerid = $(this).children('span').children('span').children('a').prop("id");
copycart_url = "<?php echo $this->getUrl('copycart/copycart/copy') ?>" + "?cartid=" + cartid + "&customerid=" + customerid;
$(this).children('span').children('span').children('a').attr("href", copycart_url);
$(this).children('span').children('span').children('a').click();
// alert($(this).children('span').children('span').children('a').prop("id"));
//$('#yourcustomers').modal('closeModal');
});
}
);
</script>