Magento 고객 그리드-마지막 주문 날짜
-
12-12-2019 - |
문제
나를 추가하려고 몇 가지 추가 열 젠토의 고객 그리드.나는 순수 성공적으로 작업(총 금액의 순서에는 고객가치)
나는 가려고 마지막 순서에서 날짜,그러나 나는 할 수 없습니다.는 사람이 이것을 전할 수 있는 누군가가 나에서 오른쪽 방향으로 어떻게 하면 이렇게 할 수 있습니까?
이것은 현재 내 코드,하지만 그것은 오류가 표시됩니다 SQL 쿼리가 있습니다.
$orderTableName = Mage::getSingleton('core/resource')
->getTableName('sales/order');
$collection->getSelect()
->joinLeft(
array('orders' => $orderTableName),
'orders.customer_id=e.entity_id',
array(
'order_count' => 'COUNT(customer_id)',
'last_order' => 'MAX(created_at)'
)
);
$collection->groupByAttribute('entity_id');
해결책
CE 1.8.1에 대한 작업 예제는 다음과 같습니다.
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
->addAttributeToSelect('email')
->addAttributeToSelect('created_at')
->addAttributeToSelect('group_id')
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');
// add 2 new fields as sub queries
$sql ='SELECT MAX(o.created_at)'
. ' FROM ' . Mage::getSingleton('core/resource')->getTableName('sales/order') . ' AS o'
. ' WHERE o.customer_id = e.entity_id ';
$expr = new Zend_Db_Expr('(' . $sql . ')');
$collection->getSelect()->from(null, array('last_order_date'=>$expr));
$sql ='SELECT COUNT(*)'
. ' FROM ' . Mage::getSingleton('core/resource')->getTableName('sales/order') . ' AS o'
. ' WHERE o.customer_id = e.entity_id ';
$expr = new Zend_Db_Expr('(' . $sql . ')');
$collection->getSelect()->from(null, array('orders_count'=>$expr));
//echo $collection->getSelect(); exit;
$this->setCollection($collection);
return parent::_prepareCollection();
}
.
및
protected function _prepareColumns()
{
$this->addColumn('entity_id', array(
'header' => Mage::helper('customer')->__('ID'),
'width' => '50px',
'index' => 'entity_id',
'type' => 'number',
));
$this->addColumn('last_order_date', array(
'header' => Mage::helper('customer')->__('Last Order Date'),
'type' => 'datetime',
'align' => 'center',
'index' => 'last_order_date',
'gmtoffset' => true,
));
$this->addColumn('orders_count', array(
'header' => Mage::helper('customer')->__('Orders Count'),
'index' => 'orders_count',
));
........
........
.
출력은
다른 팁
여기에서 내가 이것을 어떻게 할 것인지
백엔드에서만 볼 수있는 고객 엔티티에서 last_order_date
및 orders_count
라는 2 개의 특성을 생성합니다. 또는 전혀 보이지 않습니다. 이들을 추가 할 수있는 방법이 있습니다. .
이러한 속성의 값을 변경하는 checkout_submit_all_after
이벤트에 관찰자를 만듭니다.
이와 같은 것 :
public function checkoutSubmitAllAfter($observer) {
$order = $observer->getEvent()->getOrder();
if ($order) { //if on onepage checkout;
$customer = $order->getCustomer();
$lastOrderDate = $order->getCreatedAt();
$increment = 1;
}
else { //if on multishipping checkout
$orders = $observer->getEvent()->getOrders();
//get last order that's the important one;
$order = $orders[count($orders) - 1];
$customer = $order->getCustomer();
$lastOrderDate = $order->getCreatedAt();
$increment = count($orders);
}
if ($customer && $customer->getId()) { //check if the customer is logged in
//make sure you have a clean instance of the customer
//this line may not be needed. Test with it and without it.
$customer = Mage::getModel('customer/customer')->load($customer->getId());
//set the new values on the customer entity
$customer->setLastOrderDate($lastOrderDate);
$customer->setOrdersCount((int)$customer->getOrdersCount() + $increment);
$customer->save();
}
}
.
코드를 테스트하지 않았으므로 뭔가를 놓쳤을지도 모릅니다.
이제는 고객 객체에 필요한 값을 가지고 있으며 미친 조인 또는 정렬 순서가 필요없고 특별히 group by
가 필요하지 않습니다.
고객 그리드에 2 개의 새로운 열을 추가 할 수 있으며 쉽게 정렬 가능하고 필터링 할 수 있습니다.
DB에 이미 고객이있는 경우 기존 고객의 값을 설정하는 한 번만 스크립트가 필요합니다.
그러나이 값을 쉽게 식별 할 수 있습니다.
나 같은 "완전한"상태 만 필요하면 . ' AND o.status LIKE "complete" '
다음에 . ' WHERE o.customer_id = e.entity_id ';
를 추가하십시오.
또한 고객이 보낸 총 돈을 추가했습니다 (그래서 모든 것이 완전한 주문의 전체 주문)
$total_pricesql ='SELECT sum(o.base_grand_total)'
. ' FROM ' . Mage::getSingleton('core/resource')->getTableName('sales/order') . ' AS o'
. ' WHERE o.customer_id = e.entity_id ' . ' AND o.status LIKE "complete" ';
$expr = new Zend_Db_Expr('(' . $total_pricesql . ')');
$collection->getSelect()->from(null, array('total_price'=>$expr));
.
및 뒷부분의 동일한 파일의
$this->addColumn('total_price', array(
'header' => Mage::helper('sales')->__('Total'),
'index' => 'total_price',
'type' => 'number',
'filter' => false,
));
.
그래서 @ amasty의 첫 번째 코드를 변경하십시오 :
protected function _prepareCollection() {
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
->addAttributeToSelect('email')
->addAttributeToSelect('created_at')
->addAttributeToSelect('group_id')
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');
// add 3 new fields as sub queries
$sql ='SELECT MAX(o.created_at)'
. ' FROM ' . Mage::getSingleton('core/resource')->getTableName('sales/order') . ' AS o'
. ' WHERE o.customer_id = e.entity_id ' . ' AND o.status LIKE "complete" ';
$expr = new Zend_Db_Expr('(' . $sql . ')');
$collection->getSelect()->from(null, array('last_order_date'=>$expr));
$sql ='SELECT COUNT(*)'
. ' FROM ' . Mage::getSingleton('core/resource')->getTableName('sales/order') . ' AS o'
. ' WHERE o.customer_id = e.entity_id ' . ' AND o.status LIKE "complete" ';
$expr = new Zend_Db_Expr('(' . $sql . ')');
$collection->getSelect()->from(null, array('orders_count'=>$expr));
$total_pricesql ='SELECT sum(o.base_grand_total)'
. ' FROM ' . Mage::getSingleton('core/resource')->getTableName('sales/order') . ' AS o'
. ' WHERE o.customer_id = e.entity_id ' . ' AND o.status LIKE "complete" ';
$expr = new Zend_Db_Expr('(' . $total_pricesql . ')');
$collection->getSelect()->from(null, array('total_price'=>$expr));
//echo $collection->getSelect(); exit;
$this->setCollection($collection); return parent::_prepareCollection();}
.
및 두 번째 코드 :
protected function _prepareColumns() {
$this->addColumn('entity_id', array(
'header' => Mage::helper('customer')->__('ID'),
'width' => '50px',
'index' => 'entity_id',
'type' => 'number',
));
$this->addColumn('last_order_date', array(
'header' => Mage::helper('customer')->__('Last Order Date'),
'type' => 'datetime',
'align' => 'center',
'index' => 'last_order_date',
'gmtoffset' => true,
));
$this->addColumn('orders_count', array(
'header' => Mage::helper('customer')->__('Orders Count'),
'index' => 'orders_count',
));
$this->addColumn('total_price', array(
'header' => Mage::helper('sales')->__('Total'),
'index' => 'total_price',
'type' => 'number',
'filter' => false,
));
........
........
. 여기에 내 문제를 해결 필터와 주문,작업 _prepareCollection
:
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
->joinAttribute('billing_street', 'customer_address/street', 'default_billing', null, 'left')
->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
;
$sql = sprintf('SELECT MAX(o.created_at) AS created_at FROM %s AS o GROUP BY o.customer_id', Mage::getSingleton('core/resource')->getTableName('sales/order_grid'));
$expr = new Zend_Db_Expr('(' . $sql . ')');
$collection->joinTable(array('order' => 'sales/order_grid'), 'customer_id=entity_id', array('order_created_at' => 'created_at'), "order.created_at IN ($expr)", 'left');
다음 _prepareColumns
:
$this->addColumn('order_created_at', array(
'header' => Mage::helper('sales')->__('Purchased On'),
'type' => 'datetime',
'index' => 'order_created_at',
'gmtoffset' => true,
));