Adding Discount to the fresh Customers

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
Hello there,

i am using nopcommerce 1.9 to develop my project.

What i want to do is,when a customer comes to my website and places any order then i want to give 5%
discount to that customer for the whole order(if he is completely new customer. i.e he is placing the first order then & only then).

If that particular customer has placed an order previously then discount won't apply..

I m stuck on few things..

I have reffered Add to cart code..but i don't know where to code & achieve this..

Please help me..

Thnak you
12 years ago
Finally i found it..


var x = this.GetOrdersByCustomerId(CustomerId);
                    if (x.Count == 0)
                    {
                        decimal totalprice = 0;
                        int i = cart.Count();
                        for (int j = 0; j < i; j++)
                        {
                            totalprice += cart[j].ProductVariant.Price * cart[j].Quantity;
                        }
                        z = totalprice * 5 / 100;
                        orderSubTotalInclTax = totalprice - z;
                        orderSubTotalExclTax = totalprice - z;
                        orderSubtotalExclTaxInCustomerCurrency = totalprice - z;
                    }
12 years ago
I did it in another way
i created one another column having name Welcome_discount into the "dbo.Nop_Order" table with decimal datatype.

And i did the code in Orderservice.cs class--

var x = this.GetOrdersByCustomerId(CustomerId);
                    if (x.Count == 0)
                    {
                        decimal totalprice = 0;
                        int i = cart.Count();
                        for (int j = 0; j < i; j++)
                        {
                            totalprice += cart[j].ProductVariant.Price * cart[j].Quantity;
                        }
                        z = totalprice * 5 / 100;
                        orderSubTotalInclTax = totalprice - z;
                        orderSubTotalExclTax = totalprice - z;
                        orderSubtotalExclTaxInCustomerCurrency = totalprice - z;
                    }

then

