Handle date paid in getter and during save to avoid filters during construct

This commit is contained in:
Mike Jolley 2017-03-09 14:40:19 +00:00
parent 9427eb048d
commit 28a8d05285
3 changed files with 21 additions and 19 deletions

View File

@ -253,11 +253,10 @@ class WC_Order extends WC_Abstract_Order {
* `payment_complete` method.
*
* @since 2.7.0
* @param $date_paid What to set date paid to. Defaults to current time.
*/
public function maybe_set_date_paid( $date_paid = '' ) {
if ( ! $this->get_date_paid( 'edit' ) && $this->has_status( array( 'processing', 'completed' ) ) ) {
$this->set_date_paid( $date_paid ? $date_paid : current_time( 'timestamp' ) );
public function maybe_set_date_paid() {
if ( ! $this->get_date_paid( 'edit' ) && $this->has_status( apply_filters( 'woocommerce_payment_complete_order_status', $this->needs_processing() ? 'processing' : 'completed', $this->get_id() ) ) ) {
$this->set_date_paid( current_time( 'timestamp' ) );
}
}
@ -729,7 +728,13 @@ class WC_Order extends WC_Abstract_Order {
* @return int
*/
public function get_date_paid( $context = 'view' ) {
return $this->get_prop( 'date_paid', $context );
$date_paid = $this->get_prop( 'date_paid', $context );
if ( 'view' === $context && ! $date_paid && version_compare( $this->get_version( 'edit' ), '2.7', '<' ) && $this->has_status( apply_filters( 'woocommerce_payment_complete_order_status', $this->needs_processing() ? 'processing' : 'completed', $this->get_id() ) ) ) {
// In view context, return a date if missing.
$date_paid = $this->get_date_created( 'edit' )
}
return $date_paid;
}
/**

View File

@ -100,21 +100,10 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
/**
* In older versions, discounts may have been stored differently.
* Update them now so if the object is saved, the correct values are
* stored.
* @todo When/if meta is flattened, handle this in the migration script.
* stored. @todo When meta is flattened, handle this during migration.
*/
if ( ! $order->get_version( 'edit' ) || version_compare( $order->get_version( 'edit' ), '2.3.7', '<' ) ) {
if ( $order->get_prices_include_tax( 'edit' ) ) {
$order->set_discount_total( (double) get_post_meta( $order->get_id(), '_cart_discount', true ) - (double) get_post_meta( $order->get_id(), '_cart_discount_tax', true ) );
}
}
/**
* In older versions, paid date may not have been set.
* @todo When/if meta is flattened, handle this in the migration script.
*/
if ( ! $order->get_version( 'edit' ) || version_compare( $order->get_version( 'edit' ), '2.7', '<' ) ) {
$order->maybe_set_date_paid( $order->get_date_created( 'edit' ) );
if ( version_compare( $order->get_version( 'edit' ), '2.3.7', '<' ) && $order->get_prices_include_tax( 'edit' ) ) {
$order->set_discount_total( (double) get_post_meta( $order->get_id(), '_cart_discount', true ) - (double) get_post_meta( $order->get_id(), '_cart_discount_tax', true ) );
}
}
@ -248,6 +237,7 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
);
$props_to_update = $this->get_props_to_update( $order, $meta_key_to_props );
foreach ( $props_to_update as $meta_key => $prop ) {
$value = $order->{"get_$prop"}( 'edit' );

View File

@ -131,7 +131,14 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
* @param WC_Order $order
*/
public function update( &$order ) {
// Before updating, ensure date paid is set if missing.
if ( ! $order->get_date_paid( 'edit' ) && version_compare( $order->get_version( 'edit' ), '2.7', '<' ) && $order->has_status( apply_filters( 'woocommerce_payment_complete_order_status', $order->needs_processing() ? 'processing' : 'completed', $order->get_id() ) ) ) {
$order->set_date_paid( $order->get_date_created( 'edit' ) );
}
// Update the order.
parent::update( $order );
do_action( 'woocommerce_update_order', $order->get_id() );
}