Search code examples
phpjqueryjoomla3.0virtuemart

Add wishlist button to Virtuamart product list


I'm using 1. Joomla 3.4.4 2. Virtuamart 3.0.9

and I want to use send data from products sublayout in Virtuamart and send data with ajax to a controller.

var url = "?";
jQuery(document).ready(function() {
   jQuery( ".wishlist-btn" ).click(function() {
    var productid = this.id;
    var userid = jQuery('input#user_id').val();
     var fav = {
            Productid: productid,
            Userid:userid
        }


    jQuery.ajax({
            url: url,
            type: "POST",
            data: { json: JSON.stringify(fav) },
            dataType: 'json',
contentType: 'application/json; charset=utf-8',
            success: function(data) {
                alert(data);
            }
        });


});

but I don't know where should write PHP code,and what is the URL in my AJAX request. I want to send profile id and user id to add in database for wishlist. I created a PHP file in

com_virtuamar/controller/ajax.php

$json = $_POST['json'];
$person = json_encode($json);
echo $person->Userid;

and for URL in my AJAX request I used

/components/com_virtuemart/controllers/ajax.php

but I think it's completely incorrect for address and usage in controller and also I don't know why userid doesn't return


Solution

  • You are an error in your Ajax request. For send to PHP file, remove data: { json: JSON.stringify(fav) } and replace by:

    jQuery.ajax({
        url: url,
        type: "POST",
        data: JSON.stringify(fav),
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function(data) {
            alert(data);
        }
    });
    

    and on your php file replace:

    $person = json_encode($json);
    

    by

    $person = json_decode($json);
    

    If you want access directly on Joomla session for get ID or Password user, you can check for

    JFactory::getUser();
    

    please see more: https://docs.joomla.org/JFactory/getUser