HomeHome Product Discus... Product Discus...RazorCartRazorCartRazorCart Details page add single itemRazorCart Details page add single item
Previous
 
Next
New Post
12/19/2018 8:29 PM
 
My current assignment is to devop a b2b site using RazorCart. On the detail page a button "Order Sample" is wanted. Our pages have 2 variant groups, color and size and the quantity field. Current business rules are that a sample can only be ordered once per order (but samples of different product/colors/sizes are fine) and the sample is always quantity=1 and half price.

I'm looking at the cshtml and don't see anything that directly maps back to a controller and function in the button code. I'm aware of the option to modify pipeline operations and there appears to be an object that is called and can have custom code for when the "Add To Cart" button is clicked. If I could even just send a boolean to that function which is true when it's a standard add and the quantities should be kept and false when it's a sample and should just be quantity=1 and the price adjusted, I think I could handle the code there just fine. Is there a way to do this?

Thank you,
George Butler

 
New Post
12/20/2018 6:35 PM
 
Hi George,

The pipeline would be the most efficient way to do what you're asking for, I'd recommend overriding the "PreAddToCart" action to do so. but since you're looking only for modifying the viewset then you'll have to write both C# and JS, we already populate the product history in "product.OrderHistory" but it does not contain any info about the variants, you can get this value using the browser's console like this: angular.element($0).controller().product.OrderHistory
But make sue to inspect any element of the details module first as $0 refers to the selected element within the inspector.

This is an example to achieve you business logic using angular.js only, you'll need to add this at the bottom of the _Layout.cshtml of the details module, in side @{ ... } is a c# code to retrieve a list of the selected variants by looping through the order history, then we'll pass this list to the angular controller model (vm) and use it while the user change the variants selection:

@{
    var orderDetailVariants = new List<ICollection<RazorCart.Service.Data.ModelDataContext.OrderDetailVariant>>();
    foreach (var history in Model.Product.OrderHistory)
    {
        var detailVariants = RazorCart.Service.Data.Repository.Instance.ListOrderDetailVariants(history.OrderID, history.OrderDetailID);
        orderDetailVariants.Add(detailVariants);
    }
}
<script type="text/javascript">
    let container@(Dnn.ModuleContext.ModuleId) = angular.element(document.getElementById('rzcContainer_@(Dnn.ModuleContext.ModuleId)'));
    container@(Dnn.ModuleContext.ModuleId).ready(function () {
        var scope = container@(Dnn.ModuleContext.ModuleId).scope();
        var vm = container@(Dnn.ModuleContext.ModuleId).controller();
        vm.canPurchase = { Value: true, Message: '' };
        vm.orderDetailVariants = JSON.parse('@Html.Raw(Json.Encode(orderDetailVariants))');
        scope.$watchCollection(function () {
            return vm.variantsData;
        }, function (newValue, oldVal) {
            vm.canPurchase.Value = true;
            angular.forEach(vm.orderDetailVariants, function (list, index) {
                var match = false;
                angular.forEach(list, function (item, index) {
                    angular.forEach(newValue, function (value, name) {
                        var groupId, variantId;
                        try {
                            groupId = name.substr(6, name.length - 1);
                            variantId = JSON.parse(value);
                        }
                        catch (e) { }
                        if (!isNaN(groupId)) {
                            if (item.VariantGroupID == groupId) {
                                if (item.VariantID == variantId)
                                    match = true;
                                else
                                    match = false;
                            }
                        }
                    });
                });
                if (match) {
                    vm.canPurchase.Value = false;
                    vm.canPurchase.Message = 'You have already bought this item before!';
                    return true;
                }
            });
        });
        scope.$apply(function () { });
    });
</script>

PS you still need to use vm.canPurchase in Index.cshtml to show/hide AddToCart button or display the message to the user, you can use ng-if or ng-hide/ng-show
 
Previous
 
Next
HomeHome Product Discus... Product Discus...RazorCartRazorCartRazorCart Details page add single itemRazorCart Details page add single item