abstract-wc-order-data-store-cpt.php
2 months ago
abstract-wc-order-item-type-data-store.php
7 months ago
class-wc-coupon-data-store-cpt.php
2 months ago
class-wc-customer-data-store-session.php
9 months ago
class-wc-customer-data-store.php
3 months ago
class-wc-customer-download-data-store.php
3 years ago
class-wc-customer-download-log-data-store.php
1 year ago
class-wc-data-store-wp.php
9 months ago
class-wc-order-data-store-cpt.php
2 months ago
class-wc-order-item-coupon-data-store.php
8 years ago
class-wc-order-item-data-store.php
2 months ago
class-wc-order-item-fee-data-store.php
8 years ago
class-wc-order-item-product-data-store.php
4 years ago
class-wc-order-item-shipping-data-store.php
1 year ago
class-wc-order-item-tax-data-store.php
7 years ago
class-wc-order-refund-data-store-cpt.php
2 months ago
class-wc-payment-token-data-store.php
4 months ago
class-wc-product-data-store-cpt.php
2 months ago
class-wc-product-grouped-data-store-cpt.php
3 months ago
class-wc-product-variable-data-store-cpt.php
1 month ago
class-wc-product-variation-data-store-cpt.php
9 months ago
class-wc-shipping-zone-data-store.php
6 months ago
class-wc-webhook-data-store.php
2 years ago
class-wc-order-refund-data-store-cpt.php
156 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WC_Order_Refund_Data_Store_CPT file. |
| 4 | * |
| 5 | * @package WooCommerce\DataStores |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * WC Order Refund Data Store: Stored in CPT. |
| 14 | * |
| 15 | * @version 3.0.0 |
| 16 | */ |
| 17 | class WC_Order_Refund_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implements WC_Object_Data_Store_Interface, WC_Order_Refund_Data_Store_Interface { |
| 18 | |
| 19 | /** |
| 20 | * Data stored in meta keys, but not considered "meta" for an order. |
| 21 | * |
| 22 | * @since 3.0.0 |
| 23 | * @var array |
| 24 | */ |
| 25 | protected $internal_meta_keys = array( |
| 26 | '_order_currency', |
| 27 | '_cart_discount', |
| 28 | '_refund_amount', |
| 29 | '_refunded_by', |
| 30 | '_refunded_payment', |
| 31 | '_refund_reason', |
| 32 | '_cart_discount_tax', |
| 33 | '_order_shipping', |
| 34 | '_order_shipping_tax', |
| 35 | '_order_tax', |
| 36 | '_order_total', |
| 37 | '_order_version', |
| 38 | '_prices_include_tax', |
| 39 | '_payment_tokens', |
| 40 | ); |
| 41 | |
| 42 | /** |
| 43 | * Delete a refund - no trash is supported. |
| 44 | * |
| 45 | * @param WC_Order $order Order object. |
| 46 | * @param array $args Array of args to pass to the delete method. |
| 47 | */ |
| 48 | public function delete( &$order, $args = array() ) { |
| 49 | $id = $order->get_id(); |
| 50 | |
| 51 | if ( ! $id ) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | $parent_order_id = $order->get_parent_id(); |
| 56 | $refund_cache_key = WC_Cache_Helper::get_cache_prefix( 'orders' ) . 'refund_ids' . $parent_order_id; |
| 57 | wp_delete_post( $id ); |
| 58 | wp_cache_delete( $refund_cache_key, 'orders' ); |
| 59 | $order->set_id( 0 ); |
| 60 | |
| 61 | /** |
| 62 | * Fires when a refund is deleted. |
| 63 | * |
| 64 | * @param int $id The refund ID. |
| 65 | * @since 3.0.0 |
| 66 | */ |
| 67 | do_action( 'woocommerce_delete_order_refund', $id ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Read refund data. Can be overridden by child classes to load other props. |
| 72 | * |
| 73 | * @param WC_Order_Refund $refund Refund object. |
| 74 | * @param object $post_object Post object. |
| 75 | * @since 3.0.0 |
| 76 | */ |
| 77 | protected function read_order_data( &$refund, $post_object ) { |
| 78 | parent::read_order_data( $refund, $post_object ); |
| 79 | $id = $refund->get_id(); |
| 80 | |
| 81 | $post_meta = get_post_meta( $id ); |
| 82 | |
| 83 | $refunded_by = $post_meta['_refunded_by'][0] ?? null; |
| 84 | $reason = $post_meta['_refund_reason'][0] ?? ''; |
| 85 | |
| 86 | $refund->set_props( |
| 87 | array( |
| 88 | 'amount' => $post_meta['_refund_amount'][0] ?? 0, |
| 89 | 'refunded_by' => metadata_exists( 'post', $id, '_refunded_by' ) ? $refunded_by : absint( $post_object->post_author ), |
| 90 | 'refunded_payment' => wc_string_to_bool( $post_meta['_refunded_payment'][0] ?? false ), |
| 91 | 'reason' => metadata_exists( 'post', $id, '_refund_reason' ) ? $reason : $post_object->post_excerpt, |
| 92 | ) |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Method to update a refund in the database. |
| 98 | * |
| 99 | * @param \WC_Order_Refund $refund Refund object. |
| 100 | */ |
| 101 | public function update( &$refund ) { |
| 102 | parent::update( $refund ); |
| 103 | |
| 104 | /** |
| 105 | * Fires when an order refund is updated. |
| 106 | * |
| 107 | * @since 10.4.0 |
| 108 | * |
| 109 | * @param int $refund_id The order refund ID. |
| 110 | * @param \WC_Order_Refund $refund The order refund object. |
| 111 | */ |
| 112 | do_action( 'woocommerce_update_order_refund', $refund->get_id(), $refund ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Helper method that updates all the post meta for an order based on it's settings in the WC_Order class. |
| 117 | * |
| 118 | * @param WC_Order_Refund $refund Refund object. |
| 119 | * |
| 120 | * @since 3.0.0 |
| 121 | */ |
| 122 | protected function update_post_meta( &$refund ) { |
| 123 | parent::update_post_meta( $refund ); |
| 124 | |
| 125 | $updated_props = array(); |
| 126 | $meta_key_to_props = array( |
| 127 | '_refund_amount' => 'amount', |
| 128 | '_refunded_by' => 'refunded_by', |
| 129 | '_refunded_payment' => 'refunded_payment', |
| 130 | '_refund_reason' => 'reason', |
| 131 | ); |
| 132 | |
| 133 | $props_to_update = $this->get_props_to_update( $refund, $meta_key_to_props ); |
| 134 | foreach ( $props_to_update as $meta_key => $prop ) { |
| 135 | $value = $refund->{"get_$prop"}( 'edit' ); |
| 136 | update_post_meta( $refund->get_id(), $meta_key, $value ); |
| 137 | $updated_props[] = $prop; |
| 138 | } |
| 139 | |
| 140 | do_action( 'woocommerce_order_refund_object_updated_props', $refund, $updated_props ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get a title for the new post type. |
| 145 | * |
| 146 | * @return string |
| 147 | */ |
| 148 | protected function get_post_title() { |
| 149 | return sprintf( |
| 150 | /* translators: %s: Order date */ |
| 151 | __( 'Refund – %s', 'woocommerce' ), |
| 152 | ( 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 |
| 153 | ); |
| 154 | } |
| 155 | } |
| 156 |