protected function CommerceDiscountTestBase::createDiscount in Commerce Discount 7
Create a discount.
Parameters
string $discount_type: The discount type; Either 'order_discount' or 'product_discount'.
string $offer_type: The discount offer type; One of 'fixed_amount', 'percentage', 'free_products', 'free_shipping', 'percent_off_shipping' or 'shipping_upgrade'.
int|array $amount: In case of 'fixed_amount' a number: the discount amount. In case of 'percentage' a number: the percentage (a number less than 1). In case of 'free_products' an array of the product ids. In case of 'free_shipping' a string of the free shipping service name. In case of 'percent_off_shipping' an array with the percentage amount (key: 'percent', should be < 100) and the shipping service name string (key: 'service'). In case of 'shipping_upgrade' an array with the source service (key: ' source') and the target service (key: 'target').
string $name: Discount name - Optional. If given, CANNOT start with a number.
string $component_title: Component title - Optional.
Return value
object The newly created commerce_discount entity.
23 calls to CommerceDiscountTestBase::createDiscount()
- CommerceDiscountConditionsTest::testCommerceProductContainsProducts in tests/
commerce_discount_conditions.test - Test commerce_product_contains_products() condition.
- CommerceDiscountConditionsTest::testOrderContainsProductsCondition in tests/
commerce_discount_conditions.test - Tests the 'commerce_order_contains_products' rule.
- CommerceDiscountShippingTest::testCommerceDiscountShippingAndFixedAmount in tests/
commerce_discount_shipping.test - Test a free shipping discount combined with a fixed amount discount.
- CommerceDiscountShippingTest::testCommerceDiscountShippingDiscounts in tests/
commerce_discount_shipping.test - Test shipping discounts.
- CommerceDiscountTest::testCartWithDiscountsDeleted in tests/
commerce_discount.test - Test discount deletion.
File
- tests/
commerce_discount_base.test, line 110 - Commerce Discounts test base.
Class
- CommerceDiscountTestBase
- Base class for commerce discount tests.
Code
protected function createDiscount($discount_type, $offer_type, $amount, $name = '', $component_title = '', $sort_order = 10) {
// 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;
case 'free_shipping':
$offer_wrapper->commerce_free_shipping = $amount;
$offer_wrapper->commerce_free_shipping_strategy = 'only_selected';
break;
case 'percent_off_shipping':
$offer_wrapper->commerce_percent_off_shipping = $amount['percent'];
if (!empty($amount[1])) {
$offer_wrapper->commerce_percent_off_ship_serv = $amount['service'];
}
break;
case 'shipping_upgrade':
$offer_wrapper->commerce_shipping_upgrade_target = $amount['target'];
$offer_wrapper->commerce_shipping_upgrade_source = $amount['source'];
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
->save();
return $discount_wrapper
->value();
}