pub struct Promotions { /* private fields */ }Expand description
Promotions and discounts management interface.
Implementations§
Source§impl Promotions
impl Promotions
Sourcepub fn create(&self, input: CreatePromotion) -> Result<Promotion>
pub fn create(&self, input: CreatePromotion) -> Result<Promotion>
Create a new promotion.
§Example
ⓘ
use stateset_embedded::{Commerce, CreatePromotion, PromotionType};
use rust_decimal_macros::dec;
let commerce = Commerce::new(":memory:")?;
// Create a percentage off promotion
let promo = commerce.promotions().create(CreatePromotion {
name: "20% Off Everything".into(),
promotion_type: PromotionType::PercentageOff,
percentage_off: Some(dec!(0.20)),
..Default::default()
})?;Sourcepub fn get_by_code(&self, code: &str) -> Result<Option<Promotion>>
pub fn get_by_code(&self, code: &str) -> Result<Option<Promotion>>
Get a promotion by its internal code.
Sourcepub fn update(
&self,
id: PromotionId,
input: UpdatePromotion,
) -> Result<Promotion>
pub fn update( &self, id: PromotionId, input: UpdatePromotion, ) -> Result<Promotion>
Update a promotion.
Sourcepub fn delete(&self, id: PromotionId) -> Result<()>
pub fn delete(&self, id: PromotionId) -> Result<()>
Delete a promotion.
Sourcepub fn activate(&self, id: PromotionId) -> Result<Promotion>
pub fn activate(&self, id: PromotionId) -> Result<Promotion>
Sourcepub fn deactivate(&self, id: PromotionId) -> Result<Promotion>
pub fn deactivate(&self, id: PromotionId) -> Result<Promotion>
Deactivate (pause) a promotion.
Sourcepub fn create_coupon(&self, input: CreateCouponCode) -> Result<CouponCode>
pub fn create_coupon(&self, input: CreateCouponCode) -> Result<CouponCode>
Create a coupon code for a promotion.
§Example
ⓘ
use stateset_embedded::{Commerce, CreateCouponCode};
use uuid::Uuid;
let commerce = Commerce::new(":memory:")?;
let coupon = commerce.promotions().create_coupon(CreateCouponCode {
promotion_id: Uuid::new_v4(),
code: "SUMMER25".into(),
usage_limit: Some(100),
..Default::default()
})?;Sourcepub fn get_coupon(&self, id: Uuid) -> Result<Option<CouponCode>>
pub fn get_coupon(&self, id: Uuid) -> Result<Option<CouponCode>>
Get a coupon by ID.
Sourcepub fn get_coupon_by_code(&self, code: &str) -> Result<Option<CouponCode>>
pub fn get_coupon_by_code(&self, code: &str) -> Result<Option<CouponCode>>
Get a coupon by its code (the code customers enter).
Sourcepub fn list_coupons(&self, filter: CouponFilter) -> Result<Vec<CouponCode>>
pub fn list_coupons(&self, filter: CouponFilter) -> Result<Vec<CouponCode>>
List coupons with optional filtering.
Sourcepub fn validate_coupon(&self, code: &str) -> Result<Option<CouponCode>>
pub fn validate_coupon(&self, code: &str) -> Result<Option<CouponCode>>
Validate a coupon code (check if it’s valid and can be used).
§Example
ⓘ
use stateset_embedded::Commerce;
let commerce = Commerce::new(":memory:")?;
match commerce.promotions().validate_coupon("SUMMER25")? {
Some(coupon) => println!("Valid coupon for promotion: {:?}", coupon.promotion_id),
None => println!("Invalid or expired coupon"),
}Sourcepub fn apply(
&self,
request: ApplyPromotionsRequest,
) -> Result<ApplyPromotionsResult>
pub fn apply( &self, request: ApplyPromotionsRequest, ) -> Result<ApplyPromotionsResult>
Apply promotions to a request (cart or order).
This is the main entry point for promotion calculation. It:
- Finds all applicable automatic promotions
- Validates any coupon codes provided
- Checks all promotion conditions
- Calculates discounts respecting stacking rules
- Returns detailed breakdown of applied discounts
§Example
ⓘ
use stateset_embedded::{Commerce, ApplyPromotionsRequest, PromotionLineItem};
use rust_decimal_macros::dec;
let commerce = Commerce::new(":memory:")?;
let result = commerce.promotions().apply(ApplyPromotionsRequest {
subtotal: dec!(150.00),
shipping_amount: dec!(10.00),
coupon_codes: vec!["SUMMER25".into()],
line_items: vec![PromotionLineItem {
id: "item-1".into(),
quantity: 2,
unit_price: dec!(75.00),
line_total: dec!(150.00),
..Default::default()
}],
..Default::default()
})?;
println!("Total discount: ${}", result.total_discount);
println!("Final total: ${}", result.grand_total);Sourcepub fn record_usage(
&self,
promotion_id: PromotionId,
coupon_id: Option<Uuid>,
customer_id: Option<CustomerId>,
order_id: Option<OrderId>,
cart_id: Option<CartId>,
discount_amount: Decimal,
currency: &str,
) -> Result<PromotionUsage>
pub fn record_usage( &self, promotion_id: PromotionId, coupon_id: Option<Uuid>, customer_id: Option<CustomerId>, order_id: Option<OrderId>, cart_id: Option<CartId>, discount_amount: Decimal, currency: &str, ) -> Result<PromotionUsage>
Record promotion usage (called after order completion).
This increments usage counts and creates an audit trail.
Sourcepub fn get_active(&self) -> Result<Vec<Promotion>>
pub fn get_active(&self) -> Result<Vec<Promotion>>
Get all active promotions.
Sourcepub fn is_valid(&self, id: PromotionId) -> Result<bool>
pub fn is_valid(&self, id: PromotionId) -> Result<bool>
Check if a promotion is currently valid.
Sourcepub fn add_condition(
&self,
promotion_id: PromotionId,
condition: CreatePromotionCondition,
) -> Result<Promotion>
pub fn add_condition( &self, promotion_id: PromotionId, condition: CreatePromotionCondition, ) -> Result<Promotion>
Add a condition to an existing promotion.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Promotions
impl !UnwindSafe for Promotions
impl Freeze for Promotions
impl Send for Promotions
impl Sync for Promotions
impl Unpin for Promotions
impl UnsafeUnpin for Promotions
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more