var order = new Order()
                        {
                            OrderGuid = orderGuid,
                            CustomerId = customer.CustomerId,
                            CustomerLanguageId = customerLanguageId,
                            CustomerTaxDisplayTypeId = (int)customerTaxDisplayType,
                            CustomerIP = NopContext.Current.UserHostAddress,
                            Welcome_discount = z,
                            OrderSubtotalInclTax = orderSubTotalInclTax,
                            OrderSubtotalExclTax = orderSubTotalExclTax,
                            OrderSubTotalDiscountInclTax = orderSubTotalDiscountInclTax,
                            OrderSubTotalDiscountExclTax = orderSubTotalDiscountExclTax,
                            OrderShippingInclTax = orderShippingTotalInclTax.Value,
                            OrderShippingExclTax = orderShippingTotalExclTax.Value,
                            PaymentMethodAdditionalFeeInclTax = paymentAdditionalFeeInclTax,
                            PaymentMethodAdditionalFeeExclTax = paymentAdditionalFeeExclTax,
                            TaxRates = taxRates,
                            OrderTax = orderTaxTotal,
                            OrderTotal = orderTotal.Value,
                            RefundedAmount = decimal.Zero,
                            OrderDiscount = orderDiscountAmount,
                            OrderSubtotalInclTaxInCustomerCurrency = orderSubtotalInclTaxInCustomerCurrency,
                            OrderSubtotalExclTaxInCustomerCurrency = orderSubtotalExclTaxInCustomerCurrency,
                            OrderSubTotalDiscountInclTaxInCustomerCurrency = orderSubTotalDiscountInclTaxInCustomerCurrency,
                            OrderSubTotalDiscountExclTaxInCustomerCurrency = orderSubTotalDiscountExclTaxInCustomerCurrency,
                            OrderShippingInclTaxInCustomerCurrency = orderShippingInclTaxInCustomerCurrency,
                            OrderShippingExclTaxInCustomerCurrency = orderShippingExclTaxInCustomerCurrency,
                            PaymentMethodAdditionalFeeInclTaxInCustomerCurrency = paymentAdditionalFeeInclTaxInCustomerCurrency,
                            PaymentMethodAdditionalFeeExclTaxInCustomerCurrency = paymentAdditionalFeeExclTaxInCustomerCurrency,
                            TaxRatesInCustomerCurrency = taxRatesInCustomerCurrency,
                            OrderTaxInCustomerCurrency = orderTaxInCustomerCurrency,
                            OrderTotalInCustomerCurrency = orderTotalInCustomerCurrency,
                            OrderDiscountInCustomerCurrency = orderDiscountInCustomerCurrency,
                            CheckoutAttributeDescription = checkoutAttributeDescription,
                            CheckoutAttributesXml = checkoutAttributesXml,
                            CustomerCurrencyCode = customerCurrencyCode,
                            OrderWeight = orderWeight,
                            AffiliateId = customer.AffiliateId,
                            OrderStatusId = (int)OrderStatusEnum.Pending,
                            AllowStoringCreditCardNumber = processPaymentResult.AllowStoringCreditCardNumber,
                            CardType = processPaymentResult.AllowStoringCreditCardNumber ? SecurityHelper.Encrypt(paymentInfo.CreditCardType) : string.Empty,
                            CardName = processPaymentResult.AllowStoringCreditCardNumber ? SecurityHelper.Encrypt(paymentInfo.CreditCardName) : string.Empty,
                            CardNumber = processPaymentResult.AllowStoringCreditCardNumber ? SecurityHelper.Encrypt(paymentInfo.CreditCardNumber) : string.Empty,
                            MaskedCreditCardNumber = SecurityHelper.Encrypt(paymentService.GetMaskedCreditCardNumber(paymentInfo.CreditCardNumber)),
                            CardCvv2 = processPaymentResult.AllowStoringCreditCardNumber ? SecurityHelper.Encrypt(paymentInfo.CreditCardCvv2) : string.Empty,
                            CardExpirationMonth = processPaymentResult.AllowStoringCreditCardNumber ? SecurityHelper.Encrypt(paymentInfo.CreditCardExpireMonth.ToString()) : string.Empty,
                            CardExpirationYear = processPaymentResult.AllowStoringCreditCardNumber ? SecurityHelper.Encrypt(paymentInfo.CreditCardExpireYear.ToString()) : string.Empty,
                            PaymentMethodId = paymentInfo.PaymentMethodId,
                            PaymentMethodName = paymentMethodName,
                            AuthorizationTransactionId = processPaymentResult.AuthorizationTransactionId,
                            AuthorizationTransactionCode = processPaymentResult.AuthorizationTransactionCode,
                            AuthorizationTransactionResult = processPaymentResult.AuthorizationTransactionResult,
                            CaptureTransactionId = processPaymentResult.CaptureTransactionId,
                            CaptureTransactionResult = processPaymentResult.CaptureTransactionResult,
                            SubscriptionTransactionId = processPaymentResult.SubscriptionTransactionId,
                            PurchaseOrderNumber = paymentInfo.PurchaseOrderNumber,
                            PaymentStatusId = (int)processPaymentResult.PaymentStatus,
                            PaidDate = null,
                            BillingFirstName = billingFirstName,
                            BillingLastName = billingLastName,
                            BillingPhoneNumber = billingPhoneNumber,
                            BillingEmail = billingEmail,
                            BillingFaxNumber = billingFaxNumber,
                            BillingCompany = billingCompany,
                            BillingAddress1 = billingAddress1,
                            BillingAddress2 = billingAddress2,
                            BillingCity = billingCity,
                            BillingStateProvince = billingStateProvince,
                            BillingStateProvinceId = billingStateProvinceId,
                            BillingZipPostalCode = billingZipPostalCode,
                            BillingCountry = billingCountry,
                            BillingCountryId = billingCountryId,
                            ShippingStatusId = (int)shippingStatusEnum,
                            ShippingFirstName = shippingFirstName,
                            ShippingLastName = shippingLastName,
                            ShippingPhoneNumber = shippingPhoneNumber,
                            ShippingEmail = shippingEmail,
                            ShippingFaxNumber = shippingFaxNumber,
                            ShippingCompany = shippingCompany,
                            ShippingAddress1 = shippingAddress1,
                            ShippingAddress2 = shippingAddress2,
                            ShippingCity = shippingCity,
                            ShippingStateProvince = shippingStateProvince,
                            ShippingStateProvinceId = shippingStateProvinceId,
                            ShippingZipPostalCode = shippingZipPostalCode,
                            ShippingCountry = shippingCountry,
                            ShippingCountryId = shippingCountryId,
                            ShippingMethod = shippingMethodName,
                            ShippingRateComputationMethodId = shippingRateComputationMethodId,
                            ShippedDate = null,
                            DeliveryDate = null,
                            TrackingNumber = string.Empty,
                            VatNumber = vatNumber,
                            Deleted = false,
                            CreatedOn = DateTime.UtcNow
                        };
                        InsertOrder(order);


