I describe my situation:

I created a PHP script in the header.phtml file that allows customers to change the group based on their order total. I put this script inside an if and it works perfectly.

In addition to this function, I added the sending of a personalized email that informs the customer of the group change.

The problem is that this PHP code that saves the client group and sends the email executes it every time the page of the site is loaded. I can't put this code online because otherwise the user will receive an email with a group change every time he loads a page.

This is a piece of the code I put in:

$customer = Mage::getSingleton('customer/session')->getCustomer();
$collectionOrders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_email',$email)->addAttributeToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate));
foreach($collectionOrders as $orders){
        $subTotal += $orders->getSubtotal();
$localeCode = Mage::getStoreConfig('general/locale/code', Mage::app()->getStore()->getStoreId());
if($subTotal >= 1000){

    $localeCode = Mage::getStoreConfig('general/locale/code', Mage::app()->getStore()->getStoreId());
    $emailTemplate = Mage::getModel('core/email_template')->loadDefault('customer_status_template',$localeCode);
    $emailTemplateVariables = array('customer_status' => ''.$titleStatus.'','title_status' => ''.$titleStatus.'','customer_status_year' => ''.$customerStatusYear.'');
    $processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
    $emailTemplate->send($email, $name, $emailTemplateVariables);

I would like the change of the group and the sending of the email to be carried out ONLY ONCE, only when the client group is changed for the first time and not every time I load a page of the site.

How would I go about running my php code just once?

  • could you please add code for $customer & $subTotal You used ? – Pawan Oct 25 at 17:03
  • Man, i added this codes now... – Gabriel91 Oct 28 at 8:21
  • You should build an observer after order place. Then check this customer's orders, decide the group id that should have, check if it has different group id, if not change its group and send the email. Generally header phtml is not the way to add that type of scripts. – pontikos Nov 3 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.