How to I apply a Discount to a product using Coding (Programatically)

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 anni tempo fa
Regards, I have currently implementing a service to shop in nopcommerce from outside. So I call this method and I pass the coupon code for all products that I get and create dynamically to add to the card. So the product does not already exists in my Ecommerce database or may they do but I am not using the Views to enter de coupon code. It is all done by coding. So I used product.AppliedDiscounts.Add(MyDiscount) but I am not quite sure if it really applies it at the end of the process when the payment processor is called.

Regards.
6 anni tempo fa
ivancarmenates wrote:
Regards, I have currently implementing a service to shop in nopcommerce from outside. So I call this method and I pass the coupon code for all products that I get and create dynamically to add to the card. So the product does not already exists in my Ecommerce database or may they do but I am not using the Views to enter de coupon code. It is all done by coding. So I used product.AppliedDiscounts.Add(MyDiscount) but I am not quite sure if it really applies it at the end of the process when the payment processor is called.

Regards.


Yes, will apply. I think there is no cause for not applying if you code the right way. Everything is done by code nothing else.
6 anni tempo fa
Thanks for your quick answer I did try to reply this yesterday buy I lose all the text I was writing. I did manage to solve my problem so here is it. I hope it could be useful for someone. Unfortunately for me I have never been helped in this forums the support is nice but not enough.

Well I magically find out that to apply discounts you have to use a custom attribute instead of just trying to apply the discounts thorough 'product.AppliedDiscounts.Add(...)' and setting the property 'product.HasDiscountsApplied' to true. So must you use _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.DiscountCouponCode, discountCouponCode);

So the first thing I did since I am trying to buy a product from another client sending the data to a controller action in NopCommerce was to clear applied discounts for the product; you may found your self applying many discounts instead of just the one you want. So here is the code.

...
var discount = _discountService.GetDiscountByCouponCode(...discountCouponCode...);
var product = _productService.GetProductBySku(...productSku...);
var customer = _customerService.GetCustomerByEmail(...email...);
...

// External buying of this product so clears applied discounts.
product.AppliedDiscounts.Clear();
product.HasDiscountsApplied = false;

if (discount != null)
{
    // Determines whether the coupon is still valid to apply to the product.
    if (_discountService.IsDiscountValid(discount, customer, discountCouponCode))
    {
        // Applies discount to product.
        product.AppliedDiscounts.Add(discount);
        product.HasDiscountsApplied = true;

        // Sets the discount attribute so the PlaceOrder method knows there is a discount.
        _genericAttributeService.SaveAttribute(customer,
            SystemCustomerAttributeNames.DiscountCouponCode, discountCouponCode);
    }
}

// Adds product to cart
_shoppingCartService.AddToCart(customer, product, ShoppingCartType.ShoppingCart,
                              _storeContext.CurrentStore.Id, quantity: 1);

// Now the order should be placed so the payment method can handle it.
var processPaymentRequest = new ProcessPaymentRequest
{
    StoreId = _storeContext.CurrentStore.Id,
    CustomerId = customer.Id,
    PaymentMethodSystemName = "...YourPaymentMethod..."
};

var placeOrderResult = _orderProcessingService.PlaceOrder(processPaymentRequest);
...

I hope this works for someone,
Cheers.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.