简体   繁体   English

Jquery Shopping Cart插件:实现会话管理

[英]Jquery Shopping Cart Plugin: Implementing Session Management

Am using jquery shopping cart http://plugins.jquery.com/project/smartcart2 Can anyone help me in implementing onAdded, onRemoved, onUpdated methods in order to make Ajax call to maintain session on the server side. 我正在使用jquery购物车http://plugins.jquery.com/project/smartcart2谁能帮助我实现onAdded,onRemoved,onUpdated方法,以便进行Ajax调用以在服务器端维护会话。 I make the Ajax request on the server whenever an object is added/removed/updated and get the data back in JSON format. 每当添加/删除/更新对象时,我都会在服务器上发出Ajax请求,并以JSON格式获取数据。 I update the qty like obj.attr("qty", eval(msg)[1]); 我像obj.attr("qty", eval(msg)[1]);一样更新了数量obj.attr("qty", eval(msg)[1]); with the updates from server. 来自服务器的更新。 However, if i hit refresh or the table data is repopulated the cart items are no longer there. 但是,如果我单击刷新或重新填充表数据,则购物车项目不再存在。 So, the question really is how to populate the data from session so that the product would still remain in the shopping cart upon refresh etc. 因此,问题实际上是如何从会话中填充数据,以便产品在刷新等时仍保留在购物车中。

$('#SmartCart').smartCart({ 
onAdded: function(pObj,quantity){ cartAdded(pObj,quantity);}, 
onRemoved: function(pObj){ cartRemoved(pObj);},
onUpdated: function(pObj,quantity){ cartUpdated(pObj,quantity); },
});

function cartAdded(obj,qty){
var product_id = obj.attr("pid");
var quantity = qty; 
// Ajax calls for adding product to cart
function(pObj,quantity){
    cartAdded(pObj,quantity);}
}

function cartRemoved(obj){
var product_id = obj.attr("pid");
// Ajax call for removing product from cart
}

function cartUpdated(obj,qty){
var product_id = obj.attr("pid");
var quantity = qty; 
// Ajax call for updating product on cart
}

function cartAdded(obj,qty){
            var partNum = obj.attr("partNumber");
            var quantity = qty;                
         $.ajax({
        type: 'POST',
        url: "json/sessionManager",
        data : "partNum=" + partNum + "&qty=" + quantity,
        dataType: "text/json",
        success: function(msg){
            obj.attr("qty", msg[1]);
        },
        error: function(httpRequest, textStatus, errorThrown) {
           alert("status=" + textStatus + ",error=" + errorThrown);
        }
    });  
        }

I would highly appreciate recommendations around the same. 我非常希望您提出相同的建议。

  1. Implement your cart on server side. 在服务器端实施购物车。
  2. Keep your cart objects (products) in a collection or something. 将购物车中的物品(产品)存放在集合中或其他物品中。
  3. Every time an Ajax request arrives to server side: 每当Ajax请求到达服务器端时:

    • Get data (product_id,quantity, ...etc) from request. 从请求获取数据(product_id,quantity等)。
    • Get operation type (add,remove ..). 获取操作类型(添加,删除..)。
    • Check for data validity. 检查数据有效性。
    • Do required server side business (update db, call web-service etc). 做所需的服务器端业务(更新数据库,致电网络服务等)。
    • If everything is fine add/remove specified product to/from your collection. 如果一切正常,请向您的收藏中添加/删除指定的产品。
    • Convert your server side cart to JSON and write to response of Ajax call. 将您的服务器端购物车转换为JSON并写入Ajax调用的响应。
  4. Every time an JSON response arrives to client side: 每次JSON响应到达客户端时:

    • Update your cart with new data. 使用新数据更新您的购物车。

For the Ajax part. 对于Ajax部分。 Use jQuery Ajax() function 使用jQuery Ajax()函数

