将心愿单按钮添加到Virtuamart产品列表中


Add wishlist button to Virtuamart product list

我正在使用1.Joomla 3.4.42.Virtuamart 3.0.9

我想在Virtuamart中使用来自products subyout的send数据,并使用ajax将数据发送到控制器。

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);
            }
        });

});

但我不知道应该在哪里编写PHP代码,也不知道AJAX请求中的URL是什么。我想发送配置文件id和用户id添加到愿望列表的数据库中。我在中创建了一个PHP文件

com_virtuamar/controller/ajax.php

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

对于AJAX请求中的URL,我使用了

/components/com_virtuemart/controllers/ajax.php

但我认为它在控制器中的地址和用法是完全不正确的,我也不知道为什么userid不返回

您的Ajax请求中有一个错误。对于发送到PHP文件,删除data: { json: JSON.stringify(fav) }并替换为:

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

并在您的php文件上替换:

$person = json_encode($json);

通过

$person = json_decode($json);

如果你想直接在Joomla会话上访问get ID或Password用户,你可以检查

JFactory::getUser();

请查看更多:https://docs.joomla.org/JFactory/getUser