There is an auction for Magento. At the end of the auction, users who took part in the auction receive an email about the results. The winner - one template, those who lost - the other.
Not all users who submitted an application take part in the auction - only those who have been confirmed by the administrator. And the problem is that the letter that the user has lost, comes even to those who have not been admitted to the auction.
Here is the code responsible for sending these emails:
foreach($collection as $customer){ $customer->setData('name', $customer->getName()); if($members->getItemByColumnValue('customer_id', $customer->getId())->getId() != $bet->getMemberId()) { /** @var Custom_Auction_Model_Member $member */ $member = $members->getItemByColumnValue('customer_id', $customer->getId()); /** @var Custom_Auction_Model_Resource_Bet_Collection $memberBetCollection */ $memberBetCollection = Mage::getResourceModel('custom_auction/bet_collection'); $memberBetCollection->addFieldToFilter('member_id', $member->getId()); $memberBetCollection->setOrder('bet_id'); $memberBet = $memberBetCollection->getFirstItem(); $templateId = 3; /** @var Mage_Core_Model_Email_Template_Mailer $mailer */ $mailer = Mage::getModel('core/email_template_mailer'); /** @var Mage_Core_Model_Email_Info $emailInfo */ $emailInfo = Mage::getModel('core/email_info'); $emailInfo->addTo($customer->getData('email'), $customer->getName()); $mailer->addEmailInfo($emailInfo); // Set all required params and send emails $mailer->setSender(Mage::getStoreConfig(Mage_Sales_Model_Order_Invoice::XML_PATH_EMAIL_IDENTITY, Mage::app()->getStore()->getId())); $mailer->setStoreId(Mage::app()->getStore()->getId()); $mailer->setTemplateId($templateId); $mailer->setTemplateParams(array( 'customer' => $customer, 'product' => $product, 'bet' => $memberBet ) ); $mailer->send(); }else{ /** @var Custom_Auction_Model_Member $member */ $member = $members->getItemByColumnValue('customer_id', $customer->getId()); $member->setData('status', 3); $member->save(); $templateId = 4; /** @var Mage_Core_Model_Email_Template_Mailer $mailer */ $mailer = Mage::getModel('core/email_template_mailer'); /** @var Mage_Core_Model_Email_Info $emailInfo */ $emailInfo = Mage::getModel('core/email_info'); $emailInfo->addTo($customer->getData('email'), $customer->getName()); $mailer->addEmailInfo($emailInfo); // Set all required params and send emails $mailer->setSender(Mage::getStoreConfig(Mage_Sales_Model_Order_Invoice::XML_PATH_EMAIL_IDENTITY, Mage::app()->getStore()->getId())); $mailer->setStoreId(Mage::app()->getStore()->getId()); $mailer->setTemplateId($templateId); $mailer->setTemplateParams(array( 'customer' => $customer, 'product' => $product, 'bet' => $bet ) ); $mailer->send(); }
I understand that this part of the code selects the winner (participant with status 3) and sends him a letter of the winner (template with id 3).
$member = $members->getItemByColumnValue('customer_id', $customer->getId()); $member->setData('status', 3); $member->save(); $templateId = 4;
Is it possible to also sample for losers? Type:
/** @var Custom_Auction_Model_Member $member */ $member = $members->getItemByColumnValue('customer_id', $customer->getId()); $member->setData('status', 2); $member->save(); /** @var Custom_Auction_Model_Resource_Bet_Collection $memberBetCollection */ $memberBetCollection = Mage::getResourceModel('custom_auction/bet_collection'); $memberBetCollection->addFieldToFilter('member_id', $member->getId()); $memberBetCollection->setOrder('bet_id'); $memberBet = $memberBetCollection->getFirstItem();