News:

Looking for documentation? Take a look on our wiki

Main Menu

VM 4 Module Migration Troubleshooting Help/Ideas Needed

Started by smusselm, February 28, 2023, 22:45:12 PM

Previous topic - Next topic

smusselm

I'm attempting to update a VM 3 module to work with Joomla 4 and VM 4. I use an AJAX cart module in Joomla 3 and VM 3, but I can't get it to work in the latest versions. I've contacted the developer to see if I can get an update. I've looked for alternatives, but I can't find anything. It's holding up the entire migration, and I have a 4/1 deadline.

Because it uses AJAX I'm somewhat stumped on where to look for compatibility issues. I thought maybe if I provided the following information, more seasoned developers might have a clue what's going on:
- The non-ajax version works.  So if I add something to the cart and refresh the page, it works.
- The ajax version works on the cart page, but not on any other page.
- I've replaced JREQUEST with JINPUT in several instances to prevent 500 errors
- I've replaced other things like JRESPONSE->setHeader with $JFactory::getApplication()->setHeader to be J4 compatible

My assumption is that the ajax cart code is having trouble accessing the cart, or some variable or input is readily available on the cart page that isn't on another.  Has anything changed in version 4 regarding how the cart is initiated? Any other ideas?! Seriously, any theories or leads would be appreciated.

Here is example of some code that might be the cause. I get the success message on the cart page and the error message on the other pages.

public static function deleteCartItemAjax()
{
$jinput = JFactory::getApplication()->input;
$product_id = $jinput->get('cart_virtuemart_product_id', '', 'int');

if (!class_exists( 'VmConfig' )) require(JPATH_ROOT .'/administrator/components/com_virtuemart/helpers/config.php');
VmConfig::loadConfig();
vmLanguage::loadJLang('mod_vmajaxdropdowncart', true);
vmLanguage::loadJLang('com_virtuemart', true);
vmJsApi::jQuery();

//This is strange we have the whole thing again in controllers/cart.php public function viewJS()
if(!class_exists('VirtueMartCart')) require(VMPATH_SITE.DS.'helpers'.DS.'cart.php');
$cart = VirtueMartCart::getCart(false);

$state = $cart->removeProductCart($product_id);

if($state)
{
$res = array('status'=>'success', 'message'=>'deleted');
} else {
$res = array('status'=>'error', 'message'=>'Unable to delete the product');
}
echo json_encode($res);

exit;
}


I'm happy to provide any additional information/code if someone thinks they may have a theory! If this issue might be more involved, I'm open to contracting a developer for assistance.

Thanks in advance.


Studio 42

Why
vmJsApi::jQuery();
in a json response?
deleteCartItemAjax is in class XXX?
If you use a module and is visible in some menu only, then ajax call can fail , because access restriction. You need to add the menu ID (itemId=XX) in the ajax call

smusselm

Thank you! I don't know about the JQuery. I didn't write the code. Looks like copy and paste extraneous code.

Here is the class: modVmajaxdropdowncartHelper (I believe)

I think I'm zeroing in on an answer by process of elimination, but I still can't get it to work.

Here's a sample ajax call URL for adding a product:
index.php?option=com_ajax&module=vmajaxdropdowncart&method=vmadc&format=json&Itemid=2283

Here's the URL for deleting a product:
index.php?option=com_ajax&module=vmajaxdropdowncart&method=deleteCartItem&format=json&Itemid=2283

The item id is the category and the categories and menu items are public. 

In the code above, if I do a console.log( data.status ) in the cart, I get a success.  If I do it on any other page, it returns 'undefined' and not 'error'.

The data being sent via post request is: cart_virtuemart_product_id: cart_item_key  The cart_item_key is defined in the module HTML and called up via jquery.

My best guess is that the code can identify cart_virtuemart_product_id on the cart page, but not on the other pages.

Any thoughts???  My development experience is self-taught and limited.

Did something change with how the cart is called up? Is the Jinput method wrong for getting cart_virtuemart_product_id? Is there a better way of getting that id?

Studio 42

itemId is the PUBLIC menu ID and not a category
If you have trouble force the itemId from your cart menu and try