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(); 

    1 answer 1

    Apparently in the code starting with "foreach ($ collection as $ customer) {" at the end is missing the closing "}"

    Most likely the problem is how the $ collection is formed, since all the members of the collection receive the letter. You go through all the customers from the $ collection and send them an email using a template that depends on whether the member ID of the $ members collection is the same as $ bet-> getMemberId () or not. Those. if the users who are not approved by the administrator are added to the $ collection, they will still receive a message, since you do not have a check that only members of $ member receive mail.

    I also want to draw your attention to the code:

     $member = $members->getItemByColumnValue('customer_id', $customer->getId()); 

    And the code relating to the formation of the letter, you can make the condition so as not to produce entities. You can also add to the loop a check on the membership of the $ members collection of the $ collection, something like this will be released:

     /** @var Custom_Auction_Model_Member $member */ $member = $members->getItemByColumnValue('customer_id', $customer->getId()); if(!$member->getId()) { continue;} if($member->getId() != $bet->getMemberId()) { ... } else { ... } 

    But it is wiser to limit the $ collection collection at the preparation stage by adding filters that cut off users who are not approved by the administrator.

    $ templateId can be extracted from the database by referring to the Code field, not being tied strictly to the actual Id template

     $templateId = Mage::getModel('core/email_template')->loadByCode('Код')->getTemplateId(); 

    The codes for your template ов можно посмотреть admin in the "Transactional Emails" section or using the database query:

     SELECT template_id, template_code FROM core_email_template WHERE template_id IN (3,4); 

    It is convenient to declare statuses as constants in the model file:

     class Custom_Auction_Model_Member extends Mage_Core_Model_Abstract { const CUSTOMER_WIN_BET = 3; ... } 

    This is clearer. So it is easier then, after some time, to work in the logic of the module and what each status means. To record a status value, you can use the $ member-> setStatus short reference (Value) if you have nothing against magic methods :)

     }else{ $member->setStatus(Mage::getModel('custom_auction/member')::CUSTOMER_WIN_BET); $member->save(); $templateId = Mage::getModel('core/email_template')->loadByCode('TemplateCode')->getTemplateId(); 

    For what line:

     $customer->setData('name', $customer->getName()); 

    in the above code is not clear. You take the $ data ['name'] field from Data with the help of the $ customer-> getName () query and write it in $ data ['name'] in the same place. I guess this is a mistake.

    As a result, if we consider that the status is imposed by a constant in the model, and checking the membership of a member of the $ members collection all the same happens in a cycle, something like this will turn out:

     try{ foreach($collection as $customer){ /** @var Custom_Auction_Model_Member $member */ $member = $members->getItemByColumnValue('customer_id', $customer->getId()); if(!$member->getId()) { continue;} /** @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()); /** @var Mage_Core_Model_Email_Info $emailInfo */ $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()); if($member->getId() != $bet->getMemberId()) { /** @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(); $templateCode = 'TemplateCodeForId_3'; }else{ $member->setStatus(Mage::getModel('custom_auction/member')::CUSTOMER_WIN_BET); $member->save(); $memberBet = $bet; $templateCode = 'TemplateCodeForId_4'; } $mailer->setTemplateId(Mage::getModel('core/email_template')->loadByCode($templateCode)->getTemplateId()); $mailer->setTemplateParams(array( 'customer' => $customer, 'product' => $product, 'bet' => $memberBet ) ); $mailer->send(); } } catch (Exception $e) { Mage::logException($e); Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); } 

    Be careful! I did not test the code. Good luck with Magento!

    • Thank you very much for the answer. - Zoya