protected function CommerceCouponTestBase::createDiscount in Commerce Coupon 7.2
Create a discount.
Parameters
string $discount_type: The discount type; Either 'order_discount' or 'product_discount'.
string $offer_type: The discount offer type; Either 'fixed_amount' or 'percentage'.
int|array $amount: The discount offer amount, percentage or product ids array.
string $name: Discount name - Optional. If given, CANNOT start with a number.
string $component_title: Component title - Optional.
string $coupon_strategy: Coupon strategy. Either 'once' or 'multi'. Defaults to 'once'.
Return value
CommerceDiscount The newly created commerce_discount entity.
See also
\CommerceDiscountTestBase::createDiscount()
6 calls to CommerceCouponTestBase::createDiscount()
- CommerceCouponLoadCouponCodeDiscountsTest::testCommerceCouponLoadCouponCodeDiscounts in ./
commerce_coupon.test - CommerceCouponLoadCouponCodeDiscountsTest::testCommerceCouponUIEditCoupon in ./
commerce_coupon.test - CommerceCouponTest::testCommerceCouponCouponCount in ./
commerce_coupon.test - CommerceCouponTest::testCommerceCouponUIAddCoupon in ./
commerce_coupon.test - CommerceCouponTest::testCommerceCouponUICartForm in ./
commerce_coupon.test
File
- ./
commerce_coupon.test, line 108 - Commerce Coupon tests.
Class
- CommerceCouponTestBase
- Base class for Commerce Discount tests
Code
protected function createDiscount($discount_type, $offer_type, $amount, $name = '', $component_title = '', $sort_order = 10, $coupon_strategy = 'once') {
// Create the discount offer.
$commerce_discount_offer = entity_create('commerce_discount_offer', array(
'type' => $offer_type,
));
$offer_wrapper = entity_metadata_wrapper('commerce_discount_offer', $commerce_discount_offer);
switch ($offer_type) {
case 'fixed_amount':
$offer_wrapper->commerce_fixed_amount->amount = $amount;
$offer_wrapper->commerce_fixed_amount->currency_code = 'USD';
break;
case 'percentage':
$offer_wrapper->commerce_percentage = $amount;
break;
case 'free_products':
// Product ids array should be provided for $amount.
$offer_wrapper->commerce_free_products = $amount;
break;
}
$offer_wrapper
->save();
// Provide default name.
$name = $name ? $name : $discount_type . '_' . $offer_type;
$component_title = $component_title ? $component_title : $name;
// Create the discount.
$values = array(
'name' => $name,
'label' => $name,
'type' => $discount_type,
'sort_order' => $sort_order,
'component_title' => $component_title,
'status' => TRUE,
'export_status' => TRUE,
);
$commerce_discount = entity_create('commerce_discount', $values);
$discount_wrapper = entity_metadata_wrapper('commerce_discount', $commerce_discount);
$discount_wrapper->commerce_discount_offer = $commerce_discount_offer;
$discount_wrapper->commerce_coupon_strategy = $coupon_strategy;
$discount_wrapper
->save();
return $discount_wrapper
->value();
}