2

I would like to create an if with php where I assign an action if the current customer registered on my site in magento does not place an order after 6 months from his last order.

if(customer not place an order in six months){
//action
} else { //ok }

How can I do?

this is my try:

Sure: $_customer = Mage::getSingleton('customer/session')->getCustomer(); $orders = Mage::getResourceModel('sales/order_collection') ->addFieldToSelect('*') ->addFieldToFilter('customer_id', $_customer->getId()) ->addAttributeToSort('created_at', 'DESC') ->setPageSize(1);

    $date_order_creation=$orders->getFirstItem()->getCreatedAt();
    $previous_date = date('Y-m-d');

    $date_a = new DateTime($date_order_creation);
    $date_b = new DateTime($previous_date);
    $interval = date_diff($date_a,$date_b);

    if($interval->format('%d') > 1){


        echo 'more 1 day';
    }else{

        echo 'not yet 1 day';
    }

0

1 Answer 1

1

you can try this code :

<?php
$_customer = Mage::getSingleton('customer/session')->getCustomer();

$orders = Mage::getResourceModel('sales/order_collection')
    ->addFieldToSelect('*')
    ->addFieldToFilter('customer_id', $_customer->getId())
    ->addAttributeToSort('created_at', 'DESC')
    ->setPageSize(1);

$date_order_creation=$orders->getFirstItem()->getCreatedAt();
$date_now = date('Y-m-d');

$date_a = new DateTime($date_order_creation);
$date_b = new DateTime($date_now);
$interval = date_diff($date_b,$date_a);

if($interval->format('%m') >=6){
    echo '6 months from this order';
}else{

    echo 'not yet 6 months';
}

?>

Tell me if it works. Happy Coding !!!

12
  • Hi sami. i testing in this moment your solution. I'll let you know as soon as possible. Thanks for the help
    – Gabriel91
    Aug 2, 2018 at 10:58
  • hey @Gabriel91 good luck :D
    – sami23
    Aug 2, 2018 at 12:46
  • @sami23 just one improvement suggestion: Magento's timezone (from the db: $order->getCreatedAt()) could be different than php's (date function). You could use magento's functions to calculate the time difference (even if a few hours are nothing in front of 6 months).
    – Fra H
    Aug 2, 2018 at 14:21
  • @FraH yes you are right ;)
    – sami23
    Aug 2, 2018 at 17:26
  • Hi guys! I finished trying your function. Only a question, this function takes the date of the last order placed by the right customer? Because at the moment I tried to run the code like this: if ($ interval-> format ('% d') == 1) { echo 'order by 1 day'; } Else { echo 'not yet 1 day'; } but despite the last order has already passed more than a day, the php continues to take the phrase 'not yet 1 day'. Is it because of the time problem in the php? How can I do more precisely?
    – Gabriel91
    Aug 3, 2018 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.