147 lines
3.5 KiB
PHP
147 lines
3.5 KiB
PHP
<?php
|
|||
|
|
/**
|
||
|
|
* Build the shop state the smoke test asserts against.
|
||
|
|
*
|
||
|
|
* Run inside the throwaway site with `wp eval-file`, so WordPress and
|
||
|
|
* WooCommerce are fully loaded and this is real data in real tables — not a
|
||
|
|
* mocked cart that would keep passing after WooCommerce changed underneath it.
|
||
|
|
*
|
||
|
|
* @package WC_Email_Discount_Manager
|
||
|
|
*/
|
||
|
|
|
||
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create a product category and return its term ID.
|
||
|
|
*
|
||
|
|
* @param string $name Category name.
|
||
|
|
* @return int
|
||
|
|
*/
|
||
|
|
function wc_edm_ci_category( $name ) {
|
||
|
|
$term = term_exists( $name, 'product_cat' );
|
||
|
|
if ( ! $term ) {
|
||
|
|
$term = wp_insert_term( $name, 'product_cat' );
|
||
|
|
}
|
||
|
|
return (int) $term['term_id'];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create a simple product and return its ID.
|
||
|
|
*
|
||
|
|
* @param string $name Product name.
|
||
|
|
* @param float $price Price.
|
||
|
|
* @param int $category Product category term ID.
|
||
|
|
* @return int
|
||
|
|
*/
|
||
|
|
function wc_edm_ci_product( $name, $price, $category ) {
|
||
|
|
$product = new WC_Product_Simple();
|
||
|
|
$product->set_name( $name );
|
||
|
|
$product->set_regular_price( $price );
|
||
|
|
$product->set_category_ids( array( $category ) );
|
||
|
|
$product->set_catalog_visibility( 'visible' );
|
||
|
|
$product->set_stock_status( 'instock' );
|
||
|
|
return $product->save();
|
||
|
|
}
|
||
|
|
|
||
|
|
$discounted = wc_edm_ci_category( 'CI Discounted' );
|
||
|
|
$other = wc_edm_ci_category( 'CI Other' );
|
||
|
|
|
||
|
|
$product_in = wc_edm_ci_product( 'CI product in group', 100, $discounted );
|
||
|
|
$product_out = wc_edm_ci_product( 'CI product outside group', 50, $other );
|
||
|
|
|
||
|
|
$group_id = WC_EDM_DB::insert_group(
|
||
|
|
array(
|
||
|
|
'name' => 'CI Group',
|
||
|
|
'discount_percent' => 20,
|
||
|
|
)
|
||
|
|
);
|
||
|
|
WC_EDM_DB::set_group_categories( $group_id, array( $discounted ) );
|
||
|
|
|
||
|
|
$coupon = new WC_Coupon();
|
||
|
|
$coupon->set_code( 'ciflat10' );
|
||
|
|
$coupon->set_discount_type( 'fixed_cart' );
|
||
|
|
$coupon->set_amount( 10 );
|
||
|
|
$coupon->save();
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Insert an email assignment, filling in the columns the caller did not name.
|
||
|
|
*
|
||
|
|
* @param array $data Partial assignment row.
|
||
|
|
* @return int
|
||
|
|
*/
|
||
|
|
function wc_edm_ci_assign( $data ) {
|
||
|
|
return WC_EDM_DB::insert_email(
|
||
|
|
array_merge(
|
||
|
|
array(
|
||
|
|
'assignment_type' => 'group',
|
||
|
|
'group_id' => null,
|
||
|
|
'coupon_code' => null,
|
||
|
|
'custom_discount' => null,
|
||
|
|
'is_active' => 1,
|
||
|
|
'free_shipping' => 0,
|
||
|
|
),
|
||
|
|
$data
|
||
|
|
)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
// The three pattern kinds, arranged so that priority is observable:
|
||
|
|
// exact@company.com is matched by BOTH the exact rule and the domain rule, and
|
||
|
|
// "partial" is contained in partialperson@elsewhere.test.
|
||
|
|
wc_edm_ci_assign(
|
||
|
|
array(
|
||
|
|
'email_pattern' => '@company.com',
|
||
|
|
'assignment_type' => 'group',
|
||
|
|
'group_id' => $group_id,
|
||
|
|
)
|
||
|
|
);
|
||
|
|
wc_edm_ci_assign(
|
||
|
|
array(
|
||
|
|
'email_pattern' => 'exact@company.com',
|
||
|
|
'assignment_type' => 'custom',
|
||
|
|
'custom_discount' => 50,
|
||
|
|
)
|
||
|
|
);
|
||
|
|
wc_edm_ci_assign(
|
||
|
|
array(
|
||
|
|
'email_pattern' => 'partial',
|
||
|
|
'assignment_type' => 'custom',
|
||
|
|
'custom_discount' => 5,
|
||
|
|
)
|
||
|
|
);
|
||
|
|
wc_edm_ci_assign(
|
||
|
|
array(
|
||
|
|
'email_pattern' => 'buyer@coupons.test',
|
||
|
|
'assignment_type' => 'coupon',
|
||
|
|
'coupon_code' => 'ciflat10',
|
||
|
|
)
|
||
|
|
);
|
||
|
|
wc_edm_ci_assign(
|
||
|
|
array(
|
||
|
|
'email_pattern' => 'switched-off@company.dk',
|
||
|
|
'assignment_type' => 'custom',
|
||
|
|
'custom_discount' => 90,
|
||
|
|
'is_active' => 0,
|
||
|
|
)
|
||
|
|
);
|
||
|
|
|
||
|
|
update_option(
|
||
|
|
'wc_edm_ci_fixtures',
|
||
|
|
array(
|
||
|
|
'group_id' => $group_id,
|
||
|
|
'category' => $discounted,
|
||
|
|
'product_in' => $product_in,
|
||
|
|
'product_out' => $product_out,
|
||
|
|
)
|
||
|
|
);
|
||
|
|
|
||
|
|
printf(
|
||
|
|
" fixtures: group #%d (20%%) on category #%d, products #%d (100) and #%d (50), 5 assignments, coupon ciflat10\n",
|
||
|
|
$group_id,
|
||
|
|
$discounted,
|
||
|
|
$product_in,
|
||
|
|
$product_out
|
||
|
|
);
|