Pabzt Pabzt - 7 days ago 8
jQuery Question

Virtuemart call add to cart function

I'm new to Virtuemart development. I have my own template for the product details page:

templates/myTemplate/html/com_virtuemart/productdetails/myProductDetails.php


I also added a custom field (string) for the product (I will have only one product that can be configured). The configuration for that product will be done on the product details page. I have developed a configuration process using jQuery.

I also added the virtuemart "Add to cart"-Button using the following code:

echo shopFunctionsF::renderVmSubLayout('addtocart',array('product'=>$this-
>product));


If the user clicks the "Add to cart"-Button i have to check the configuration. If the configuration is valid then it should be stored in the custom field i have created for the product. I'd like to store the data to the custom field as a JSON-string. After the data has been saved to the custom field, I'd like to add the product to the cart. Then the usual popup dialog should be shown. The custom field value should be shown as cart attribute. If the configuration is not valid then there should be some sort of an error output. The product should not be added to the cart.

How can i use the virtuemart "Add to cart" functionality to do this?

I also have to check the submitted data in the custom field on the server side. What is the best approach to do this? (Maybe using the checkout page?)

Answer

I found the solution to add Items to cart and check the configuration:

$('#myaddtocart').click(function() {

  var data = "quantity[]="+20+"&virtuemart_product_id[]="+1;

  $.ajax({               

    type: "GET",
    dataType: 'json',
    url: "index.php?option=com_virtuemart&nosef=1&view=cart&task=addJS",
    data: data,
    success: function(data) {

    }

  });

});

This snippet calls the addJS() function located in /com_virtuemart/controllers/cart.php - only quantity and product id must be provided. Also I used my own button (#myaddtocart) to trigger the click event and add the item to the cart.