Drupalien Hello!

I hit a small problem with my drupal trade!

I created a menu with the API of drupal, the items are "registration" "connection" "Shopping Cart".

My goal is to add the number of items in basket a side of the menu title "Shopping Cart" to have made ​​this (3) Shopping Cart.

Someone has a track or a specific method please?

Thank you all.

Comments

rszrama’s picture

Status: Active » Fixed

There's already a menu item for this in the Cart module, you just need to enable it. : )

woprrr’s picture

Oh! seriously?! ca or I can not find?!

woprrr’s picture

Priority: Major » Minor
Status: Fixed » Active
rszrama’s picture

Status: Active » Fixed

It's a disabled link in your Navigation menu. You'll see it; just need to enable it.

woprrr’s picture

OH MY GOD I don't look this >_< ! thanks and sorry ;)

rszrama’s picture

lol No worries. It's kinda hidden away in there. : P

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Status: Closed (fixed) » Active

I don't think this is fixed because the op said they wanted to display it like this: (3) Shopping Cart. But if I try and change anything about the menu item text, the number stops showing up - instead it just shows a # character. I guess this is because the Commerce Cart module is doing a replace on the full string "Shopping cart (# items)" - would it be possible to change it so that it just replaces the '#' no matter what the rest of the string is? I would like to have Basket (3).

rszrama’s picture

Status: Active » Closed (fixed)

What you'll need to do is either 1) override the title callback of the menu item and copy / modify the callback from the Cart module as necessary or 2) perhaps you can translate the strings 'Shopping cart (1 item)' and 'Shopping cart (@count items)' to achieve your desired result.

woprrr’s picture

The two numbers works well!

2pha’s picture

I wanted it in the format of 'cart (1)' and did it with the string override module
http://drupal.org/project/stringoverrides/

germaike mv’s picture

woprrr’s picture

Another technique is to also create a view, which allows many possibilities!

nigelw’s picture

Status: Closed (fixed) » Needs work

Going to reopen as I am trying to achieve this with string overrides using just settings.php (as I don't want to install another module just to do this). It appears using any of the following has no affect. Thoughts?

$conf['locale_custom_strings_en'][''] = array(
	'Shopping cart'      => 'Cart',
	'Shopping cart (@count items)'      => 'Cart (@count)',
	'Shopping cart (@count item)'      => 'Cart (@count)'
);
rszrama’s picture

Status: Needs work » Closed (fixed)

You sure you have the array syntax right? I'm not sure you should have a NULL string in there as an array key.

nigelw’s picture

Status: Closed (fixed) » Needs work

All I am doing is modifying the examples to fit this scenario. Am I missing something. I need some hand holding :(

/**
 * String overrides:
 *
 * To override specific strings on your site with or without enabling locale
 * module, add an entry to this list. This functionality allows you to change
 * a small number of your site's default English language interface strings.
 *
 * Remove the leading hash signs to enable.
 */
# $conf['locale_custom_strings_en'][''] = array(
#   'forum'      => 'Discussion board',
#   '@count min' => '@count minutes',
# );
rszrama’s picture

Status: Needs work » Closed (fixed)

Sorry, then I'm not sure. Unfortunately, we don't provide multilingual support here in the issue queue. Maybe it's something as simple as not having the Locale module enabled or something else, but there's nothing here specific to Drupal Commerce. I'd recommend finding Drupal translation tutorials or something.

rajeevk’s picture

Hello,

How it can be done by stringoverride module ?

I tried like 'Shopping cart (@count items)' to 'Cart (@count)' , but it didn't work.

Thanks :)

remydenton’s picture

I went the route of doing a custom menu item callback. Here's some sample code in case it's helpful to anyone (just adapt the text in the second function to your specific needs; using it as-is will change "Shopping Cart" to "My Cart"):

/**
 * Implements hook_menu_alter().
 */
function MY_MODULE_menu_alter(&$items) {
  $items['cart/my']['title callback'] = 'MY_MODULE_menu_item_title';
}

/**
 * Returns the title of the shopping cart menu item with an item count.
 */
function MY_MODULE_menu_item_title() {
  global $user;

  // Default to a static title.
  $title = t('My cart');

  // If the user actually has a cart order...
  if ($order = commerce_cart_order_load($user->uid)) {
    // Count the number of product line items on the order.
    $wrapper = entity_metadata_wrapper('commerce_order', $order);
    $quantity = commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types());

    // If there are more than 0 product line items on the order...
    if ($quantity > 0) {
      // Use the dynamic menu item title.
      $title = format_plural($quantity, 'My cart (1 item)', 'My cart (@count items)');
    }
  }

  return $title;
}
rajeevk’s picture

