| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\API; |
| 4 |
|
| 5 |
use StoreEngine\Classes\Countries; |
| 6 |
use StoreEngine\Utils\Helper; |
| 7 |
use StoreEngine\Utils\TaxUtil; |
| 8 |
use StoreEngine\Utils\Template; |
| 9 |
use WP_REST_Controller; |
| 10 |
use WP_REST_Request; |
| 11 |
use WP_REST_Server; |
| 12 |
|
| 13 |
|
| 14 |
// no aliasing required |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
class MiniCart extends WP_REST_Controller { |
| 21 |
|
| 22 |
public static function init() { |
| 23 |
$self = new self(); |
| 24 |
$self->namespace = STOREENGINE_PLUGIN_SLUG . '/v1'; |
| 25 |
$self->rest_base = 'mini-cart'; |
| 26 |
|
| 27 |
add_action( 'rest_api_init', array( $self, 'register_routes' ) ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Register the routes for the objects of the controller. |
| 32 |
*/ |
| 33 |
public function register_routes() { |
| 34 |
register_rest_route( $this->namespace, '/' . $this->rest_base . '/current', [ |
| 35 |
'methods' => WP_REST_Server::READABLE, |
| 36 |
'callback' => [ $this, 'get_mini_cart_info' ], |
| 37 |
'permission_callback' => '__return_true', |
| 38 |
'args' => [ |
| 39 |
'render' => [ |
| 40 |
'type' => 'boolean', |
| 41 |
'default' => true, |
| 42 |
], |
| 43 |
], |
| 44 |
] ); |
| 45 |
} |
| 46 |
|
| 47 |
public function get_mini_cart_info( WP_REST_Request $request ) { |
| 48 |
$cart = Helper::cart(); |
| 49 |
|
| 50 |
$response = apply_filters( 'storeengine/api/mini_cart_response', [ |
| 51 |
'is_empty' => $cart->is_cart_empty(), |
| 52 |
'tax_itemized' => 'itemized' === Helper::get_settings( 'tax_total_display' ), |
| 53 |
'subtotal' => (float) $cart->get_subtotal(), |
| 54 |
'total' => (float) $cart->get_total( 'edit' ), |
| 55 |
'taxes_total' => (float) $cart->get_taxes_total(), |
| 56 |
'tax_or_vat' => Countries::get_instance()->tax_or_vat(), |
| 57 |
'tax_label_suffix' => TaxUtil::get_tax_label_suffix(), |
| 58 |
'tax_totals' => $cart->get_tax_totals(), |
| 59 |
'totals' => $cart->get_totals(), |
| 60 |
'fees' => $cart->get_fees(), |
| 61 |
'cart_items_count' => $cart->get_count(), |
| 62 |
'cart_url' => Helper::get_cart_url(), |
| 63 |
'checkout_url' => Helper::get_checkout_url(), |
| 64 |
'shop_url' => Helper::get_shop_url(), |
| 65 |
'cart_items' => $cart->get_cart_items(), |
| 66 |
'cart_items_html' => $request->get_param( 'render' ) ? Template::get_template_content( 'cart/cart-list-table.php' ) : null, |
| 67 |
] ); |
| 68 |
|
| 69 |
return rest_ensure_response( $response ); |
| 70 |
} |
| 71 |
|
| 72 |
} |
| 73 |
|