Drupal 7 Commerce HOWTO: Get commerce_add_to_cart_confirmation Message out of the Messages

Drupal 7 Commerce HOWTO: Get commerce_add_to_cart_confirmation Message out of the Messages

The ‎Commerce add to cart confirmation‎ uses the drupal_set_message() mechanism to display the add to cart layer after a product has been added to the cart.

This is being triggered via a ‎Rules‎ rule, provided by the module.
The problem is, that this may create a conflict with other modules, like for example the absolut_messages.module. Furthermore it is a handy **hack** for the ‎Commerce add to cart confirmation‎ but you may see it as a flaw.

As a concrete problem I had to solve the conflict with absolut_messages.module as described above and I'd like to share my solution with you, if you should run into similar problems.

What I've done is created a theme hook function in HOOK_preprocess_page($vars) and added a little snippet to the page.tpl.php where to render the add to cart confirmation message into.

Have fun using the following snippets on your own risk:

Add the following preprocess function to your theme and rename it accordingly:

  1. function MYTHEMENAME_preprocess_page(&$vars) {
  2.   if (isset($_SESSION['messages']['commerce-add-to-cart-confirmation'])) {
  3.     $add_to_cart_messages = $_SESSION['messages']['commerce-add-to-cart-confirmation'];
  4.     $markup = '';
  5.     if (!empty($add_to_cart_messages)) {
  6.       foreach ($add_to_cart_messages as $add_to_cart_message) {
  7.         $markup .= $add_to_cart_message;
  8.       }
  9.     }
  10.     $vars['commerce_add_to_cart_confirmation_messages'] = array(
  11.       '#type' => 'container',
  12.       '#attributes' => array('class' => array('messages my-commerce-add-to-cart-confirmation-messages commerce-add-to-cart-confirmation')),
  13.       'content' => array(
  14.         '#type' => 'markup',
  15.         '#markup' => $markup)
  16.     );
  17.     unset($_SESSION['messages']['commerce-add-to-cart-confirmation']);
  18.   }
  19.   else {
  20.     $vars['commerce_add_to_cart_confirmation_messages'] = NULL;
  21.   }
  22. }

Then add the following snippet to where the add to cart confirmation should be output in the template code (page.tpl.php)

  1. if (!empty($commerce_add_to_cart_confirmation_messages)) {
  2.   print render($commerce_add_to_cart_confirmation_messages);
  3. }

Eventually you will have to modify one or two lines of your css selectors, if it doesn't work ad hoc.

Please leave a comment, if this was useful for you or if you have any suggestions for a better solutions.

Kommentare

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt. Wenn Sie ein zulässiges Avatar mit Ihrer E-Mail-Adresse verknüpft haben, wird dieses als Avatar verwendet.

Weitere Informationen über Formatierungsoptionen

Aktualisieren Geben Sie die Zeichen ein, die Sie im Bild sehen. Geben Sie die im Bild dargestellten Zeichen ein; wenn Sie diese nicht lesen können, senden Sie das Formular ohne Eingabe ab, um ein neues Bild zu generieren. Groß-/Kleinschreibung wird nicht beachtet.  Switch to audio verification.