After having spent a good 2 days trying to educate myself in getting down and dirty with views, I'm hoping someone will show me what I'm currently not seeing... ;-)
I've created one additional table for use with ubercart (more specific coupon_purchase), which holds the combination of the order (oid), the product id (nid), the coupon id (cid).
I'm trying to get the following join-structure:
uc_coupons_products <- join oid on order_id -> uc_orders
uc_coupons_products <- join nid on nid -> uc_products
uc_coupons_products <- join cid on cid -> uc_coupons
Sofar I've gotten this far in hooks_views_data:
$data['uc_coupons_products']['table']['group'] = t('CUSTOM - Orders to Products to Coupons');
$data['uc_coupons_products']['table']['join'] = array(
'uc_coupons' => array(
'left_field' => 'cid',
'field' => 'cid',
),
'uc_order_products' => array(
'left_field' => 'nid',
'field' => 'nid',
),
'uc_orders' => array(
'left_field' => 'order_id',
'field' => 'oid',
)
);Which gives me a query (using order_id as an argument) like this:
SELECT uc_order_products.title AS uc_order_products_title,
uc_coupons_uc_coupons_products.code AS uc_coupons_uc_coupons_products_code,
uc_order_products.price AS uc_order_products_price,
uc_orders.primary_email AS uc_orders_primary_email,
uc_orders.billing_first_name AS uc_orders_billing_first_name,
uc_orders.billing_last_name AS uc_orders_billing_last_name,
uc_orders.delivery_first_name AS uc_orders_delivery_first_name,
uc_orders.delivery_last_name AS uc_orders_delivery_last_name,
uc_coupons_uc_coupons_products.cid AS uc_coupons_uc_coupons_products_cid
FROM uc_orders uc_orders
LEFT JOIN uc_coupons_products uc_coupons_products ON uc_orders.order_id = uc_coupons_products.oid
INNER JOIN uc_orders uc_orders_uc_coupons_products ON uc_coupons_products.oid = uc_orders_uc_coupons_products.order_id
INNER JOIN uc_coupons uc_coupons_uc_coupons_products ON uc_coupons_products.cid = uc_coupons_uc_coupons_products.cid
INNER JOIN uc_order_products uc_order_products_uc_coupons_products ON uc_coupons_products.nid = uc_order_products_uc_coupons_products.nid
LEFT JOIN uc_order_products uc_order_products ON uc_orders.order_id = uc_order_products.order_id
WHERE (uc_coupons_uc_coupons_products.status <> 0) AND (uc_orders.order_id = 53 )This gives me almost the desired results, but creates a row for each product and each coupon for the given order.
Simplifying the query, the desired query should look like this:
SELECT uc_order_products.title AS uc_order_products_title,
uc_coupons.cid AS uc_coupons_products_cid,
uc_coupons.code AS uc_coupons_code,
uc_order_products.price AS uc_order_products_price,
uc_orders.primary_email AS uc_orders_primary_email,
uc_orders.billing_first_name AS uc_orders_billing_first_name,
uc_orders.billing_last_name AS uc_orders_billing_last_name,
uc_orders.delivery_first_name AS uc_orders_delivery_first_name,
uc_orders.delivery_last_name AS uc_orders_delivery_last_name
FROM uc_orders uc_orders
INNER JOIN uc_order_products ON uc_order_products.order_id = uc_orders.order_id
INNER JOIN uc_coupons_products ON uc_coupons_products.nid = uc_order_products.nid
INNER JOIN uc_coupons ON uc_coupons.cid = uc_coupons_products.cid
WHERE (uc_orders.order_id = 53 )Where am I loosing the trail? It looks like a really small thing, but I'm stooped at the moment on where to look...