| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Shortcode; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
use StoreEngine\Classes\Order; |
| 10 |
use StoreEngine\Utils\Formatting; |
| 11 |
use StoreEngine\Utils\Helper; |
| 12 |
use StoreEngine\Utils\Template; |
| 13 |
use WP_Error; |
| 14 |
|
| 15 |
class OrderDetails { |
| 16 |
|
| 17 |
public function __construct() { |
| 18 |
add_shortcode( 'storeengine_order_details', [ $this, 'render' ] ); |
| 19 |
} |
| 20 |
|
| 21 |
public function render( $atts ) { |
| 22 |
$attributes = shortcode_atts( [ |
| 23 |
'dummy' => false, |
| 24 |
], $atts ); |
| 25 |
$dummy = Formatting::string_to_bool( $attributes['dummy'] ); |
| 26 |
|
| 27 |
if ( ! $dummy ) { |
| 28 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 29 |
$order_hash = isset( $_GET['order_hash'] ) ? sanitize_text_field( wp_unslash( $_GET['order_hash'] ) ) : ''; |
| 30 |
$order = Helper::get_order_by_key( $order_hash ); |
| 31 |
$order = $order instanceof WP_Error ? false : $order; |
| 32 |
|
| 33 |
// Fall back to sample content when there's no real order to show |
| 34 |
// (page opened directly, previewed, or rendered in the editor). |
| 35 |
if ( ! $order ) { |
| 36 |
$dummy = true; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
if ( $dummy ) { |
| 41 |
$order = new Order(); |
| 42 |
$order->set_payment_method( 'check' ); |
| 43 |
$order->set_payment_method_title( 'Check payments' ); |
| 44 |
$order->set_total( 35 ); |
| 45 |
add_filter( 'storeengine/get_order_item_totals', [ __CLASS__, 'set_dummy_totals' ] ); |
| 46 |
|
| 47 |
$new_order_item = new Order\OrderItemProduct(); |
| 48 |
$new_order_item->set_subtotal( 20.00 ); |
| 49 |
$new_order_item->set_quantity( 1 ); |
| 50 |
$new_order_item->set_name( 'Sample product' ); |
| 51 |
$order_items = [ $new_order_item ]; |
| 52 |
$show_purchase_note = true; |
| 53 |
} else { |
| 54 |
$order_items = $order->get_items( apply_filters( 'storeengine/purchase_order_item_types', 'line_item' ) ); |
| 55 |
$show_purchase_note = $order->has_status( apply_filters( 'storeengine/purchase_note_order_statuses', [ |
| 56 |
'completed', |
| 57 |
'processing', |
| 58 |
] ) ); |
| 59 |
} |
| 60 |
|
| 61 |
ob_start(); |
| 62 |
Template::get_template( 'shortcode/order-details.php', [ |
| 63 |
'order' => $order, |
| 64 |
'order_items' => $order_items, |
| 65 |
'show_purchase_note' => $show_purchase_note, |
| 66 |
] ); |
| 67 |
$output = ob_get_clean(); |
| 68 |
|
| 69 |
if ( $dummy ) { |
| 70 |
remove_filter( 'storeengine/get_order_item_totals', [ __CLASS__, 'set_dummy_totals' ] ); |
| 71 |
} |
| 72 |
|
| 73 |
return $output; |
| 74 |
} |
| 75 |
|
| 76 |
public static function set_dummy_totals(): array { |
| 77 |
$total_rows = []; |
| 78 |
$total_rows['cart_subtotal'] = [ |
| 79 |
'type' => 'cart_subtotal', |
| 80 |
'label' => __( 'Subtotal:', 'storeengine' ), |
| 81 |
'value' => Formatting::price( 20.00 ), |
| 82 |
]; |
| 83 |
|
| 84 |
$total_rows['shipping'] = [ |
| 85 |
'type' => 'shipping', |
| 86 |
'label' => __( 'Shipping:', 'storeengine' ), |
| 87 |
'value' => Formatting::price( 15.00 ) . ' <small class="shipped_via">via Flat rate</small>', |
| 88 |
]; |
| 89 |
|
| 90 |
$total_rows['cart_total'] = [ |
| 91 |
'type' => 'cart_total', |
| 92 |
'label' => __( 'Total:', 'storeengine' ), |
| 93 |
'value' => Formatting::price( 35.00 ), |
| 94 |
]; |
| 95 |
|
| 96 |
$total_rows['payment_method'] = [ |
| 97 |
'type' => 'payment_method', |
| 98 |
'label' => __( 'Payment method:', 'storeengine' ), |
| 99 |
'value' => __( 'Check payments', 'storeengine' ), |
| 100 |
]; |
| 101 |
|
| 102 |
return $total_rows; |
| 103 |
} |
| 104 |
} |
| 105 |
|