VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Topic started by: alanski on September 25, 2012, 21:41:01 PM

Title: Add to cart Itemid issue resolved - bug or oversight
Post by: alanski on September 25, 2012, 21:41:01 PM
So am using ajax add to cart solution. (eg Display modal popup upon 'Add to cart' in Checkout Config)
THe add to cart button brings up little modal window and updates cart module.
Brilliant.

However what is not so brilliant is that the links to show cart and continue shopping dont actually keep the Itemid of my published menu item for the VM shop. Therefore you lose your main menu highlight and breadcrumb pathway.

I looked everywhere for template overrides but alas there are none!
This is done thru the file:components/com_virtuemart/controllers/cart.php
line 97 the function 'addJS'

Please VM/Max consider making this a config option or coding it in next version

Here the original code in version 2.010 is:
Quote

public function addJS() {

      //maybe we should use $mainframe->close(); or jexit();instead of die;
      /* Load the cart helper */
      //require_once(JPATH_VM_SITE.DS.'helpers'.DS.'cart.php');
      $this->json = null;
      $cart = VirtueMartCart::getCart(false);
      if ($cart) {
         // Get a continue link */
         $virtuemart_category_id = shopFunctionsF::getLastVisitedCategoryId();
         if ($virtuemart_category_id) {
            $categoryLink = '&view=category&virtuemart_category_id=' . $virtuemart_category_id;
         } else
         $categoryLink = '';
         $continue_link = JRoute::_('index.php?option=com_virtuemart' . $categoryLink);
         $virtuemart_product_ids = JRequest::getVar('virtuemart_product_id', array(), 'default', 'array');
         $errorMsg = JText::_('COM_VIRTUEMART_CART_PRODUCT_ADDED');
         if ($cart->add($virtuemart_product_ids, $errorMsg )) {

            $this->json->msg = '<a class="continue" href="' . $continue_link . '" >' . JText::_('COM_VIRTUEMART_CONTINUE_SHOPPING') . '</a>';
            $this->json->msg .= '<a class="showcart floatright" href="' . JRoute::_("index.php?option=com_virtuemart&view=cart") . '">' . JText::_('COM_VIRTUEMART_CART_SHOW_MODAL') . '</a>';
            if ($errorMsg) $this->json->msg .= '<div>'.$errorMsg.'</div>';
            $this->json->stat = '1';
         } else {
            // $this->json->msg = '<p>' . $cart->getError() . '</p>';
            $this->json->msg = '<a class="continue" href="' . $continue_link . '" >' . JText::_('COM_VIRTUEMART_CONTINUE_SHOPPING') . '</a>';
            $this->json->msg .= '<div>'.$errorMsg.'</div>';
            $this->json->stat = '2';
         }
      } else {
         $this->json->msg = '<a href="' . JRoute::_('index.php?option=com_virtuemart') . '" >' . JText::_('COM_VIRTUEMART_CONTINUE_SHOPPING') . '</a>';
         $this->json->msg .= '<p>' . JText::_('COM_VIRTUEMART_MINICART_ERROR') . '</p>';
         $this->json->stat = '0';
      }
      echo json_encode($this->json);
      jExit();
   }

That wont do!

So i added the current menu itemid from wherever the shop is being viewed into the function. THere are only a few changes but here is the whole function with menu item id added so that the modal pop up has links that keep the item id from where my shop is published in the menu system.