@remydenton - Worked like charm. Thanks for it...

devonuto’s picture

What would be the best way to hide the link altogether if the cart is empty via this method?

woprrr’s picture

With this method set $title = NULL; work for me ;)

It's actualy best method to views your cart (it's superpower with custom panel blocks).

zendric’s picture

Assigned: Unassigned » zendric
Status: Closed (fixed) » Active

Thanks for this one Remy!
But I'm a bit stuck here... I'm trying to get the menu item to show just the number of different products in cart, not the total quantity of all the items. Is it possible with this same approach?

zendric’s picture

Assigned: zendric » Unassigned
rszrama’s picture

Status: Active » Closed (fixed)

Yep, you can do the same thing - just take out the bits that actually put the "My cart" text in there. Let's leave this issue closed, though, as it's quite old and we've since moved to using the Commerce Q&A on DC.org. You could also just not use this at all and translate the strings instead, since format_plural() arguments should be translatable.

jenlampton’s picture

Version: 7.x-1.2 » 7.x-1.7
Category: Support request » Feature request
Issue summary: View changes
Status: Closed (fixed) » Active

I would like to re-open this issue as a feature request. I have the same thought as in #8 that the module should only be replacing the # in the string provided in the menu UI. I actually went to the menu UI and changed the string several times before I realized this was not already happening.

Is there a way we can read in what's provided in the UI, and use that as the singular string for format_plural, simply replacing the # with @count?

RedEight’s picture

The solution in #1538094-19: Drupal Commerce - number of items in the basket on a menu element appears to work when I dropped it into my theme module. However, it occasionally falls back to the one title from the cart module.

Kind of maddening actually... Cache clearing can cause it to switch which one is handling it but there doesn't seem to be a rhyme or reason to it.

nadavoid’s picture

If using a menu item added manually in the menu UI, using hook_menu_alter() will not affect the link title. In that case, hook_preprocess_link() will work.

nithinkolekar’s picture

for future readers..
with the code provided @ #19 menu item not get updated when cart modified(cache has to flushed every time).

O U T L A W’s picture

I don't know if it is right implement this kind of logic in a theme function, but it works for me and I'm able to edit the link text in the Drupal UI. This code works for all links, you can use theme_menu_link() to alter only menu links.

/**
 * Implements theme_link().
 *
 * This code appends an item count to the link's text and wraps it
 * in a <span> for styling.
 *
 * Originally written by O U T L A W.
 * @see https://www.drupal.org/node/1538094
 */
function THEME_link (array $variables) {
  global $user;
  $path = $variables['path'];

  if($path == 'cart/my') {

    if($order = commerce_cart_order_load($user->uid)) {
      // Count the number of product line items on the order.
      $wrapper = entity_metadata_wrapper('commerce_order', $order);
      $quantity = commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types());

      // We're injecting custom HTML into the link text, so if the original
      // link text was not set to allow HTML (the usual case for menu items),
      // we MUST do our own filtering of the original text with check_plain(),
      // then specify that the link text has HTML content.
      if (!isset($variables['options']['html']) || empty($variables['options']['html'])) {
        $variables['text'] = check_plain($variables['text']);
        $variables['options']['html'] = TRUE;
      }

      $variables['text'] .= ' <span class="item-counter">' . $quantity . '</span>';
    }
  }

  // Call core theme_link() with edited elements.
  return theme_link($variables);
}
rattusrattus’s picture

