class-wc-cart-fees.php
| 1 | <?php |
| 2 | /** |
| 3 | * Cart fees API. |
| 4 | * |
| 5 | * Developers can add fees to the cart via WC()->cart->fees_api() which will reference this class. |
| 6 | * |
| 7 | * We suggest using the action woocommerce_cart_calculate_fees hook for adding fees. |
| 8 | * |
| 9 | * @package WooCommerce\Classes |
| 10 | * @version 3.2.0 |
| 11 | */ |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * WC_Cart_Fees class. |
| 17 | * |
| 18 | * @since 3.2.0 |
| 19 | */ |
| 20 | final class WC_Cart_Fees { |
| 21 | |
| 22 | /** |
| 23 | * An array of fee objects. |
| 24 | * |
| 25 | * @var object[] |
| 26 | */ |
| 27 | private $fees = array(); |
| 28 | |
| 29 | /** |
| 30 | * New fees are made out of these props. |
| 31 | * |
| 32 | * @var array |
| 33 | */ |
| 34 | private $default_fee_props = array( |
| 35 | 'id' => '', |
| 36 | 'name' => '', |
| 37 | 'tax_class' => '', |
| 38 | 'taxable' => false, |
| 39 | 'amount' => 0, |
| 40 | 'total' => 0, |
| 41 | ); |
| 42 | |
| 43 | /** |
| 44 | * Constructor. Reference to the cart. |
| 45 | * |
| 46 | * @param null $deprecated Deprecated since WooCommerce 8.2.0. |
| 47 | * |
| 48 | * @since 3.2.0 |
| 49 | */ |
| 50 | public function __construct( $deprecated = null ) { |
| 51 | if ( isset( $deprecated ) ) { |
| 52 | wc_doing_it_wrong( |
| 53 | 'new WC_Cart_Fees', |
| 54 | 'You don\'t need to pass a cart parameter to the WC_Cart_Fees constructor anymore', |
| 55 | '8.2.0' |
| 56 | ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Register methods for this object on the appropriate WordPress hooks. |
| 62 | */ |
| 63 | public function init() {} |
| 64 | |
| 65 | /** |
| 66 | * Add a fee. Fee IDs must be unique. |
| 67 | * |
| 68 | * @since 3.2.0 |
| 69 | * @param array $args Array of fee properties. |
| 70 | * @return object Either a fee object if added, or a WP_Error if it failed. |
| 71 | */ |
| 72 | public function add_fee( $args = array() ) { |
| 73 | $fee_props = (object) wp_parse_args( $args, $this->default_fee_props ); |
| 74 | $fee_props->name = $fee_props->name ? $fee_props->name : __( 'Fee', 'woocommerce' ); |
| 75 | $fee_props->tax_class = in_array( $fee_props->tax_class, array_merge( WC_Tax::get_tax_classes(), WC_Tax::get_tax_class_slugs() ), true ) ? $fee_props->tax_class : ''; |
| 76 | $fee_props->taxable = wc_string_to_bool( $fee_props->taxable ); |
| 77 | $fee_props->amount = wc_format_decimal( $fee_props->amount ); |
| 78 | |
| 79 | if ( empty( $fee_props->id ) ) { |
| 80 | $fee_props->id = $this->generate_id( $fee_props ); |
| 81 | } |
| 82 | |
| 83 | if ( array_key_exists( $fee_props->id, $this->fees ) ) { |
| 84 | return new WP_Error( 'fee_exists', __( 'Fee has already been added.', 'woocommerce' ) ); |
| 85 | } |
| 86 | |
| 87 | $this->fees[ $fee_props->id ] = $fee_props; |
| 88 | |
| 89 | return $this->fees[ $fee_props->id ]; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get fees. |
| 94 | * |
| 95 | * @return array |
| 96 | */ |
| 97 | public function get_fees() { |
| 98 | uasort( $this->fees, array( $this, 'sort_fees_callback' ) ); |
| 99 | |
| 100 | return $this->fees; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Set fees. |
| 105 | * |
| 106 | * @param object[] $raw_fees Array of fees. |
| 107 | */ |
| 108 | public function set_fees( $raw_fees = array() ) { |
| 109 | $this->fees = array(); |
| 110 | |
| 111 | foreach ( $raw_fees as $raw_fee ) { |
| 112 | $this->add_fee( $raw_fee ); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Remove all fees. |
| 118 | * |
| 119 | * @since 3.2.0 |
| 120 | */ |
| 121 | public function remove_all_fees() { |
| 122 | $this->set_fees(); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Sort fees by amount. |
| 127 | * |
| 128 | * @param stdClass $a Fee object. |
| 129 | * @param stdClass $b Fee object. |
| 130 | * @return int |
| 131 | */ |
| 132 | protected function sort_fees_callback( $a, $b ) { |
| 133 | /** |
| 134 | * Filter sort fees callback. |
| 135 | * |
| 136 | * @since 3.8.0 |
| 137 | * @param int Sort order, -1 or 1. |
| 138 | * @param stdClass $a Fee object. |
| 139 | * @param stdClass $b Fee object. |
| 140 | */ |
| 141 | return apply_filters( 'woocommerce_sort_fees_callback', $a->amount > $b->amount ? -1 : 1, $a, $b ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Generate a unique ID for the fee being added. |
| 146 | * |
| 147 | * @param string $fee Fee object. |
| 148 | * @return string fee key. |
| 149 | */ |
| 150 | private function generate_id( $fee ) { |
| 151 | return sanitize_title( $fee->name ); |
| 152 | } |
| 153 | } |
| 154 |