Jump to content

Trying to develop a feature in Prestashop to apply promotions


nicoleta_voic

Recommended Posts

I'm trying to develop a feature in prestashop to apply promotions.

 

i have set up this code    

public static void Promotion()
        {
            using (SqlConnection con = new SqlConnection(Properties.Settings.Default.HQRConnectionString))
            {
                con.Open();

                string cmdString =
                    "select"
                    + " P.productId, Pd.price, PD.promotionId "
                    + " from promotiondetails PD "
                    + " join products P "
                    + " on P.productId = Pd.productId "
                    + " where P.isecommerceenabled = '1' "
                    + " and PD.promotionId in (select promotionid from promotionstores where storeid = @StoreId)"; 


                SqlCommand cmd = new SqlCommand(cmdString, con);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Add("@StoreId", SqlDbType.Int).Value = Settings.Default.WebStoreId;

                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                sda.Fill(table);

                foreach (DataRow row in table.Rows)
                {
                   
                    string productId = Convert.ToString(row["ProductId"]);
                    IPosXPO.Products product = unitOfWork.GetObjectByKey<Products>(productId);
                    Bukimedia.PrestaSharp.Entities.product prod = GetPrestashopProduct(product);

                    if (product.IsEcommerceEnabled && prod != null)
                        {
                        specific_price specificPrice = new specific_price();
                        specificPrice.id_product = prod.id;
                        
                        int promotionId= Convert.ToInt16(row["PromotionId"]);
                        IPosXPO.Promotions promotion = unitOfWork.GetObjectByKey<Promotions>(promotionId);
                        //   string format = Convert.ToString(promotion.StartDate);
                        //DateTime dt = DateTime.ParseExact(format, "YYYY-MM-DD HH:MM:SS", CultureInfo.InvariantCulture);

                        int year = promotion.StartDate.Year;
                        int month = promotion.StartDate.Month;
                        int day = promotion.StartDate.Day;
                        specificPrice.from =  year + "-" + month + "-" + day + " " + "00" + ":" + "00" + ":" + "00";
                     

                       
                         year = promotion.EndDate.Year;
                         month = promotion.EndDate.Month;
                        day = promotion.EndDate.Day;
                        specificPrice.to = year + "-" + month + "-" + day + " " + "00" + ":" + "00" + ":" + "00";

                        decimal price = Convert.ToDecimal(row["Price"]);
                        specificPrice.id_shop = Settings.Default.WebStoreId;
                        specificPrice.price = price;
                        specificPrice.id_cart = 1;
                       Currencies currency = unitOfWork.GetObjectByKey<Currencies>("EUR");
                        specificPrice.id_currency = 1;
                        specificPrice.id_group = 1;
                        specificPrice.id_customer = null;
                        specificPrice.reduction_type = "amount";
                        specificPrice.reduction_tax = 1;
                        specificPrice.id_country = promotion.CountryId.CountryId;
                        specificPriceFactory.Add(specificPrice);

                    }

 

i want to create multiple specific prices fro several products as you can see. My problem is this code does not work.

i harcoded   specificPrice.id_group = 1;  specificPrice.id_group = 1;      specificPrice.id_cart = 1; because they are mandatory but i dont have them in my data base. Can someone help me find the right solution? i want to create a specific price for multiple products to apply to all groups, all coostumers but i don't know what id should i pass to    specificPrice.id_group....

this  is my specificprice class


 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Bukimedia.PrestaSharp.Entities
{
    [XmlType(Namespace = "Bukimedia/PrestaSharp/Entities")]
    public class specific_price : PrestaShopEntity, IPrestaShopFactoryEntity
    {
        public long? id { get; set; }
        public long? id_shop_group { get; set; }
        public long? id_shop { get; set; }
        public long? id_cart { get; set; }
        public long? id_product { get; set; }
        public long? id_product_attribute { get; set; }
        public long? id_currency { get; set; }
        public long? id_country { get; set; }
        public long? id_group { get; set; }
        public long? id_customer { get; set; }
        public long? id_specific_price_rule { get; set; }
        public decimal price { get; set; }
        public int from_quantity { get; set; }
        public decimal reduction { get; set; }
        /// <summary>
        /// Reduction type is either "amount" or "percentage"
        /// </summary>
        public string reduction_type { get; set; }
        /// <summary>
        /// It´s a logical bool.
        /// </summary>
        public int reduction_tax { get; set; }
        /// <summary>
        /// It´s a logical DateTime. Format YYYY-MM-DD HH:MM:SS.
        /// </summary>
        public string from { get; set; }
        /// <summary>
        /// It´s a logical DateTime. Format YYYY-MM-DD HH:MM:SS.
        /// </summary>
        public string to { get; set; }

        public specific_price()
        {
            from = "0000-00-00 00:00:00";
            to = "0000-00-00 00:00:00";
        }
    }
}

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...