for getting all the parameters for the order..


And finally to indsert order i did----

public void InsertOrder(Order order)
        {
            if (order == null)
                throw new ArgumentNullException("order");

            order.TaxRates = CommonHelper.EnsureNotNull(order.TaxRates);
            order.TaxRatesInCustomerCurrency = CommonHelper.EnsureNotNull(order.TaxRatesInCustomerCurrency);
            order.CustomerIP = CommonHelper.EnsureNotNull(order.CustomerIP);
            order.CheckoutAttributeDescription = CommonHelper.EnsureNotNull(order.CheckoutAttributeDescription);
            order.CheckoutAttributesXml = CommonHelper.EnsureNotNull(order.CheckoutAttributesXml);
            order.CardType = CommonHelper.EnsureNotNull(order.CardType);
            order.CardName = CommonHelper.EnsureNotNull(order.CardName);
            order.CardNumber = CommonHelper.EnsureNotNull(order.CardNumber);
            order.MaskedCreditCardNumber = CommonHelper.EnsureNotNull(order.MaskedCreditCardNumber);
            order.CardCvv2 = CommonHelper.EnsureNotNull(order.CardCvv2);
            order.CardExpirationMonth = CommonHelper.EnsureNotNull(order.CardExpirationMonth);
            order.CardExpirationYear = CommonHelper.EnsureNotNull(order.CardExpirationYear);
            order.PaymentMethodName = CommonHelper.EnsureNotNull(order.PaymentMethodName);
            order.AuthorizationTransactionId = CommonHelper.EnsureNotNull(order.AuthorizationTransactionId);
            order.AuthorizationTransactionCode = CommonHelper.EnsureNotNull(order.AuthorizationTransactionCode);
            order.AuthorizationTransactionResult = CommonHelper.EnsureNotNull(order.AuthorizationTransactionResult);
            order.CaptureTransactionId = CommonHelper.EnsureNotNull(order.CaptureTransactionId);
            order.CaptureTransactionResult = CommonHelper.EnsureNotNull(order.CaptureTransactionResult);
            order.SubscriptionTransactionId = CommonHelper.EnsureNotNull(order.SubscriptionTransactionId);
            order.PurchaseOrderNumber = CommonHelper.EnsureNotNull(order.PurchaseOrderNumber);
            order.BillingFirstName = CommonHelper.EnsureNotNull(order.BillingFirstName);
            order.BillingLastName = CommonHelper.EnsureNotNull(order.BillingLastName);
            order.BillingPhoneNumber = CommonHelper.EnsureNotNull(order.BillingPhoneNumber);
            order.BillingEmail = CommonHelper.EnsureNotNull(order.BillingEmail);
            order.BillingFaxNumber = CommonHelper.EnsureNotNull(order.BillingFaxNumber);
            order.BillingCompany = CommonHelper.EnsureNotNull(order.BillingCompany);
            order.BillingAddress1 = CommonHelper.EnsureNotNull(order.BillingAddress1);
            order.BillingAddress2 = CommonHelper.EnsureNotNull(order.BillingAddress2);
            order.BillingCity = CommonHelper.EnsureNotNull(order.BillingCity);
            order.BillingStateProvince = CommonHelper.EnsureNotNull(order.BillingStateProvince);
            order.BillingZipPostalCode = CommonHelper.EnsureNotNull(order.BillingZipPostalCode);
            order.BillingCountry = CommonHelper.EnsureNotNull(order.BillingCountry);
            order.ShippingFirstName = CommonHelper.EnsureNotNull(order.ShippingFirstName);
            order.ShippingLastName = CommonHelper.EnsureNotNull(order.ShippingLastName);
            order.ShippingPhoneNumber = CommonHelper.EnsureNotNull(order.ShippingPhoneNumber);
            order.ShippingEmail = CommonHelper.EnsureNotNull(order.ShippingEmail);
            order.ShippingFaxNumber = CommonHelper.EnsureNotNull(order.ShippingFaxNumber);
            order.ShippingCompany = CommonHelper.EnsureNotNull(order.ShippingCompany);
            order.ShippingAddress1 = CommonHelper.EnsureNotNull(order.ShippingAddress1);
            order.ShippingAddress2 = CommonHelper.EnsureNotNull(order.ShippingAddress2);
            order.ShippingCity = CommonHelper.EnsureNotNull(order.ShippingCity);
            order.ShippingStateProvince = CommonHelper.EnsureNotNull(order.ShippingStateProvince);
            order.ShippingZipPostalCode = CommonHelper.EnsureNotNull(order.ShippingZipPostalCode);
            order.ShippingCountry = CommonHelper.EnsureNotNull(order.ShippingCountry);
            order.ShippingMethod = CommonHelper.EnsureNotNull(order.ShippingMethod);
            order.TrackingNumber = CommonHelper.EnsureNotNull(order.TrackingNumber);
            order.VatNumber = CommonHelper.EnsureNotNull(order.VatNumber);
            
            order.BillingEmail = order.BillingEmail.Trim();
            order.ShippingEmail = order.ShippingEmail.Trim();
            
            order.TaxRates = CommonHelper.EnsureMaximumLength(order.TaxRates, 4000);
            order.TaxRatesInCustomerCurrency = CommonHelper.EnsureMaximumLength(order.TaxRatesInCustomerCurrency, 4000);
            order.CustomerIP = CommonHelper.EnsureMaximumLength(order.CustomerIP, 50);

            order.Welcome_discount = order.Welcome_discount;

            order.CardType = CommonHelper.EnsureMaximumLength(order.CardType, 100);
            order.CardName = CommonHelper.EnsureMaximumLength(order.CardName, 1000);
            order.CardNumber = CommonHelper.EnsureMaximumLength(order.CardNumber, 100);
            order.MaskedCreditCardNumber = CommonHelper.EnsureMaximumLength(order.MaskedCreditCardNumber, 100);
            order.CardCvv2 = CommonHelper.EnsureMaximumLength(order.CardCvv2, 100);
            order.CardExpirationMonth = CommonHelper.EnsureMaximumLength(order.CardExpirationMonth, 100);
            order.CardExpirationYear = CommonHelper.EnsureMaximumLength(order.CardExpirationYear, 100);
            order.PaymentMethodName = CommonHelper.EnsureMaximumLength(order.PaymentMethodName, 100);
            order.AuthorizationTransactionId = CommonHelper.EnsureMaximumLength(order.AuthorizationTransactionId, 4000);
            order.AuthorizationTransactionCode = CommonHelper.EnsureMaximumLength(order.AuthorizationTransactionCode, 4000);
            order.AuthorizationTransactionResult = CommonHelper.EnsureMaximumLength(order.AuthorizationTransactionResult, 4000);
            order.CaptureTransactionId = CommonHelper.EnsureMaximumLength(order.CaptureTransactionId, 4000);
            order.CaptureTransactionResult = CommonHelper.EnsureMaximumLength(order.CaptureTransactionResult, 4000);
            order.SubscriptionTransactionId = CommonHelper.EnsureMaximumLength(order.SubscriptionTransactionId, 4000);
            order.PurchaseOrderNumber = CommonHelper.EnsureMaximumLength(order.PurchaseOrderNumber, 100);
            order.BillingFirstName = CommonHelper.EnsureMaximumLength(order.BillingFirstName, 100);
            order.BillingLastName = CommonHelper.EnsureMaximumLength(order.BillingLastName, 100);
            order.BillingPhoneNumber = CommonHelper.EnsureMaximumLength(order.BillingPhoneNumber, 50);
            order.BillingEmail = CommonHelper.EnsureMaximumLength(order.BillingEmail, 255);
            order.BillingFaxNumber = CommonHelper.EnsureMaximumLength(order.BillingFaxNumber, 50);
            order.BillingCompany = CommonHelper.EnsureMaximumLength(order.BillingCompany, 100);
            order.BillingAddress1 = CommonHelper.EnsureMaximumLength(order.BillingAddress1, 100);
            order.BillingAddress2 = CommonHelper.EnsureMaximumLength(order.BillingAddress2, 100);
            order.BillingCity = CommonHelper.EnsureMaximumLength(order.BillingCity, 100);
            order.BillingStateProvince = CommonHelper.EnsureMaximumLength(order.BillingStateProvince, 100);
            order.BillingZipPostalCode = CommonHelper.EnsureMaximumLength(order.BillingZipPostalCode, 30);
            order.BillingCountry = CommonHelper.EnsureMaximumLength(order.BillingCountry, 100);
            order.ShippingFirstName = CommonHelper.EnsureMaximumLength(order.ShippingFirstName, 100);
            order.ShippingLastName = CommonHelper.EnsureMaximumLength(order.ShippingLastName, 100);
            order.ShippingPhoneNumber = CommonHelper.EnsureMaximumLength(order.ShippingPhoneNumber, 50);
            order.ShippingEmail = CommonHelper.EnsureMaximumLength(order.ShippingEmail, 255);
            order.ShippingFaxNumber = CommonHelper.EnsureMaximumLength(order.ShippingFaxNumber, 50);
            order.ShippingCompany = CommonHelper.EnsureMaximumLength(order.ShippingCompany, 100);
            order.ShippingAddress1 = CommonHelper.EnsureMaximumLength(order.ShippingAddress1, 100);
            order.ShippingAddress2 = CommonHelper.EnsureMaximumLength(order.ShippingAddress2, 100);
            order.ShippingCity = CommonHelper.EnsureMaximumLength(order.ShippingCity, 100);
            order.ShippingStateProvince = CommonHelper.EnsureMaximumLength(order.ShippingStateProvince, 100);
            order.ShippingZipPostalCode = CommonHelper.EnsureMaximumLength(order.ShippingZipPostalCode, 30);
            order.ShippingCountry = CommonHelper.EnsureMaximumLength(order.ShippingCountry, 100);
            order.ShippingMethod = CommonHelper.EnsureMaximumLength(order.ShippingMethod, 100);
            order.TrackingNumber = CommonHelper.EnsureMaximumLength(order.TrackingNumber, 100);
            order.VatNumber = CommonHelper.EnsureMaximumLength(order.VatNumber, 100);

            

            _context.Orders.AddObject(order);
            _context.SaveChanges();

            //quickbooks
            if (IoC.Resolve<IQBService>().QBIsEnabled)
            {
                IoC.Resolve<IQBService>().RequestSynchronization(order);
            }

            //raise event            
            EventContext.Current.OnOrderCreated(null,
                new OrderEventArgs() { Order = order });
        }

Now it works fine but the value of Welcome_discount is not getting inserted in the table..

I don't understand what exactly the problem is.

Do needful on ergent basis.

Thank u
12 years ago
Try having them register for the newsletter and then insert a promo into the auto email for registration.  They use the promo during checkout and you add someone to your newsletter for future sales...
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.