Search code examples
prestashop-1.7promotions

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


I want to create a feature for promotions in prestashop but this does not work

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....

 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);

                    }
                    
                }
                con.Close();
            }
        }

Solution

  • Just set that to zero.

    When creating a specific_price, leaving values to 0 means that price will be applied to all entity values.

    Also you should set id_cart = 0 too, because you don't want specific prices being binded to specific customer carts, I guess.