Comments

rszrama’s picture

Status: Needs review » Needs work

This is a great idea, but I think we should simplify it so the new function just returns TRUE or FALSE. It's checking for a specific state in the application, and making it also function as an order loader complicates things. There's no performance hit to using commerce_cart_order_load() after the empty check since it's going to be cached anyways.

FranciscoLuz’s picture

Status: Needs work » Needs review
StatusFileSize
new2.17 KB

Cheers Ryan!

Here it is, a new patch.

joelpittet’s picture

+++ b/modules/cart/commerce_cart.module
@@ -655,26 +655,43 @@ function commerce_cart_block_view($delta) {
+    if (!commerce_cart_is_cart_empty()) {
+      $order = commerce_cart_order_load($user->uid);

To avoid calling commerce_cart_order_load() twice here, maybe consider passing it in as an optional $order param?

joelpittet’s picture

Status: Needs review » Needs work
FranciscoLuz’s picture

Status: Needs work » Needs review
StatusFileSize
new2.44 KB

Added suggestion from #3.

joelpittet’s picture

Thanks for considering my feedback @FranciscoLuz, though it looks like something went sideways on that patch:

+++ b/modules/cart/commerce_cart.module
@@ -654,28 +654,48 @@ function commerce_cart_block_view($delta) {
+		'order' => $order,
+		'contents_view' => commerce_embed_view('commerce_cart_block', 'default', array($order->order_id), $_GET['q']),
+	  );
+  ¶

May be just whitespace issues but it's got a mix of tabs and spaces.

Could you provide an interdiff between the two patches with your changes? They help people reviewing to see the changes between the two so they don't have to manually compare on each update. @see https://www.drupal.org/documentation/git/interdiff

joelpittet’s picture

Status: Needs review » Needs work
mglaman’s picture

Going to say, also, needs work because it has tabs and coding standards issues. FranciscoLuz, check out https://www.drupal.org/coding-standards

FranciscoLuz’s picture

Status: Needs work » Needs review

@mglaman please point out the issue. Simply handing over a pages long manual does not help.

mglaman’s picture

Status: Needs review » Needs work
  1. +++ b/modules/cart/commerce_cart.module
    @@ -654,28 +654,48 @@ function commerce_cart_block_view($delta) {
    +	$order = commerce_cart_order_load($user->uid);
    +	// If there are one or more products in the cart...
    +	if (!commerce_cart_is_cart_empty($order)) {
    +	  // Build the variables array to send to the cart block template.
    +	  $variables = array(
    +		'order' => $order,
    +		'contents_view' => commerce_embed_view('commerce_cart_block', 'default', array($order->order_id), $_GET['q']),
    +	  );
    +  ¶
    +	  $content = theme('commerce_cart_block', $variables);
    +	}
    

    Tabs not spaces.

  2. +++ b/modules/cart/commerce_cart.module
    @@ -654,28 +654,48 @@ function commerce_cart_block_view($delta) {
    +    return array('subject' => t('Shopping cart'), 'content' => $content);
    

    Array items should be on own line

EDIT:

+++ b/modules/cart/commerce_cart.module
@@ -654,28 +654,48 @@ function commerce_cart_block_view($delta) {
+		'contents_view' => commerce_embed_view('commerce_cart_block', 'default', array($order->order_id), $_GET['q']),

Also, why not current_path()

mglaman’s picture

StatusFileSize
new2.06 KB

Attaching interdiff for review. FranciscoLuz, I didn't mean offense, just that it was hard to review the diff because of tabs. Far too used to using Dreditor (https://dreditor.org/) to review patch diffs that I forgot it isn't quite as highlighted when viewing as regular text.

FranciscoLuz’s picture

I did not take it as an offence bro. Seriously, I am good with your comments. I am sorry if I sounded annoyed, I really am not.

On the current_path() issue. It makes totally sense but $_GET['q'] was not being introduced by my patch. Cheers.

iqbalpk’s picture

vadym.kononenko’s picture

Version: 7.x-1.x-dev » 7.x-1.11
Status: Needs work » Needs review
StatusFileSize
new1.62 KB
new2.33 KB

Re-made patch from #3. Made changes as minimal as possible. Patch respects drupal coding standards as well.
hook_alter() has been added to provide ability to make result management flexible.

Status: Needs review » Needs work

The last submitted patch, 14: interdiff--2372223--3-14.patch, failed testing.

joelpittet’s picture

Version: 7.x-1.11 » 7.x-1.x-dev
Status: Needs work » Needs review

Thanks @vadym.kononenko. Setting back to -dev as that is where the development happens for the next release.

hanskuiters’s picture

Status: Needs review » Reviewed & tested by the community

Nice patch, just what I needed. I use it to alter the body class.

I placed the code below in my theme's template.php so I can show or hide the cart menu item I added to my menu.

/**
 * Implements hook_preprocess_html().
 */
function THEME_preprocess_html(&$vars) {
  if (function_exists('commerce_cart_is_cart_empty') && !commerce_cart_is_cart_empty()) {
    $vars['classes_array'][] = 'cart-full';
  }
  else {
    $vars['classes_array'][] = 'cart-empty';
  }
}

dieuwe’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new1.63 KB

When testing the patch, I found that if a user does not have a cart the function still returns FALSE (meaning: cart is not empty).

I've set the default value of $is_empty to TRUE so that we get nicer behaviour.

hanskuiters’s picture

Status: Needs review » Reviewed & tested by the community

That is nice. I had an inconcistancy, but not the time yet to look into it. Thanks, this works fine now.

joelpittet’s picture

Status: Reviewed & tested by the community » Needs work

This is looking great, one last thing: there are 4 places in the commerce modules that check for emptiness. It would be nice if this were used in all those places:

modules/cart/commerce_cart.module:
  658  
  659        // If there are one or more products in the cart...
  660:       if (commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types()) > 0) {
  661  
  662          // Build the variables array to send to the cart block template.

modules/cart/includes/commerce_cart.pages.inc:
   40  
   41      // Only show the cart form if we found product line items.
   42:     if (commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types()) > 0) {
   43  
   44        // Add the form for editing the cart contents.

modules/checkout/commerce_checkout.api.php:
   60    $wrapper = entity_metadata_wrapper('commerce_order', $order);
   61  
   62:   if (commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types()) > 0) {
   63      return TRUE;
   64    }

modules/product_reference/commerce_product_reference.module:
 1430    $wrapper = entity_metadata_wrapper('commerce_order', $order);
 1431  
 1432:   if (commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types()) > 0) {
 1433      return TRUE;
 1434    }
pinueve’s picture

joelpittet’s picture

@pinueve thoughts on #20?

subhojit777’s picture

StatusFileSize
new1.74 KB

rerolling

subhojit777’s picture

Status: Needs work » Needs review
StatusFileSize
new4.82 KB
new3.08 KB
millionleaves’s picture

#24 did the trick for me after upgrading to Commerce 7.x-1.14 from 7.x-1.13. I don't recall which version I'd applied to 7.x-1.13, but am guessing it was #18.

joelpittet’s picture

@millionleaves feel free to update the status to RTBC. I don't have a project to test this out at the moment but the interdiff looks exactly what I asked for in #20

millionleaves’s picture

Status: Needs review » Reviewed & tested by the community

@joelpittet I don't see "RTBC" as a status option, but have marked it Reviewed and tested by the community

joelpittet’s picture

Sorry, you picked the right one, I got lazy and used the shorthand. Thank you for updating it!
https://www.drupal.org/node/156119#rtbc

torgospizza’s picture

Confirmed that this function worked for me and is something we were in need of. Many thanks!

mglaman’s picture

Status: Reviewed & tested by the community » Needs work

There is an edge case and performance fix.

  1. +++ b/modules/cart/commerce_cart.module
    @@ -693,7 +691,34 @@ function commerce_cart_block_view($delta) {
    +  $order = (is_null($order)) ? commerce_cart_order_load($user->uid) : $order;
    

    Is there a chance that FALSE could be passed and then this breaks?

    I'm pretty sure entity_load() uses reset($entities) which returns FALSE. So there is a chance this could go sideways.

    I don't see any added tests to provide a way to confirm my suspicion or deny it.

  2. +++ b/modules/cart/commerce_cart.module
    @@ -693,7 +691,34 @@ function commerce_cart_block_view($delta) {
    +    $wrapper = entity_metadata_wrapper('commerce_order', $order);
    +
    +    // If cart order has 0 line items it is empty.
    +    $is_empty = commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types()) === 0;
    

    Let's not use entity metadata wrappers here. They're a performance hog and commerce_line_items_quantity converts a wrapper to entity value anyways.

    We can use field_get_items and pass the item IDs to commerce_line_item_load_multiple() and pass that result.

torgospizza’s picture

Status: Needs work » Needs review
StatusFileSize
new5.03 KB
new1.23 KB

New patch as per @mglaman's feedback, with interdiff.

torgospizza’s picture

StatusFileSize
new5.11 KB
new1.31 KB

Whoops, somehow removed my call to field_get_items().

New patch and interdiff attached.

mglaman’s picture

Issue tags: +Needs tests
+++ b/modules/cart/commerce_cart.module
@@ -693,7 +691,44 @@ function commerce_cart_block_view($delta) {
+    if (!empty($line_items) && is_array($line_items)) {
+      foreach ($line_items as $delta => $item) {
+        $line_item_ids[] = $item['line_item_id'];
+      }

:) We never end up fetching the line items, the field_get_items seems to be missing.

That's why we also need a test.

EDIT: :D patch rolled

The last submitted patch, 31: 2372223-commerce_cart_is_empty-31.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

torgospizza’s picture

StatusFileSize
new6.93 KB
new3.13 KB

New patch with tests. Please take a look and let me know if I'm doing it wrong :)

mglaman’s picture

Issue tags: -Needs tests
+++ b/modules/cart/tests/commerce_cart.test
@@ -166,6 +170,11 @@ class CommerceCartTestCaseSimpleProduct extends CommerceCartTestCase {
+    $this->assertTrue($is_empty === TRUE, t('Cart is verified as empty.'));

@@ -432,6 +441,10 @@ class CommerceCartTestCaseAttributes extends CommerceCartTestCase {
+    $this->assertTrue($is_empty === FALSE, t('Cart is verified as containing products.'));

Could be just assertTrue($is_empty) :) but thanks for adding the test!