cancel
Showing results for 
Search instead for 
Did you mean: 

Magento PHP GiftCards Extension - sending email GC every minute

Magento PHP GiftCards Extension - sending email GC every minute

I'm trying to implement a Gift Card extension in Magento. It is this extension: https://www.webtexsoftware.com/gift-cards-magento-extension

 

The problem that I'm having is that I've enabled the Email Delivery Date field, but what happens is that when that date comes, it won't stop sending emails. It sends 1 email every minute!

 

I do have a cronjob that runs every minute to look for gift certs that have to be sent, but that should not be causing this issue, to my knowledge.

 

I've tried everything I can think of to ensure that this doesn't run twice on the same gift card, but it somehow does. Any ideas? I do want this to run every minute to make sure I'm getting every GC sent out as quickly as possible, but what I don't want is to re-send every GC every minute.

 

This is the PHP file that my cronjob calls every minute:

 

	<?php
	ini_set('memory_limit', '128M');
	define('MAGENTO_ROOT', getcwd());

	// exit;

	$time = time();
	$time2 = $time - 0;
	$to = date('Y-m-d H:i:s', $time2);
	$lastTime = $time - 120; //3600; // Orders from the past half hour
	$from = date('Y-m-d H:i:s', $lastTime);
	$mageFilename = ''; //removed for security
	require_once $mageFilename;

	Mage::app();
	$order_collection = Mage::getResourceModel('sales/order_collection');

	// $order_collection->addFieldToFilter('status', 'pending')

	$order_collection->addAttributeToSelect('*')->addAttributeToFilter('updated_at', array(
		'from' => $from,
		'to' => $to
	))

	// ->addAttributeToFilter('updated_at', array('from' => $from, 'to' => $to))

	->getSelect();
	print count($order_collection);

	foreach($order_collection->getItems() as $order) {
		$cards = Mage::getModel('giftcards/giftcards')->getCollection()->addFieldToFilter('order_id', $order->getId());
		print count($cards);
		foreach($cards as $card) {
			if (($card->getCardStatus() == 0) && ($curDate == $card->getMailDeliveryDate()))  {
					$card->setCardStatus(1)->save();
					if ((($card->getMailDeliveryDate() == null) || ($curDate == $card->getMailDeliveryDate())) && $card->getCardType() != 'offline') {
						$card->send();
					}
				}
			}
		}
	?>

 

9 REPLIES

Re: Magento PHP GiftCards Extension - sending email GC every minute

Just a thought. But any reason not to have the cronjob on fire once a day and have the date exclude hours and minutes, or is it needed to run minutely?

Re: Magento PHP GiftCards Extension - sending email GC every minute


elfling wrote:

Just a thought. But any reason not to have the cronjob on fire once a day and have the date exclude hours and minutes, or is it needed to run minutely?


Good afternoon elfling,

 

Yeah, I'm running the cronjob every minute so as soon as someone places an order for a GC, they'll get their email containing the GC very quickly.

 

However, what I'm trying to take into account here, is if the Delivery Date is selected for let's say tomorrow. Tomorrow rolls around, and at 12:01 you start receiving emails every single minute, which is exceptionally weird. So I'm looking for a way to ensure that doesn't happen.

 

Thank you.

Re: Magento PHP GiftCards Extension - sending email GC every minute

Since Magento 1.9x uses a Mail Queue, when the order is placed could you not add the giftcard email to the queue with the date specified?

 

Just trying to find a solution that would be a bit more practical is all

Re: Magento PHP GiftCards Extension - sending email GC every minute

Ok, rather than chatting about what could be done completely differently.

 

I would add int column gc_sent to the sales_flat_order table with default zero

 

Clear Magento store cache

 

Add the following filter to your collection

 

->AddFieldToFilter('gc_sent', false)

 

Then update the field to

$order->setGcSent(true)->save(); 

 

That way there is a flag on the order to say the the email has been sent already

 

Re: Magento PHP GiftCards Extension - sending email GC every minute


elfling wrote:

Ok, rather than chatting about what could be done completely differently.

 

I would add int column gc_sent to the sales_flat_order table with default zero

 

Clear Magento store cache

 

Add the following filter to your collection

 

->AddFieldToFilter('gc_sent', false)

 

Then update the field to

$order->setGcSent(true)->save(); 

 

That way there is a flag on the order to say the the email has been sent already

 


Just tried it and that did not work. I added the field to the table but it's still set to 0 and I'm still receiving emails every minute.

 

gcs.png

 

<?php
ini_set('memory_limit', '128M');
define('MAGENTO_ROOT', getcwd());

// exit;

$time = time();
$time2 = $time - 0;
$to = date('Y-m-d H:i:s', $time2);
$lastTime = $time - 120; //3600; // Orders from the past half hour
$from = date('Y-m-d H:i:s', $lastTime);
$mageFilename = 'app/Mage.php';
require_once $mageFilename;

Mage::app();
$order_collection = Mage::getResourceModel('sales/order_collection');

// $order_collection->addFieldToFilter('status', 'pending')

$order_collection->addAttributeToSelect('*')->addAttributeToFilter('updated_at', array(
	'from' => $from,
	'to' => $to
))

// ->addAttributeToFilter('updated_at', array('from' => $from, 'to' => $to))

->getSelect();
print count($order_collection);

foreach($order_collection->getItems() as $order) {
	$cards = Mage::getModel('giftcards/giftcards')->getCollection()
		->addFieldToFilter('order_id', $order->getId())
		->addFieldToFilter('gc_sent', false);
	print count($cards);
	foreach($cards as $card) {
		if (($card->getCardStatus() == 0) && ($curDate == $card->getMailDeliveryDate()))  {
				$card->setCardStatus(1)->save();
				if ((($card->getMailDeliveryDate() == null) || ($curDate == $card->getMailDeliveryDate())) && $card->getCardType() != 'offline') {
					$order->setGcSent(true)->save();
					$card->send();					
				}
			}
		}
	}
?>

Re: Magento PHP GiftCards Extension - sending email GC every minute

In the database, has a it updated the order column gc_sent to have a value of 1? If so then I’ll send an updated version of the filter

Re: Magento PHP GiftCards Extension - sending email GC every minute


elfling wrote:
In the database, has a it updated the order column gc_sent to have a value of 1? If so then I’ll send an updated version of the filter

Yes, it updated the database, but still kept sending email after email.

Re: Magento PHP GiftCards Extension - sending email GC every minute

Try updating the filter as follows and it should filter correctly.

 

->addAttributeToFilter('gc_sent', array('in' => '0'))

 

Re: Magento PHP GiftCards Extension - sending email GC every minute

All fixed?