Here is the amended function from line 97 of components/com_virtuemart/controllers/cart.php [front end people!]:
Quote
public function addJS() {

      //maybe we should use $mainframe->close(); or jexit();instead of die;
      /* Load the cart helper */
      //require_once(JPATH_VM_SITE.DS.'helpers'.DS.'cart.php');
      $this->json = null;
      //added by joomkit
      $menuid = JRequest::getVar('Itemid');
      $cart = VirtueMartCart::getCart(false);
      if ($cart) {
         // Get a continue link */
         $virtuemart_category_id = shopFunctionsF::getLastVisitedCategoryId();
         if ($virtuemart_category_id) {
            $categoryLink = '&view=category&virtuemart_category_id=' . $virtuemart_category_id.'&Itemid='.$menuid; //amended by joomkit

         } else
         $categoryLink = '';
         $continue_link = JRoute::_('index.php?option=com_virtuemart' . $categoryLink.'&Itemid='.$menuid);
         $virtuemart_product_ids = JRequest::getVar('virtuemart_product_id', array(), 'default', 'array');
         $errorMsg = JText::_('COM_VIRTUEMART_CART_PRODUCT_ADDED');
         if ($cart->add($virtuemart_product_ids, $errorMsg )) {

            $this->json->msg = '<a class="continue" href="' . $continue_link . '" >' . JText::_('COM_VIRTUEMART_CONTINUE_SHOPPING') . '</a>';
            $this->json->msg .= '<a class="showcart floatright" href="' . JRoute::_("index.php?option=com_virtuemart&view=cart&Itemid=".$menuid) . '">' . JText::_('COM_VIRTUEMART_CART_SHOW_MODAL') . '</a>'; //amended by joomkit
            if ($errorMsg) $this->json->msg .= '<div>'.$errorMsg.'</div>';
            $this->json->stat = '1';
         } else {
            // $this->json->msg = '<p>' . $cart->getError() . '</p>';
            $this->json->msg = '<a class="continue" href="' . $continue_link . '" >' . JText::_('COM_VIRTUEMART_CONTINUE_SHOPPING') . '</a>';
            $this->json->msg .= '<div>'.$errorMsg.'</div>';
            $this->json->stat = '2';
         }
      } else {
         $this->json->msg = '<a href="' . JRoute::_('index.php?option=com_virtuemart') . '" >' . JText::_('COM_VIRTUEMART_CONTINUE_SHOPPING') . '</a>';
         $this->json->msg .= '<p>' . JText::_('COM_VIRTUEMART_MINICART_ERROR') . '</p>';
         $this->json->stat = '0';
      }
      echo json_encode($this->json);
      jExit();
   }



How to use? Just replace original function with this one. Backup first though :)

[loving VM2 a whole lot more than VM1]
Title: Re: Add to cart Itemid issue resolved - bug or oversight
Post by: johk on February 06, 2013, 12:50:52 PM
Hi,
Thanks for the tip

Yes this unfortunate as you even lose your module positions.
Worse is that you can't put it in a template – so when you update VM you might have re-do the hack again???!!

Cheers,
jh
Title: Re: Add to cart Itemid issue resolved - bug or oversight
Post by: Milbo on May 25, 2013, 21:34:22 PM
In vm2.0.21 you can do that. The layout is padded.php. I also added the menuId in 2.0.21b (not uploaded yet, but tomorrow). But I dont see any difference, maybe because I have the cart only as module?
Title: Re: Add to cart Itemid issue resolved - bug or oversight
Post by: sandstorm on September 05, 2013, 12:55:43 PM
I just updated from 2.0.18a, where I was using this modification and it worked well.
Updated to 2.0.22b and this code looks completely different? 
I don't know what to modify to get "Contine Shopping" link to work correctly. But with SEF on this still does not work for me with default code.

public function addJS() {

$this->json = new stdClass();
$cart = VirtueMartCart::getCart(false);
if ($cart) {

$virtuemart_product_ids = JRequest::getVar('virtuemart_product_id', array(), 'default', 'array');
$view = $this->getView ('cart', 'json');
$errorMsg = 0;//JText::_('COM_VIRTUEMART_CART_PRODUCT_ADDED');
$product = $cart->add($virtuemart_product_ids, $errorMsg );
if ($product) {
$view->setLayout('padded');
$this->json->stat = '1';
} else {
$view->setLayout('perror');
$this->json->stat = '2';
}
$view->assignRef('product',$product);
$view->assignRef('errorMsg',$errorMsg);
ob_start();
$view->display ();
$this->json->msg = ob_get_clean();
} else {
$this->json->msg = '<a href="' . JRoute::_('index.php?option=com_virtuemart', FALSE) . '" >' . JText::_('COM_VIRTUEMART_CONTINUE_SHOPPING') . '</a>';
$this->json->msg .= '<p>' . JText::_('COM_VIRTUEMART_MINICART_ERROR') . '</p>';
$this->json->stat = '0';
}
echo json_encode($this->json);
jExit();
}


I use SH404SEF