Externally Adding Product Specific Promotions

Hi all,



I have done a fair amount of work for a few clients with cs-cart, however I have hit an issue with something which I have spent hours trying to figure out and can't, so I hope someone here can lend a hand.



I have been creating an external 'voucher manager' for one client, as they wish to run cron jobs to create vouchers for birthdays, abandoned carts etc. These I have done all perfectly fine.



I am however having an issue when working on their 're-buy this item for X% off' part of the system. I can create the promotions and gather the data I need all fine, there is just ONE tiny thing which I cannot get my head aorund.



This works by going through all orders placed in the previous day, and creating a scheduled reminder to trigger the creation of the promotion code + email it to the customer after X days from purchasing.



When creating the conditions of the promotion, and adding a product to it, I notice that it adds an 'item_id' into the database for the conditions.



I tried getting the item_id from the time the order was placed from the order_details table, and use this in the promotion data - but it is rarely ever the right one. It seems the item_id changes very often, and I cannot find anyway of being able to get the right item_id to insert into the promotion data.



Below is the content that puts the data into the database. $item_id is where the item_id needs to go.



Is there a way I can find out the correct item_id to go here? I find it odd how this seems to change, as this suggests that all promotions get updated when this changes, otherwise any item-specific promotion would break if the item_id was to change.



I need my promotion to be applicable to a specific product_id without caring for any unique options etc.



$conditions = 'a:3:{s:3:"set";s:3:"all";s:9:"set_value";s:1:"1";s:10:"conditions";a:3:{i:1;a:3:{s:8:"operator";s:2:"eq";s:9:"condition";s:11:"coupon_code";s:5:"value";s:10:"'.$voucherCode.'";}i:2;a:3:{s:8:"operator";s:2:"in";s:9:"condition";s:5:"users";s:5:"value";s:4:"'.$userId.'";}i:3;a:3:{s:8:"operator";s:3:"lte";s:9:"condition";s:16:"number_of_usages";s:5:"value";s:1:"1";}i:4;a:3:{s:8:"operator";s:2:"in";s:9:"condition";s:8:"products";s:5:"value";a:1:{i:'.$item_id.';a:2:{s:10:"product_id";s:'.strlen($pid).':"'.$pid.'";s:6:"amount";s:1:"1";}}}}}';




If I manually edit this in the database by copying a matching item and product id from a promotion created in the admin section it works, it is simply the item_id part that I cannot populate correctly…



This script has to be completely external from cs-cart files itself, I cannot modify any of them, but I can pull in any of them into my script if I need to.



The closest I got to finding where CS-Cart gets this item_id from is that when you add a product to a promotion in the admin panel it gives the 'name' field of the hidden input related to it this item_id, but I could not find in any cs-cart files where this value was getting returned, and where cs-cart was finding it in the first place!



Thanks for your help!

The index of the products within a cart or order are a unique id and are not related to the product. What you want to use is the product_id withing the current product. I believe you should think of the “item_id” as more of an “object_id” since it might be a category_id, option_id, product_id, etc…

Hi, the item_id inside the order_details table is not unique. Each item_id seems to be related to a single product_id, but each product_id may have many item_id's. Please take a look in the database for this table, and you will see item_id is NOT a category/object/product/option id. I cannot find these values anywhere else in the entire database, or any values that remotely resemble it.



This is the only location I see the item_id, and the object string for the promotion conditions needs both the product_id AND and item_id within it (see my code line for $item_id and $pid to see where these belong).



I know this is an item id, as if I create an example promotion in the admin panel, the number where $item_id is can be found in the database using “SELECT * FROM cscart_order_details WHERE item_id = ”. But how I find out what number to put here when creating these promotions myself is what I cannot determine.

Sorry, without digging through the code I can't be more specific. I do know that when an item is added to the cart, the index of that cart is in fact unique. Whether that translates to an item_id within the promotions, I'm not certain. Suggest you walk the promotions controller and see where it gets the item_id for creating the serialized info that is stored with the promotion.



Have you taken your 'item_id' value and done a search of your database for that value? It might show you where it is referencing.

Ill re-read the controller, but I saw no reference to it, which is why I am confused.



Searching the DB for a valid item_id only shows it in order_details table, nowhere else, which is odd to me :(

For a promotion where I have a coupon code that gives a discount on a specific product, I have:

cscart_promotions.conditions:


a:3:{s:3:"set";
s:3:"all";
s:9:"set_value";
s:1:"1";
s:10:"conditions";
a:1:{i:1;
a:3:{
s:8:"operator";
s:2:"eq";
s:9:"condition";
s:11:"coupon_code";
s:5:"value";s:7:"xxxx_10";
}
}
}


So I have no idea why you are not just using the product_id as the bonus and just leaving the coupon code be a coupon code. If you must use a product condition, then just use the product_id from the cart item you want to have it apply to.



The bonus is where the discount is applied to a specific product:

cscart_promotions.bonus:


a:1:{i:1;
a:4:{
s:5:"bonus";
s:20:"discount_on_products";
s:5:"value";
s:2:"40";
s:14:"discount_bonus";
s:8:"by_fixed";
s:14:"discount_value";
s:2:"10";
}
}


Which in this case is product_id 40 (value parameter).

Hmm… I see. So you have added the product as a bonus for the promotion.



I was going down the route of adding it as a condition (but it seems to add this item_id thingy), but I see now that if it is a condition, then the code will apply the discount to the whole basket >_< why didn’t I think of that before and try it as a bonus.



Thanks for the help, using it as a bonus makes a LOT more sense.



:)

Yes, what you're saying is if xyz coupon code is used and product_id 123 is in the cart, then apply the bonus. Otherwise the coupon doesn't apply to this purchase since the bonus is only applied to that product. It is a 'cart' promotion that applies only to a specific product.