See #2535070: Allow theming of cart menu link title for a patch to provide a theme funciton which can be overridden to alter the menu link title.

thirdender’s picture

I wanted to use the Commerce Cart module provided menu link, but I needed it in the user-menu menu instead of the navigation menu. Both the 'cart' and 'cart/my' items had to be moved to the new menu before they would appear in the UI. The following code was necessary to move the link:

/**
 * Implements hook_menu_alter
 */
function MY_MODULE_menu_alter(&$items) {
  // Alter the 'cart' and 'cart/my' items to place them in the 'User menu' menu
  $items['cart']['menu_name'] = 'user-menu';
  $items['cart/my']['menu_name'] = 'user-menu';
}
danielb’s picture

@ #32 Moving a menu item to another menu does not require any code, you can just do it by editing the menu item via the UI and changing it's parent.

Here is some code for hiding the Shopping Cart link altogether if there are 0 items:

/**
 * Implements hook_menu_alter.
 */
function MYMODULE_menu_alter(&$items) {
  $items['cart/my']['access callback'] = 'MYMODULE_check_shopping_cart_link_access';
  unset($items['cart/my']['access arguments']);
}

/**
 * Access callback for shopping cart menu link.
 */
function MYMODULE_check_shopping_cart_link_access() {
  global $user;

  // If the user actually has a cart order...
  if ($order = commerce_cart_order_load($user->uid)) {
    // Count the number of product line items on the order.
    $wrapper  = entity_metadata_wrapper('commerce_order', $order);
    $quantity = commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types());

    // Return true if quantity is greater than 0 and user can access content, otherwise false.
    return $quantity > 0 && user_access('access content');
  }

  return FALSE;
}

There's probably several ways to do that, but that's the way I concocted just now.

dkosbob’s picture

String overrides in settings.php worked just fine for me for altering the menu label while retaining functionality.

$conf['locale_custom_strings_en'][''] = array(
  'Shopping cart' => '',
  'Shopping cart (1 item)' => '(1 item)',
  'Shopping cart (@count items)' => '(@count items)',
);
nbaosullivan’s picture

I ended up doing the following:


function THEME_preprocess_link(&$vars){
  if($vars['path'] == 'cart/my'){
    $vars['text'] = THEME_cart_link_title();
  }
}


/**
 * Returns the title of the shopping cart menu item with an item count.
 */
function THEME_cart_link_title(){
  global $user;

  // Default to a static title.
  $title = t('Basket');

  // If the user actually has a cart order...
  if ($order = commerce_cart_order_load($user->uid)) {

    // Count the number of product line items on the order.
    $wrapper = entity_metadata_wrapper('commerce_order', $order);
    $quantity = commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types());

    // If there are more than 0 product line items on the order...
    if ($quantity > 0) {
      // Use the dynamic menu item title.
      $title = format_plural($quantity, 'Basket (1 item)', 'Basket (@count items)');
    }
  }

  return $title;
}
voodi777’s picture

Show product in cart by product count :

function get_simple_cart() {
  global $user;
  global $language;

  $cart_label = t('Your cart');
  $order = commerce_cart_order_load($user->uid);
	
  if (!empty($order)) {
    $wrapper = entity_metadata_wrapper('commerce_order', $order);
    $line_items = $wrapper->commerce_line_items;
    $total = commerce_line_items_total($line_items);
    $currency = commerce_currency_load($total['currency_code']);
    $quantity = commerce_line_items_quantity($line_items, commerce_product_line_item_types());
    $summ = commerce_currency_format($total['amount'], $total['currency_code']);
	 if(count($wrapper->commerce_line_items) <= 1) {
		  $count = count($wrapper->commerce_line_items);
	  } else {
	$count = count($wrapper->commerce_line_items) - 1;
	  }
    // For Russian cart we need plural function.
    if ($language->language == 'ru') {
      $quantity_label = getNumEnding($quantity, array('товар', 'товара', 'товаров'));
    }
    else {
      $quantity_label = 'item(s)';
    }

    $output = "<div id='cart-wrapper'><a href='/cart'><span class='icon-bag'><span class='bag-label'>{$count}</span></span>";
    $output .= "<div class='right-side full'><span class='label'>{$cart_label}</span><a href='/cart'>{$count} {$quantity_label} - {$summ}</a></div></div>";

  }
  else {
    $cart_label = t('<a href="/user" >Войти</a>');
    $cart_empty_label = t('личный кабинет');
    $output = "<div id='cart-wrapper'><span class='icon-bag'></span>";
    $output .= "<div class='right-side'><span >{$cart_label}</span><br>{$cart_empty_label}</div></div>";
  }

  return $output;
}
voodi777’s picture

