abstracts
4 weeks ago
admin
1 week ago
blocks
10 months ago
cli
7 months ago
customizer
3 months ago
data-stores
3 weeks ago
emails
1 week ago
export
1 year ago
gateways
2 months ago
import
2 months ago
integrations
4 weeks ago
interfaces
3 months ago
legacy
3 months ago
libraries
1 year ago
log-handlers
1 year ago
payment-tokens
5 years ago
product-usage
1 year ago
queue
3 months ago
react-admin
3 months ago
rest-api
4 weeks ago
shipping
2 months ago
shortcodes
2 months ago
theme-support
2 years ago
tracks
3 months ago
traits
5 years ago
walkers
5 years ago
wccom-site
4 weeks ago
widgets
4 weeks ago
class-wc-ajax.php
4 weeks ago
class-wc-auth.php
1 year ago
class-wc-autoloader.php
7 months ago
class-wc-background-emailer.php
4 weeks ago
class-wc-background-updater.php
5 years ago
class-wc-brands-brand-settings-manager.php
1 year ago
class-wc-brands-coupons.php
1 year ago
class-wc-brands.php
4 months ago
class-wc-breadcrumb.php
3 months ago
class-wc-cache-helper.php
4 weeks ago
class-wc-cart-fees.php
2 years ago
class-wc-cart-session.php
2 months ago
class-wc-cart-totals.php
10 months ago
class-wc-cart.php
2 months ago
class-wc-checkout.php
4 weeks ago
class-wc-cli.php
9 months ago
class-wc-comments.php
3 months ago
class-wc-countries.php
4 weeks ago
class-wc-coupon.php
4 weeks ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
1 year ago
class-wc-customer.php
4 weeks ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
3 years ago
class-wc-datetime.php
4 years ago
class-wc-deprecated-action-hooks.php
2 years ago
class-wc-deprecated-filter-hooks.php
2 months ago
class-wc-discounts.php
10 months ago
class-wc-download-handler.php
1 year ago
class-wc-emails.php
1 week ago
class-wc-embed.php
1 year ago
class-wc-form-handler.php
2 months ago
class-wc-frontend-scripts.php
4 weeks ago
class-wc-geo-ip.php
7 months ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
4 weeks ago
class-wc-https.php
2 years ago
class-wc-install.php
1 week ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
2 years ago
class-wc-logger.php
3 months ago
class-wc-meta-data.php
4 years ago
class-wc-order-factory.php
4 weeks ago
class-wc-order-item-coupon.php
4 years ago
class-wc-order-item-fee.php
4 months ago
class-wc-order-item-meta.php
4 years ago
class-wc-order-item-product.php
4 weeks ago
class-wc-order-item-shipping.php
4 months ago
class-wc-order-item-tax.php
4 years ago
class-wc-order-item.php
4 months ago
class-wc-order-query.php
3 months ago
class-wc-order-refund.php
1 year ago
class-wc-order.php
3 weeks ago
class-wc-payment-gateways.php
4 weeks ago
class-wc-payment-tokens.php
3 years ago
class-wc-post-data.php
4 weeks ago
class-wc-post-types.php
4 weeks ago
class-wc-privacy-background-process.php
1 year ago
class-wc-privacy-erasers.php
9 months ago
class-wc-privacy-exporters.php
4 years ago
class-wc-privacy.php
11 months ago
class-wc-product-attribute.php
3 months ago
class-wc-product-download.php
3 months ago
class-wc-product-external.php
1 year ago
class-wc-product-factory.php
2 months ago
class-wc-product-grouped.php
2 months ago
class-wc-product-query.php
3 months ago
class-wc-product-simple.php
10 months ago
class-wc-product-variable.php
2 months ago
class-wc-product-variation.php
1 year ago
class-wc-query.php
4 weeks ago
class-wc-rate-limiter.php
4 years ago
class-wc-regenerate-images-request.php
3 years ago
class-wc-regenerate-images.php
1 year ago
class-wc-register-wp-admin-settings.php
4 years ago
class-wc-rest-authentication.php
1 year ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
2 months ago
class-wc-shipping-rate.php
11 months ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
6 months ago
class-wc-shipping.php
4 weeks ago
class-wc-shortcodes.php
1 year ago
class-wc-structured-data.php
4 weeks ago
class-wc-tax.php
4 weeks ago
class-wc-template-loader.php
6 months ago
class-wc-tracker.php
7 months ago
class-wc-validation.php
2 years ago
class-wc-webhook.php
4 weeks ago
class-woocommerce.php
1 week ago
wc-account-functions.php
6 months ago
wc-attribute-functions.php
4 weeks ago
wc-brands-functions.php
1 year ago
wc-cart-functions.php
4 months ago
wc-conditional-functions.php
10 months ago
wc-core-functions.php
4 weeks ago
wc-coupon-functions.php
4 months ago
wc-deprecated-functions.php
3 months ago
wc-formatting-functions.php
6 months ago
wc-interactivity-api-functions.php
4 weeks ago
wc-notice-functions.php
4 months ago
wc-order-functions.php
3 weeks ago
wc-order-item-functions.php
3 years ago
wc-order-step-logger-functions.php
3 months ago
wc-page-functions.php
3 weeks ago
wc-product-functions.php
4 weeks ago
wc-rest-functions.php
6 months ago
wc-stock-functions.php
6 months ago
wc-template-functions.php
4 weeks ago
wc-template-hooks.php
9 months ago
wc-term-functions.php
4 weeks ago
wc-update-functions.php
1 week ago
wc-user-functions.php
4 weeks ago
wc-webhook-functions.php
4 weeks ago
wc-widget-functions.php
5 years ago
class-wc-order-refund.php
259 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Order refund. Refunds are based on orders (essentially negative orders) and |
| 4 | * contain much of the same data. |
| 5 | * |
| 6 | * @version 3.0.0 |
| 7 | * @package WooCommerce\Classes |
| 8 | */ |
| 9 | |
| 10 | use Automattic\WooCommerce\Enums\OrderStatus; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * Order refund class. |
| 16 | */ |
| 17 | class WC_Order_Refund extends WC_Abstract_Order { |
| 18 | |
| 19 | /** |
| 20 | * Which data store to load. |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | protected $data_store_name = 'order-refund'; |
| 25 | |
| 26 | /** |
| 27 | * This is the name of this object type. |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | protected $object_type = 'order_refund'; |
| 32 | |
| 33 | /** |
| 34 | * Stores product data. |
| 35 | * |
| 36 | * @var array |
| 37 | */ |
| 38 | protected $extra_data = array( |
| 39 | 'amount' => '', |
| 40 | 'reason' => '', |
| 41 | 'refunded_by' => 0, |
| 42 | 'refunded_payment' => false, |
| 43 | ); |
| 44 | |
| 45 | /** |
| 46 | * List of properties that were earlier managed by data store. However, since DataStore is a not a stored entity in itself, they used to store data in metadata of the data object. |
| 47 | * With custom tables, some of these are moved from metadata to their own columns, but existing code will still try to add them to metadata. This array is used to keep track of such properties. |
| 48 | * |
| 49 | * Only reason to add a property here is that you are moving properties from DataStore instance to data object. Otherwise, if you are adding a new property, consider adding it to $data array instead. |
| 50 | * |
| 51 | * @var array |
| 52 | */ |
| 53 | protected $legacy_datastore_props = array( |
| 54 | '_refund_amount', |
| 55 | '_refund_reason', |
| 56 | '_refunded_by', |
| 57 | '_refunded_payment', |
| 58 | ); |
| 59 | |
| 60 | |
| 61 | /** |
| 62 | * Get internal type (post type.) |
| 63 | * |
| 64 | * @return string |
| 65 | */ |
| 66 | public function get_type() { |
| 67 | return 'shop_order_refund'; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get status - always completed for refunds. |
| 72 | * |
| 73 | * @param string $context What the value is for. Valid values are view and edit. |
| 74 | * @return string |
| 75 | */ |
| 76 | public function get_status( $context = 'view' ) { |
| 77 | return OrderStatus::COMPLETED; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get a title for the new post type. |
| 82 | */ |
| 83 | public function get_post_title() { |
| 84 | // @codingStandardsIgnoreStart |
| 85 | return sprintf( __( 'Refund – %s', 'woocommerce' ), (new DateTime('now'))->format( _x( 'M d, Y @ h:i A', 'Order date parsed by DateTime::format', 'woocommerce' ) ) ); |
| 86 | // @codingStandardsIgnoreEnd |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get refunded amount. |
| 91 | * |
| 92 | * @param string $context What the value is for. Valid values are view and edit. |
| 93 | * @return int|float |
| 94 | */ |
| 95 | public function get_amount( $context = 'view' ) { |
| 96 | return $this->get_prop( 'amount', $context ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get refund reason. |
| 101 | * |
| 102 | * @since 2.2 |
| 103 | * @param string $context What the value is for. Valid values are view and edit. |
| 104 | * @return string |
| 105 | */ |
| 106 | public function get_reason( $context = 'view' ) { |
| 107 | return $this->get_prop( 'reason', $context ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get ID of user who did the refund. |
| 112 | * |
| 113 | * @since 3.0 |
| 114 | * @param string $context What the value is for. Valid values are view and edit. |
| 115 | * @return int |
| 116 | */ |
| 117 | public function get_refunded_by( $context = 'view' ) { |
| 118 | return $this->get_prop( 'refunded_by', $context ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Return if the payment was refunded via API. |
| 123 | * |
| 124 | * @since 3.3 |
| 125 | * @param string $context What the value is for. Valid values are view and edit. |
| 126 | * @return bool |
| 127 | */ |
| 128 | public function get_refunded_payment( $context = 'view' ) { |
| 129 | return $this->get_prop( 'refunded_payment', $context ); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Get formatted refunded amount. |
| 134 | * |
| 135 | * @since 2.4 |
| 136 | * @return string |
| 137 | */ |
| 138 | public function get_formatted_refund_amount() { |
| 139 | return apply_filters( 'woocommerce_formatted_refund_amount', wc_price( $this->get_amount(), array( 'currency' => $this->get_currency() ) ), $this ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Set refunded amount. |
| 144 | * |
| 145 | * @param string $value Value to set. |
| 146 | * @throws WC_Data_Exception Exception if the amount is invalid. |
| 147 | */ |
| 148 | public function set_amount( $value ) { |
| 149 | $this->set_prop( 'amount', wc_format_decimal( $value ) ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Set refund reason. |
| 154 | * |
| 155 | * @param string $value Value to set. |
| 156 | * @throws WC_Data_Exception Exception if the amount is invalid. |
| 157 | */ |
| 158 | public function set_reason( $value ) { |
| 159 | $this->set_prop( 'reason', $value ); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Set refunded by. |
| 164 | * |
| 165 | * @param int $value Value to set. |
| 166 | * @throws WC_Data_Exception Exception if the amount is invalid. |
| 167 | */ |
| 168 | public function set_refunded_by( $value ) { |
| 169 | $this->set_prop( 'refunded_by', absint( $value ) ); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Set if the payment was refunded via API. |
| 174 | * |
| 175 | * @since 3.3 |
| 176 | * @param bool $value Value to set. |
| 177 | */ |
| 178 | public function set_refunded_payment( $value ) { |
| 179 | $this->set_prop( 'refunded_payment', (bool) $value ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Magic __get method for backwards compatibility. |
| 184 | * |
| 185 | * @param string $key Value to get. |
| 186 | * @return mixed |
| 187 | */ |
| 188 | public function __get( $key ) { |
| 189 | wc_doing_it_wrong( $key, 'Refund properties should not be accessed directly.', '3.0' ); |
| 190 | /** |
| 191 | * Maps legacy vars to new getters. |
| 192 | */ |
| 193 | if ( 'reason' === $key ) { |
| 194 | return $this->get_reason(); |
| 195 | } elseif ( 'refund_amount' === $key ) { |
| 196 | return $this->get_amount(); |
| 197 | } |
| 198 | return parent::__get( $key ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Gets an refund from the database. |
| 203 | * |
| 204 | * @deprecated 3.0 |
| 205 | * @param int $id (default: 0). |
| 206 | * @return bool |
| 207 | */ |
| 208 | public function get_refund( $id = 0 ) { |
| 209 | wc_deprecated_function( 'get_refund', '3.0', 'read' ); |
| 210 | |
| 211 | if ( ! $id ) { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | $result = wc_get_order( $id ); |
| 216 | |
| 217 | if ( $result ) { |
| 218 | $this->populate( $result ); |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Get refund amount. |
| 227 | * |
| 228 | * @deprecated 3.0 |
| 229 | * @return int|float |
| 230 | */ |
| 231 | public function get_refund_amount() { |
| 232 | wc_deprecated_function( 'get_refund_amount', '3.0', 'get_amount' ); |
| 233 | return $this->get_amount(); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Get refund reason. |
| 238 | * |
| 239 | * @deprecated 3.0 |
| 240 | * @return string |
| 241 | */ |
| 242 | public function get_refund_reason() { |
| 243 | wc_deprecated_function( 'get_refund_reason', '3.0', 'get_reason' ); |
| 244 | return $this->get_reason(); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Indicates if the current order has an associated Cost of Goods Sold value. |
| 249 | * For refunds the cost will be sum of the cost of the refunded items. |
| 250 | * |
| 251 | * @since 9.9.0 |
| 252 | * |
| 253 | * @return bool True if this order has an associated Cost of Goods Sold value. |
| 254 | */ |
| 255 | public function has_cogs() { |
| 256 | return true; |
| 257 | } |
| 258 | } |
| 259 |