Skip to content

Instantly share code, notes, and snippets.

@GreatPotato
Last active January 4, 2016 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save GreatPotato/8600713 to your computer and use it in GitHub Desktop.
Save GreatPotato/8600713 to your computer and use it in GitHub Desktop.
Total sales for category custom column
<?php
public function subscribeEvents()
{
Backend::$events->addEvent('shop:onExtendCategoryModel', $this, 'extend_category_model');
}
public function extend_category_model($category, $context)
{
$total_sales = "SELECT IFNULL(count(shop_order_items.quantity)*shop_order_items.quantity, 0)
FROM shop_products_categories
LEFT JOIN shop_categories t1
ON shop_products_categories.shop_category_id = t1.id
LEFT JOIN shop_products
ON shop_products_categories.shop_product_id = shop_products.id
LEFT JOIN shop_order_items
ON shop_products.id = shop_order_items.shop_product_id
LEFT JOIN shop_orders
ON shop_order_items.shop_order_id = shop_orders.id
WHERE shop_products_categories.shop_category_id = shop_categories.id
AND shop_orders.payment_processed IS NOT NULL
";
$category->define_column('total_sales', 'Total sales');
$category->calculated_columns['total_sales'] = array(
'sql' => $total_sales,
'type' => db_number
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment