verbatim from http://www.firewing1.com/node/27
This is an unusual situation, but it's a very annoying problem; adding an item to the cart, switching languages and then click on that same item in the cart will open the product in first language, not the one the customer is browsing your website in. To solve this, hook_cart_item() will be used to override the default cart rendering with our own version which will replace node with translated nodes. Open the custom module file, sites/all/modules/custommod/custommod.module, which was created earlier and add before the closing ?> tag:
<?php /** * Implementation of hook_add_to_cart(). */ function custommod_add_to_cart($nid, $qty, $data) { /* Due to Drupal's use of multiple nodes for product translations, the same * product in a different language is treated as a different product entirely. * This is problematic as the same product in different languages can be added * to the cart simultaneously. This function works around that problem by * always using the tnid/original node. As a result, the cart must be * localized as it is displayed. */ $node = node_load($nid); // Determine if this node is the source node or a translated one // Remember: tnid is 0 if there are no translations $is_source = ($node->nid == $node->tnid || $node->tnid == 0) ? 1 : 0; if ($is_source) { // If it is the source, then all is well… $result[] = array('success' => TRUE); } else { /* If we are not the source node, then fail to add this product silently and * call uc_cart_add_item() to add the source node's product instead. It will * be localized later - see custommod_cart_item() */ uc_cart_add_item($node->tnid, $qty, $data); $result[] = array('success' => FALSE, 'silent' => TRUE); } // Remember: We need an array in an array here return $result; } /** * Implementation of hook_cart_item(). */ function custommod_cart_item($op, &$item) { /* hook_cart_display() isn't really a hook, it's mostly for internal use. * However, we do need to access later. Setting $item->module forces * a module_invoke() call in uc_cart.module to call custommod_cart_display() * instead of the default uc_product_cart_display(). We will call * uc_product_cart_display() inside our function to ensure things work as * usual in future versions. */ $item->module = "custommod"; /* Note that although it is possible to use check for case 'load' in $op and * then override the $item->nid and $item->title values, this will cause bugs * when attempting to add or remove products in different languages. To * resolve these bugs, we are forcing the use of custommod_cart_display() and * rewriting the code for the title, img, and anchors to localize the cart. */ } /** * Implementation of hook_cart_display(). */ function custommod_cart_display($item) { /* Call uc_product_cart_display() to get things setup as usual and to ensure * this hack still works even if uc_product_cart_display changes at some point * in the future. */ $display_item = uc_product_cart_display($item); // Get the translations, if any. $node = node_load($item->nid); global $language; $translations = translation_node_get_translations($node->tnid); if ($translations[$language->language]) { // Reminder: NEVER override the nid. That is what causes the bugs! $tnode = node_load($translations[$language->language]->nid); $display_item["title"]["#value"] = node_access('view', $tnode) ? l($tnode->title, 'node/'. $tnode->nid) : check_plain($tnode->title); $display_item["image"]["#value"] = uc_product_get_picture($tnode->nid, 'cart'); } return $display_item; } ?>After reloading the Custom module at Administer > Site building > Modules, the cart should behave properly when switching languages. As well, adding a product to the cart in one language, switching languages, then adding it again should not result in two different products being added to the cart. Instead, the quantity of the product will increase by 1.
Credit for this solution goes to user Docc at Ubercart forums, who posted the code sample (see reference 3)
Comments
Comment #1
stewart.adam commentedsubscribing
Comment #2
intyms commentedsubscribing
Comment #3
longwaveDuplicate of #705786: On checkout: cart contents are not translatable