Update coupon data store to use new dates

This commit is contained in:
Claudiu Lodromanean 2017-03-13 14:24:05 -07:00
parent 258845fb7b
commit f1925e561a
3 changed files with 35 additions and 5 deletions

View File

@ -54,7 +54,7 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
* @param WC_Coupon
*/
public function create( &$coupon ) {
$coupon->set_date_created( current_time( 'timestamp' ) );
$coupon->set_date_created( current_time( 'timestamp', true ) );
$coupon_id = wp_insert_post( apply_filters( 'woocommerce_new_coupon_data', array(
'post_type' => 'shop_coupon',
@ -63,8 +63,8 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
'post_title' => $coupon->get_code(),
'post_content' => '',
'post_excerpt' => $coupon->get_description(),
'post_date' => date( 'Y-m-d H:i:s', $coupon->get_date_created() ),
'post_date_gmt' => get_gmt_from_date( date( 'Y-m-d H:i:s', $coupon->get_date_created() ) ),
'post_date' => date( 'Y-m-d H:i:s', $coupon->get_date_created()->getOffsetTimestamp() ),
'post_date_gmt' => date( 'Y-m-d H:i:s', $coupon->get_date_created()->getTimestamp() ),
) ), true );
if ( $coupon_id ) {
@ -93,8 +93,8 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
$coupon->set_props( array(
'code' => $post_object->post_title,
'description' => $post_object->post_excerpt,
'date_created' => $post_object->post_date,
'date_modified' => $post_object->post_modified,
'date_created' => strtotime( $post_object->post_date_gmt ),
'date_modified' => strtotime( $post_object->post_modified_gmt ),
'date_expires' => get_post_meta( $coupon_id, 'expiry_date', true ),
'discount_type' => get_post_meta( $coupon_id, 'discount_type', true ),
'amount' => get_post_meta( $coupon_id, 'coupon_amount', true ),
@ -210,6 +210,9 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
case 'email_restrictions' :
$updated = update_post_meta( $coupon->get_id(), $meta_key, array_filter( array_map( 'sanitize_email', $value ) ) );
break;
case 'date_expires' :
$updated = update_post_meta( $coupon->get_id(), $meta_key, ( $value ? $value->getTimestamp() : null ) );
break;
default :
$updated = update_post_meta( $coupon->get_id(), $meta_key, $value );
break;

View File

@ -250,6 +250,11 @@ class WC_Tests_Coupon extends WC_Unit_Test_Case {
WC_Helper_Product::delete_product( $product->get_id() );
}
/**
* Test date setters/getters.
*
* @since 3.0.0
*/
public function test_dates() {
$valid_coupon = WC_Helper_Coupon::create_coupon();
$valid_coupon->set_date_expires( time() + 1000 );

View File

@ -99,6 +99,28 @@ class WC_Tests_Coupon_Data_Store extends WC_Unit_Test_Case {
$this->assertNotEquals( 0, $new_coupon_id );
}
/**
* Test coupon date saving/loading.
* @since 3.0.0
*/
function test_coupon_dates() {
$created_date = time() - 30;
$modified_date = time() - 20;
$expiry_date = time() - 10;
$coupon = WC_Helper_Coupon::create_coupon();
$coupon->set_date_created( $created_date);
$coupon->set_date_modified( $modified_date );
$coupon->set_date_expires( $expiry_date );
$coupon->save();
$coupon_read = new WC_Coupon( $coupon->get_id() );
$this->assertEquals( $created_date, $coupon->get_date_created()->getTimestamp() );
$this->assertEquals( $modified_date, $coupon->get_date_modified()->getTimestamp() );
$this->assertEquals( $expiry_date, $coupon->get_date_expires()->getTimestamp() );
}
/**
* Test coupon increase, decrease, user usage count methods.
* @since 2.7.0