This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It isn't 100% complete yet, but introduce a custom data store for ref…
…unds. Refs #45.
- Loading branch information
1 parent
4a524a4
commit b4ccdec
Showing
6 changed files
with
259 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
includes/class-wc-order-refund-data-store-custom-table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?php | ||
/** | ||
* WooCommerce order refund data store. | ||
* | ||
* @package WooCommerce_Custom_Orders_Table | ||
* @author Liquid Web | ||
*/ | ||
|
||
/** | ||
* Extend the WC_Order_Refund_Data_Store_CPT class, overloading methods that require database access in | ||
* order to use the new table. | ||
* | ||
* This operates in a way similar to WC_Order_Data_Store_Custom_Table, but is for *refunds*. | ||
*/ | ||
class WC_Order_Refund_Data_Store_Custom_Table extends WC_Order_Refund_Data_Store_CPT { | ||
|
||
/** | ||
* Set to true when creating so we know to insert meta data. | ||
* | ||
* @var boolean | ||
*/ | ||
protected $creating = false; | ||
|
||
/** | ||
* Create a new refund in the database. | ||
* | ||
* @param WC_Order_Refund $refund The refund object, passed by reference. | ||
*/ | ||
public function create( &$refund ) { | ||
$this->creating = true; | ||
|
||
parent::create( $refund ); | ||
} | ||
|
||
/** | ||
* Read refund data. | ||
* | ||
* @param WC_Order_Refund $refund The refund object, passed by reference. | ||
* @param object $post_object The post object. | ||
*/ | ||
protected function read_order_data( &$refund, $post_object ) { | ||
global $wpdb; | ||
|
||
$data = $this->get_order_data_from_table( $refund ); | ||
|
||
if ( ! empty( $data ) ) { | ||
$refund->set_props( $data ); | ||
|
||
} else { | ||
// Automatically backfill refund data from meta, but allow for disabling. | ||
if ( apply_filters( 'wc_custom_order_table_automatic_migration', true ) ) { | ||
$this->populate_from_meta( $refund ); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Retrieve a single refund from the database. | ||
* | ||
* @global $wpdb | ||
* | ||
* @param WC_Order_Refund $refund The refund object. | ||
* | ||
* @return object The refund row, as an associative array. | ||
*/ | ||
public function get_order_data_from_table( $refund ) { | ||
global $wpdb; | ||
|
||
$data = $wpdb->get_row( $wpdb->prepare( | ||
'SELECT * FROM ' . esc_sql( wc_custom_order_table()->get_table_name() ) . ' WHERE order_id = %d LIMIT 1', | ||
$refund->get_id() | ||
), ARRAY_A ); // WPCS: DB call OK. | ||
|
||
// If no matches were found, this record needs to be created. | ||
if ( null === $data ) { | ||
$this->creating = true; | ||
|
||
return array(); | ||
} | ||
|
||
// Expand anything that might need assistance. | ||
$data['prices_include_tax'] = wc_string_to_bool( $data['prices_include_tax'] ); | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* Helper method that updates all the post meta for a refund based on it's settings in the | ||
* WC_Order_Refund class. | ||
* | ||
* @global $wpdb | ||
* | ||
* @param WC_Order $refund The refund to be updated. | ||
*/ | ||
protected function update_post_meta( &$refund ) { | ||
global $wpdb; | ||
|
||
$table = wc_custom_order_table()->get_table_name(); | ||
$refund_data = array( | ||
'order_id' => $refund->get_id( 'edit' ), | ||
'discount_total' => $refund->get_discount_total( 'edit' ), | ||
'discount_tax' => $refund->get_discount_tax( 'edit' ), | ||
'shipping_total' => $refund->get_shipping_total( 'edit' ), | ||
'shipping_tax' => $refund->get_shipping_tax( 'edit' ), | ||
'cart_tax' => $refund->get_cart_tax( 'edit' ), | ||
'total' => $refund->get_total( 'edit' ), | ||
'version' => $refund->get_version( 'edit' ), | ||
'currency' => $refund->get_currency( 'edit' ), | ||
'prices_include_tax' => wc_bool_to_string( $refund->get_prices_include_tax( 'edit' ) ), | ||
'amount' => $refund->get_amount( 'edit' ), | ||
'reason' => $refund->get_reason( 'edit' ), | ||
'refunded_by' => $refund->get_refunded_by( 'edit' ), | ||
); | ||
|
||
// Insert or update the database record. | ||
if ( $this->creating ) { | ||
$inserted = $wpdb->insert( $table, $refund_data ); // WPCS: DB call OK. | ||
|
||
if ( 1 !== $inserted ) { | ||
return; | ||
} | ||
|
||
$this->creating = false; | ||
|
||
} else { | ||
$refund_data = array_intersect_key( $refund_data, $refund->get_changes() ); | ||
|
||
// There's nothing to update. | ||
if ( empty( $refund_data ) ) { | ||
return; | ||
} | ||
|
||
$wpdb->update( | ||
wc_custom_order_table()->get_table_name(), | ||
$refund_data, | ||
array( 'order_id' => (int) $refund->get_id() ) | ||
); | ||
} | ||
|
||
do_action( 'woocommerce_order_refund_object_updated_props', $refund, $refund_data ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
/** | ||
* Tests for the WC_Order_Refund_Data_Store_Custom_Table class. | ||
* | ||
* @package WooCommerce_Custom_Orders_Table | ||
* @author Liquid Web | ||
*/ | ||
|
||
class OrderRefundDataStoreTest extends TestCase { | ||
|
||
protected $user; | ||
|
||
/** | ||
* Since refunds are issued by people, generate and act as a user. | ||
* | ||
* @before | ||
*/ | ||
public function set_current_user() { | ||
$this->user = $this->factory()->user->create(); | ||
|
||
wp_set_current_user( $this->user ); | ||
} | ||
|
||
public function test_read_order_data_meta() { | ||
$order = WC_Helper_Order::create_order(); | ||
$refund = wc_create_refund( array( | ||
'order_id' => $order->get_id(), | ||
'amount' => 5, | ||
'reason' => 'For testing', | ||
) ); | ||
|
||
// Refresh the refund. | ||
$refund = wc_get_order( $refund->get_id() ); | ||
|
||
$this->assertEquals( 5, $refund->get_amount() ); | ||
$this->assertEquals( $this->user, $refund->get_refunded_by() ); | ||
$this->assertEquals( 'For testing', $refund->get_reason() ); | ||
} | ||
|
||
public function test_update_post_meta() { | ||
$order = WC_Helper_Order::create_order(); | ||
$refund = wc_create_refund( array( | ||
'order_id' => $order->get_id(), | ||
'amount' => 7, | ||
'reason' => 'For testing', | ||
) ); | ||
$row = $this->get_order_row( $refund->get_id() ); | ||
|
||
$this->assertEquals( 7, $row['amount'] ); | ||
$this->assertEquals( $this->user, $row['refunded_by'] ); | ||
$this->assertEquals( 'For testing', $row['reason'] ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters