diff --git a/commerce.module b/commerce.module index 1b24ca7..4803dee 100644 --- a/commerce.module +++ b/commerce.module @@ -1063,6 +1063,7 @@ function commerce_entity_access_query_alter($query, $entity_type, $base_table = // in an invalid query if the first table is not the table we expect, // forcing the caller to actually properly pass a base table in that case. $tables = &$query->getTables(); + reset($tables); $base_table = key($tables); } diff --git a/modules/line_item/commerce_line_item.module b/modules/line_item/commerce_line_item.module index 557087e..f37b6d2 100644 --- a/modules/line_item/commerce_line_item.module +++ b/modules/line_item/commerce_line_item.module @@ -708,8 +708,21 @@ function commerce_line_item_query_commerce_line_item_access_alter(QueryAlterable // Join the line items to their orders. if (module_exists('commerce_order')) { $tables = &$query->getTables(); - $base_table = key($tables); - $order_alias = $query->innerJoin('commerce_order', 'co', '%alias.order_id = ' . $base_table . '.order_id'); + + // Look for an existing commerce_order table. + foreach ($tables as $table) { + if ($table['table'] === 'commerce_order') { + $order_alias = $table['alias']; + break; + } + } + + // If not found, attempt a join against the first table. + if (!isset($order_alias)) { + reset($tables); + $base_table = key($tables); + $order_alias = $query->innerJoin('commerce_order', 'co', '%alias.order_id = ' . $base_table . '.order_id'); + } // Perform the access control on the order. commerce_entity_access_query_alter($query, 'commerce_order', $order_alias); diff --git a/modules/payment/commerce_payment.module b/modules/payment/commerce_payment.module index 335fc75..634c203 100644 --- a/modules/payment/commerce_payment.module +++ b/modules/payment/commerce_payment.module @@ -997,8 +997,21 @@ function commerce_payment_query_commerce_payment_transaction_access_alter(QueryA // Join the payment transaction to their orders. if (user_access('view payments', $account)) { $tables = &$query->getTables(); - $base_table = key($tables); - $order_alias = $query->innerJoin('commerce_order', 'co', '%alias.order_id = ' . $base_table . '.order_id'); + + // Look for an existing commerce_order table. + foreach ($tables as $table) { + if ($table['table'] === 'commerce_order') { + $order_alias = $table['alias']; + break; + } + } + + // If not found, attempt a join against the first table. + if (!isset($order_alias)) { + reset($tables); + $base_table = key($tables); + $order_alias = $query->innerJoin('commerce_order', 'co', '%alias.order_id = ' . $base_table . '.order_id'); + } // Perform the access control on the order. commerce_entity_access_query_alter($query, 'commerce_order', $order_alias);