2

I'm trying to change/customize this message and have found that it is generated by "Display an Add to Cart message" rule from Drupal Rules. Everything would be fine but I can't find this text and I also don't know what to alter (and how) to change this message. Does anyone tried to do this?

3
  • I also have this problem :(
    – Yuseferi
    Dec 12 '13 at 10:17
  • commerce_cart.rules.inc -> function commerce_cart_rules_add_to_cart_message($product) didn't help you?
    – xurshid29
    Dec 12 '13 at 10:34
  • @xurshid29 ,patching module is not clear way :(
    – Yuseferi
    Dec 15 '13 at 20:40
2
+50

There are several Options,

  1. got to rules and disable the add to cart rule, then create your own.
  2. Use this module https://drupal.org/project/stringoverrides to override the string. The string to override is this "%title added to your cart."

I wouldn't edit the module file commerce_cart.rules.inc -> function commerce_cart_rules_add_to_cart_message($product) as when you update it will just overwrite what you have done

3
  • as I wrote it is not a good solution but i couldn't find another one. And I don't think that stringoverride supports wildcards.
    – xurshid29
    Dec 12 '13 at 10:53
  • actually a good thread here about it drupalcommerce.org/discussions/669/change-cart-basket Dec 12 '13 at 10:56
  • I mean, drupal_set_title() in commerce_cart_rules_add_to_cart_message($product) gets a $product object as argument, and it can be different product objects and different titles. That's why module string overrides can't handle the $product objects and titles, I think.
    – xurshid29
    Dec 12 '13 at 11:09
2

The best way to do this is with: hook_rules_action_info_alter()

function HOOK_rules_action_info_alter(&$actions) {
    $actions['commerce_cart_add_to_cart_message']['callbacks']['execute'] = 'MODULE_custom_cart_callback';
}

function MODULE_custom_cart_callback($product) {
    drupal_set_message(t('%title added to <a href="!cart-url">your cart</a>.', array('%title' => $product->title, '!cart-url' => url('cart'))));
}

Basic steps:

  1. Create a new module.
  2. Add the hook_rules_action_info_alter() hook replacing "HOOK" with your module name.
  3. Define MODULE_custom_cart_callback($product) replacing "MODULE" with your module name.
  4. Enable the module and clear the cache.
5
  • Just edit the rule via the UI, no need to override the callback. Dec 15 '13 at 5:04
  • True, you could delete the show cart action and use the System action for "Show a message on the site". However I'd prefer to manage it in code, less chance of something getting reverted and trackable in git. Also, with above you'd override any other places that same action is used, eg a quick add to cart module may re-use that same action.
    – benjy
    Dec 15 '13 at 6:12
  • But it would lead to frustration if the user tried to change it from the UI and it didn't work (as it would be overriden by the alter hook). Could it be made to work with hook_default_rules_configuration? Dec 16 '13 at 10:17
  • You can't customise the message from the UI and the code above only changes the function used to display the message. So I'm not sure what you mean.
    – benjy
    Dec 17 '13 at 1:21
  • Definitely the best answer. Disabling and cloning rules is a bit of a ball-ache when you're doing everything with features. I needed a custom implementation because I want to suppress the drupal_set_message and instead pop the cart block out, so this worked perfectly. Thanks! Aug 18 '17 at 9:51
0

I am using this to replace an existing drupal message to my custom message.

function _exclude_message($mymessage, $mytype="status"){    $flag = false if ($messageArr = drupal_set_message()) {  foreach($messageArr as $type=>$messages) {
  if ($mytype==$type) {
    foreach($messages as $key=>$message) {
      if ($mymessage==$message) {
    unset($_SESSION['messages'][$type][intval($key)]);
       $flag = true;
      }
   }
  }

  if (count($_SESSION['messages'][$type])==0) unset($_SESSION['messages'][$type]);}} return $flag;}
function my_theme_status_messages($display = NULL) {
  if (_exclude_message('Sorry, unrecognized username or password. <a href="/user/password">Have you forgotten your password?</a>',"error")) drupal_set_message("Sorry, unrecognized username or password.","error");
 return theme_status_messages($display);
}

under template.php

this code will replace existing "Sorry, unrecognized username or password.Have you forgotten your password?" with my custom message "Sorry, unrecognized username or password.".

Hope this will help you.

0

I'm using String Overrides module and there was no need to delete the message rule. I found the message rule defined in the commerce cart module code

%title added to your cart. so just changed it, for example, to %title added to your basket.

...where as the other message was configured %title removed from your cart. and needed changing to, for example %title removed from your basket.

Hope that would be helpful :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.