$.ajax({
   type: "POST",
   url: "cart.jsp",
   data: "p_id=SKU001&quantity=4",
   success: function(msg){
     alert( "FOUR SKU001 ADDED TO CART");
   }
 });

Edit: Ooh i see. 编辑:哦,我明白了。 product_id is undefined means your obj.attr("pid"); product_id未定义意味着您的obj.attr("pid"); is not working. 不管用。

This plug-in uses hidden HTML inputs for product definitions. 该插件将隐藏的HTML输入用于产品定义。 (plain stupid if you ask me) This inputs have some pseudo attributes which you can get by obj.attr("pid"); (如果您问我,那么愚蠢)此输入具有一些属性,可以通过obj.attr("pid"); . If there is no input in your form or your input does not have that pseudo attributes your code fails. 如果您的表单中没有输入或您的输入不具有该属性,则代码将失败。

BE SURE YOUR HIDDEN HTML INPUTS HAS THIS PSEUDO ATTRIBUTES. 请确保您的隐藏HTML输入具有此属性。

For example: 例如:

<div id="SmartCart" class="scMain">
  <input type="hidden" pimage="products/product1.jpg" pprice="2299.99" pdesc="" 
    pcategory="Computers" pname="Apple MacBook Pro MA464LL/A 15.4" pid="100">

  <input type="hidden" pimage="products/product6.jpg" pprice="2699.99" pdesc="" 
    pcategory="Computers" pname="Sony VAIO 11.1&quot; Notebook PC" pid="101">
  <input type="hidden" pimage="products/product3.jpg" pprice="550.00" pdesc="" 
    pcategory="Cameras" pname="Canon Digital Rebel" pid="102">
</div>

From the authors documentation: 从作者文档中:

Description:Text marked bold are the pseudo attributes that describes about the product. 描述:标记为粗体的文本是描述产品的属性。 Like product name, price, description etc. 如产品名称,价格,说明等。

  • pid : Product ID pid :产品编号
  • pname : Name of the product pname :产品名称
  • pdesc : Description of the product pdesc :产品说明
  • pprice : Price of the product pprice :产品价格
  • pimage : Product image source pimage :产品图片来源
  • pcategory : Category of the product pcategory :产品类别

You can add more product details by adding new attributes to the input element, and so you can show them on product list or cart by editing the template. 您可以通过向输入元素添加新属性来添加更多产品详细信息,因此可以通过编辑模板将其显示在产品列表或购物车中。 You can customize the pseudo attribute names, and configure it on the plug-in file on the Attribute Settings section. 您可以自定义伪属性名称,并在“属性设置”部分的插件文件上对其进行配置。

Edit2: I think you have difficulties in this section of my recommendations. Edit2:我认为您在我建议的这一部分中遇到困难。

Every time an JSON response arrives to client side: 每次JSON响应到达客户端时:

Update your cart with new data. 使用新数据更新您的购物车。

Hidden inputs below are your data. 以下隐藏的输入是您的数据。 Open another question on stackoverflow and ask 在stackoverflow上打开另一个问题并询问

how can i modify (Create, Update, Delete) this inputs in jQuery 我如何在jQuery中修改(创建,更新,删除)此输入

<div id="SmartCart" class="scMain">
  <input type="hidden" pimage="products/product1.jpg" pprice="2299.99" pdesc="" 
    pcategory="Computers" pname="Apple MacBook Pro MA464LL/A 15.4" pid="100">

  <input type="hidden" pimage="products/product6.jpg" pprice="2699.99" pdesc="" 
    pcategory="Computers" pname="Sony VAIO 11.1&quot; Notebook PC" pid="101">
  <input type="hidden" pimage="products/product3.jpg" pprice="550.00" pdesc="" 
    pcategory="Cameras" pname="Canon Digital Rebel" pid="102">
</div>

because after solving your first question asking another in the same post is bad practice . 因为在解决您的第一个问题后在同一职位上问另一个是不好的做法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM