enable_rest_api(); $this->enable_wp_cli(); add_action('wu_gateway_payment_processed', array($this, 'maybe_add_use_on_payment_received')); } /** * Listens for payments received in order to increase the discount code uses. * * @since 2.0.4 * * @param \WP_Ultimo\Models\Payment $payment The payment received. * @return void */ public function maybe_add_use_on_payment_received($payment) { if ( ! $payment) { return; } /* * Try to fetch the original cart of the payment. * We want only to increase the number of uses * for the first time payments are done. */ $original_cart = $payment->get_meta('wu_original_cart'); if (is_a($original_cart, \WP_Ultimo\Checkout\Cart::class) === false) { return; } $discount_code = $original_cart->get_discount_code(); if ( ! $discount_code) { return; } /* * Refetch the object, as the original version * might be too old and out-of-date by now. */ $discount_code = wu_get_discount_code($discount_code->get_id()); if ($discount_code) { $discount_code->add_use(); $discount_code->save(); } } }