1

I'm trying to build a plugin to get customer order and customer lifetime value.

This is my code so far:

require 'vendor/autoload.php';
use HelpScoutApp\DynamicApp;
use Illuminate\Database\Eloquent\Model as Model;
$app = new DynamicApp('HELPSCOUT-SECRET-KEY');
if ($app->isSignatureValid())
{
  $customer = $app->getCustomer();
  $user     = $app->getUser();
  $convo    = $app->getConversation();

  $html = array();
  $html[] = 'Orders';
  $html[] = '';
  $settings = array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'magento',
    'username'  => 'root',
    'password'  => 'topsecret',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
  );
  // This is far more readable on newer versions of Illuminate (with PHP 5.4 and above)
  $connFactory = new \Illuminate\Database\Connectors\ConnectionFactory(new Illuminate\Container\Container);
  $conn = $connFactory->make($settings);
  $resolver = new \Illuminate\Database\ConnectionResolver();
  $resolver->addConnection('default', $conn);
  $resolver->setDefaultConnection('default');
  \Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);

  class Order extends Model {
    public $table = 'sales_flat_order'; 
    public $timestamps = false;
  }
  $orders = Order::whereIn('customer_email', $customer->getEmails())->orderBy('entity_id', 'desc')->take(5)->get();
  foreach($orders as $order)
  {
    $html[] = '<li><a href="http://example.com/index.php/admin/sales_order/view/order_id/'.$order->entity_id.'/" target="_blank">#'.$order->increment_id.'</a>, '.number_format($order->base_grand_total, 2).' '.$order->base_currency_code .'</li>';
  }
  $html[] = '</ul>';
  echo $app->getResponse($html);
}
else
{
  echo 'Invalid Request';
}

Orders are OK, I need to get customer lifetime from the sales_flat_order table. Any idea how to get this?

1

1 Answer 1

1

A better way to accomplish what you are trying to do would be

//get any third party information you need
//customer = $app->getCustomer();

require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();

// Now you can run ANY Magento code you want

$orderCollection = Mage::getModel('sales/order')->getCollection();
//$orderCollection->addFieldToFilter('customer_email', $email);
$orderCollection->addFieldToFilter('customer_id', $id);

foreach ($orderCollection as $_order)
{

}

See

3
  • 1
    only suggeted frinds:-SELECT DISTINCT customer_email, customer_firstname, customer_lastname, SUM(subtotal_invoiced) AS Total FROM sales_flat_order AS a GROUP BY customer_email ORDER BY SUM(subtotal_invoiced) DESC Commented Jul 1, 2015 at 13:42
  • If I use your aproach, the file should be inside magento root? The thing is that help scout require a secure server and my magento instance is not secure, no ssl. So I've build this app outside of magento, on a secure server and that's why I've used this aproach.
    – solitarul
    Commented Jul 1, 2015 at 15:43
  • To get lifetime sales you can either use php to sum up all order or query the database to sum it up. See stackoverflow.com/questions/7168137/… Commented Jul 1, 2015 at 15:47

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.