CustomOrdersTableController.php
3 years ago
DataSynchronizer.php
3 years ago
OrdersTableDataStore.php
3 years ago
OrdersTableDataStoreMeta.php
3 years ago
OrdersTableFieldQuery.php
3 years ago
OrdersTableMetaQuery.php
3 years ago
OrdersTableQuery.php
3 years ago
OrdersTableRefundDataStore.php
3 years ago
OrdersTableSearchQuery.php
3 years ago
OrdersTableRefundDataStore.php
195 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Order refund data store. Refunds are based on orders (essentially negative orders) but there is slight difference in how we save them. |
| 4 | * For example, order save hooks etc can't be fired when saving refund, so we need to do it a separate datastore. |
| 5 | */ |
| 6 | |
| 7 | namespace Automattic\WooCommerce\Internal\DataStores\Orders; |
| 8 | |
| 9 | /** |
| 10 | * Class OrdersTableRefundDataStore. |
| 11 | */ |
| 12 | class OrdersTableRefundDataStore extends OrdersTableDataStore { |
| 13 | |
| 14 | /** |
| 15 | * We do not have and use all the getters and setters from OrderTableDataStore, so we only select the props we actually need. |
| 16 | * |
| 17 | * @var \string[][] |
| 18 | */ |
| 19 | protected $operational_data_column_mapping = array( |
| 20 | 'id' => array( 'type' => 'int' ), |
| 21 | 'order_id' => array( 'type' => 'int' ), |
| 22 | 'woocommerce_version' => array( |
| 23 | 'type' => 'string', |
| 24 | 'name' => 'version', |
| 25 | ), |
| 26 | 'prices_include_tax' => array( |
| 27 | 'type' => 'bool', |
| 28 | 'name' => 'prices_include_tax', |
| 29 | ), |
| 30 | 'coupon_usages_are_counted' => array( |
| 31 | 'type' => 'bool', |
| 32 | 'name' => 'recorded_coupon_usage_counts', |
| 33 | ), |
| 34 | 'shipping_tax_amount' => array( |
| 35 | 'type' => 'decimal', |
| 36 | 'name' => 'shipping_tax', |
| 37 | ), |
| 38 | 'shipping_total_amount' => array( |
| 39 | 'type' => 'decimal', |
| 40 | 'name' => 'shipping_total', |
| 41 | ), |
| 42 | 'discount_tax_amount' => array( |
| 43 | 'type' => 'decimal', |
| 44 | 'name' => 'discount_tax', |
| 45 | ), |
| 46 | 'discount_total_amount' => array( |
| 47 | 'type' => 'decimal', |
| 48 | 'name' => 'discount_total', |
| 49 | ), |
| 50 | ); |
| 51 | |
| 52 | /** |
| 53 | * Delete a refund order from database. |
| 54 | * |
| 55 | * @param \WC_Order $refund Refund object to delete. |
| 56 | * @param array $args Array of args to pass to the delete method. |
| 57 | * |
| 58 | * @return void |
| 59 | */ |
| 60 | public function delete( &$refund, $args = array() ) { |
| 61 | $refund_id = $refund->get_id(); |
| 62 | if ( ! $refund_id ) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | $this->delete_order_data_from_custom_order_tables( $refund_id ); |
| 67 | $refund->set_id( 0 ); |
| 68 | |
| 69 | // If this datastore method is called while the posts table is authoritative, refrain from deleting post data. |
| 70 | if ( ! is_a( $refund->get_data_store(), self::class ) ) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | // Delete the associated post, which in turn deletes order items, etc. through {@see WC_Post_Data}. |
| 75 | // Once we stop creating posts for orders, we should do the cleanup here instead. |
| 76 | wp_delete_post( $refund_id ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Read a refund object from custom tables. |
| 81 | * |
| 82 | * @param \WC_Abstract_Order $refund Refund object. |
| 83 | * |
| 84 | * @return void |
| 85 | */ |
| 86 | public function read( &$refund ) { |
| 87 | parent::read( $refund ); |
| 88 | $this->set_refund_props( $refund ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Read multiple refund objects from custom tables. |
| 93 | * |
| 94 | * @param \WC_Order $refunds Refund objects. |
| 95 | */ |
| 96 | public function read_multiple( &$refunds ) { |
| 97 | parent::read_multiple( $refunds ); |
| 98 | foreach ( $refunds as $refund ) { |
| 99 | $this->set_refund_props( $refund ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Helper method to set refund props. |
| 105 | * |
| 106 | * @param \WC_Order $refund Refund object. |
| 107 | */ |
| 108 | private function set_refund_props( $refund ) { |
| 109 | $refund->set_props( |
| 110 | array( |
| 111 | 'amount' => $refund->get_meta( '_refund_amount', true ), |
| 112 | 'refunded_by' => $refund->get_meta( '_refunded_by', true ), |
| 113 | 'refunded_payment' => wc_string_to_bool( $refund->get_meta( '_refunded_payment', true ) ), |
| 114 | 'reason' => $refund->get_meta( '_refund_reason', true ), |
| 115 | ) |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Method to create a refund in the database. |
| 121 | * |
| 122 | * @param \WC_Abstract_Order $refund Refund object. |
| 123 | */ |
| 124 | public function create( &$refund ) { |
| 125 | $refund->set_status( 'completed' ); // Refund are always marked completed. |
| 126 | $this->persist_save( $refund ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Update refund in database. |
| 131 | * |
| 132 | * @param \WC_Order $refund Refund object. |
| 133 | */ |
| 134 | public function update( &$refund ) { |
| 135 | $this->persist_updates( $refund ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Helper method that updates post meta based on an refund object. |
| 140 | * Mostly used for backwards compatibility purposes in this datastore. |
| 141 | * |
| 142 | * @param \WC_Order $refund Refund object. |
| 143 | */ |
| 144 | public function update_order_meta( &$refund ) { |
| 145 | parent::update_order_meta( $refund ); |
| 146 | |
| 147 | // Update additional props. |
| 148 | $updated_props = array(); |
| 149 | $meta_key_to_props = array( |
| 150 | '_refund_amount' => 'amount', |
| 151 | '_refunded_by' => 'refunded_by', |
| 152 | '_refunded_payment' => 'refunded_payment', |
| 153 | '_refund_reason' => 'reason', |
| 154 | ); |
| 155 | |
| 156 | $props_to_update = $this->get_props_to_update( $refund, $meta_key_to_props ); |
| 157 | foreach ( $props_to_update as $meta_key => $prop ) { |
| 158 | $value = $refund->{"get_$prop"}( 'edit' ); |
| 159 | $refund->update_meta_data( $meta_key, $value ); |
| 160 | $updated_props[] = $prop; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Fires after updating meta for a order refund. |
| 165 | * |
| 166 | * @since 2.7.0 |
| 167 | */ |
| 168 | do_action( 'woocommerce_order_refund_object_updated_props', $refund, $updated_props ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Get a title for the new post type. |
| 173 | * |
| 174 | * @return string |
| 175 | */ |
| 176 | protected function get_post_title() { |
| 177 | return sprintf( |
| 178 | /* translators: %s: Order date */ |
| 179 | __( 'Refund – %s', 'woocommerce' ), |
| 180 | ( new \DateTime( 'now' ) )->format( _x( 'M d, Y @ h:i A', 'Order date parsed by DateTime::format', 'woocommerce' ) ) // phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment, WordPress.WP.I18n.UnorderedPlaceholdersText |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | /** |
| 186 | * Returns data store object to use backfilling. |
| 187 | * |
| 188 | * @return \WC_Order_Refund_Data_Store_CPT |
| 189 | */ |
| 190 | protected function get_post_data_store_for_backfill() { |
| 191 | return new \WC_Order_Refund_Data_Store_CPT(); |
| 192 | } |
| 193 | |
| 194 | } |
| 195 |