2013-07-04 72 views
1

我擊中構建基於現有http://thesitedoctor.co.uk/portfolio/avenue-clothingcom/uCommerce - 添加動態屬性命令行

演示剃刀店頂部的uCommerce網站上的演示使用servicestack併爲其籃功能ucommerceapi一個問題。

我想在用戶點擊購買的地方添加一個動態屬性到購物籃(訂單行)。我通過productpage.js文件跟蹤和修改代碼以添加一個新的屬性(「信息」):使用Firebug

function (data) { 
var variant = data.Variant; 
$.uCommerce.addToBasket(
{ 
    sku: variant.Sku, 
    variantSku: variant.VariantSku, 
    quantity: qty, 
    message: $('#personalisedMessage').val() 
}, 
function() { 
    updateCartTotals(addToCartButton); 
} 
); 
}); 

,我檢查了正在張貼

addToExistingLine: true 
message: "this is a message" 
quantity:"1" 
sku: "Product (options: none)" 
variantSku:"" 

發佈該數據不會導致錯誤,但我不知道它是否有效 - 我無法在數據庫中找到它,假設它將存儲在OrderProperty表中。在這種情況下,我'購買'沒有變化的產品。

任何幫助非常感謝與此。

回答

0

開箱即用,您無法通過API添加訂單/訂單項屬性。已添加的API有效內容已指定,但有效的JSON不會被API解釋/使用。

相反,您需要做的是將您自己的方法添加到API中。要做到這一點,您需要從IUCommerceApiService實施服務,然後您可以做到您需要的。我已經在下面創建了一個示例(未經測試),並將它添加到演示商店,因爲我認爲這是一個有用的功能位。

public class AddOrderLineProperty 
{ 
    public int? OrderLineId { get; set; } 

    public string Sku { get; set; } 

    public string VariantSku { get; set; } 

    public string Key { get; set; } 

    public string Value { get; set; } 
} 
public class AddOrderLinePropertyResponse : IHasResponseStatus 
{ 
    public AddOrderLinePropertyResponse() { } 

    public AddOrderLinePropertyResponse(UCommerce.EntitiesV2.OrderLine line) 
    { 
     if (line == null) 
     { 
      UpdatedLine = new LineItem(); 
      return; 
     } 

     var currency = SiteContext.Current.CatalogContext.CurrentCatalog.PriceGroup.Currency; 
     var lineTotal = new Money(line.Total.Value, currency); 

     UpdatedLine = new LineItem() 
     { 
      OrderLineId = line.OrderLineId, 
      Quantity = line.Quantity, 
      Sku = line.Sku, 
      VariantSku = line.VariantSku, 
      Price = line.Price, 
      ProductName = line.ProductName, 
      Total = line.Total, 
      FormattedTotal = lineTotal.ToString(), 
      UnitDiscount = line.UnitDiscount, 
      VAT = line.VAT, 
      VATRate = line.VATRate 
     }; 
    } 

    public ResponseStatus ResponseStatus { get; set; } 

    public LineItem UpdatedLine { get; set; } 
} 
public class AddOrderLinePropertyService : ServiceBase<AddOrderLineProperty>, IUCommerceApiService 
{ 
    protected override object Run(AddOrderLineProperty request) 
    { 
     var orderLineId = request.OrderLineId; 
     var sku = request.Sku; 
     var variantSku = request.VariantSku; 

     var orderLine = findOrderLine(orderLineId, sku, variantSku); 
     addPropertyToOrderLine(orderLine, request.Key, request.Value); 

     TransactionLibrary.ExecuteBasketPipeline(); 
     var newLine = findOrderLine(orderLineId, sku, variantSku); 
     return new AddOrderLinePropertyResponse(newLine); 
    } 

    private void addPropertyToOrderLine(OrderLine orderLine, string key, string value) 
    { 
     if (orderLine == null) 
      return; 

     orderLine[key] = value; 

     orderLine.Save(); 
    } 

    private static OrderLine findOrderLine(int? orderLineId, string sku, string variantSku) 
    { 
     return orderLineId.HasValue 
          ? getOrderLineByOrderLineId(orderLineId) 
          : getOrderLineBySku(sku, variantSku); 
    } 

    private static OrderLine getOrderLineBySku(string sku, string variantSku) 
    { 
     return String.IsNullOrWhiteSpace(variantSku) 
          ? getOrderLines().FirstOrDefault(l => (l.Sku == sku)) 
          : getOrderLines().FirstOrDefault(l => (l.Sku == sku && l.VariantSku == variantSku)); 
    } 

    private static OrderLine getOrderLineByOrderLineId(int? orderLineId) 
    { 
     return getOrderLines().FirstOrDefault(l => l.OrderLineId == orderLineId); 
    } 

    private static ICollection<OrderLine> getOrderLines() 
    { 
     return TransactionLibrary.GetBasket().PurchaseOrder.OrderLines; 
    } 
} 

你需要新的方法添加到uCommerce.jQuery.js還有這樣的事情:

addOrderLineProperty: function (options, onSuccess, onError) { 
    var defaults = { 
     orderLineId: 0 
    }; 
    var extendedOptions = $.extend(defaults, options); 
    callServiceStack({ AddOrderLineProperty: extendedOptions }, onSuccess, onError); 
} 

讓我知道,如果你有使用它的任何問題。

Tim