Show product in cart by product count :

function get_simple_cart() {
  global $user;
  global $language;

  $cart_label = t('Your cart');
  $order = commerce_cart_order_load($user->uid);
	
  if (!empty($order)) {
    $wrapper = entity_metadata_wrapper('commerce_order', $order);
    $line_items = $wrapper->commerce_line_items;
    $total = commerce_line_items_total($line_items);
    $currency = commerce_currency_load($total['currency_code']);
    $quantity = commerce_line_items_quantity($line_items, commerce_product_line_item_types());
    $summ = commerce_currency_format($total['amount'], $total['currency_code']);
	 if(count($wrapper->commerce_line_items) <= 1) {
		  $count = count($wrapper->commerce_line_items);
	  } else {
	$count = count($wrapper->commerce_line_items) - 1;
	  }
    // For Russian cart we need plural function.
    if ($language->language == 'ru') {
      $quantity_label = getNumEnding($quantity, array('товар', 'товара', 'товаров'));
    }
    else {
      $quantity_label = 'item(s)';
    }

    $output = "<div id='cart-wrapper'><a href='/cart'><span class='icon-bag'><span class='bag-label'>{$count}</span></span>";
    $output .= "<div class='right-side full'><span class='label'>{$cart_label}</span><a href='/cart'>{$count} {$quantity_label} - {$summ}</a></div></div>";

  }
  else {
    $cart_label = t('<a href="/user" >Войти</a>');
    $cart_empty_label = t('личный кабинет');
    $output = "<div id='cart-wrapper'><span class='icon-bag'></span>";
    $output .= "<div class='right-side'><span >{$cart_label}</span><br>{$cart_empty_label}</div></div>";
  }

  return $output;
}
voodi777’s picture

Show product in cart by product count :

function get_simple_cart() {
  global $user;
  global $language;

  $cart_label = t('Your cart');
  $order = commerce_cart_order_load($user->uid);
	
  if (!empty($order)) {
    $wrapper = entity_metadata_wrapper('commerce_order', $order);
    $line_items = $wrapper->commerce_line_items;
    $total = commerce_line_items_total($line_items);
    $currency = commerce_currency_load($total['currency_code']);
    $quantity = commerce_line_items_quantity($line_items, commerce_product_line_item_types());
    $summ = commerce_currency_format($total['amount'], $total['currency_code']);
	 if(count($wrapper->commerce_line_items) <= 1) {
		  $count = count($wrapper->commerce_line_items);
	  } else {
	$count = count($wrapper->commerce_line_items) - 1;
	  }
    // For Russian cart we need plural function.
    if ($language->language == 'ru') {
      $quantity_label = getNumEnding($quantity, array('товар', 'товара', 'товаров'));
    }
    else {
      $quantity_label = 'item(s)';
    }

    $output = "<div id='cart-wrapper'><a href='/cart'><span class='icon-bag'><span class='bag-label'>{$count}</span></span>";
    $output .= "<div class='right-side full'><span class='label'>{$cart_label}</span><a href='/cart'>{$count} {$quantity_label} - {$summ}</a></div></div>";

  }
  else {
    $cart_label = t('<a href="/user" >Войти</a>');
    $cart_empty_label = t('личный кабинет');
    $output = "<div id='cart-wrapper'><span class='icon-bag'></span>";
    $output .= "<div class='right-side'><span >{$cart_label}</span><br>{$cart_empty_label}</div></div>";
  }

  return $output;
}