Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
47.06% covered (danger)
47.06%
8 / 17
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
My_Account_View_Order
47.06% covered (danger)
47.06%
8 / 17
50.00% covered (danger)
50.00%
1 / 2
6.37
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 print_status_instructions
40.00% covered (danger)
40.00%
6 / 15
0.00% covered (danger)
0.00%
0 / 1
4.94
1<?php
2/**
3 * Add the payment instructions to the order page.
4 *
5 * @see woocommerce/templates/myaccount/view-order.php
6 *
7 * @package    brianhenryie/bh-wp-bitcoin-gateway
8 */
9
10namespace BrianHenryIE\WP_Bitcoin_Gateway\WooCommerce;
11
12use BrianHenryIE\WP_Bitcoin_Gateway\API_Interface;
13use Exception;
14use Psr\Log\LoggerAwareTrait;
15use Psr\Log\LoggerInterface;
16use WC_Order;
17
18/**
19 * Get the order details and pass them to the my-account order ui template.
20 */
21class My_Account_View_Order {
22    use LoggerAwareTrait;
23
24    /**
25     * Check is it a Bitcoin order.
26     * Get the order details.
27     */
28    protected API_Interface $api;
29
30    const TEMPLATE_NAME = 'myaccount/view-order-bitcoin-instructions-status.php';
31
32    /**
33     * Constructor
34     *
35     * @param API_Interface   $api The main plugin functions.
36     * @param LoggerInterface $logger A PSR logger.
37     */
38    public function __construct( API_Interface $api, LoggerInterface $logger ) {
39        $this->setLogger( $logger );
40        $this->api = $api;
41    }
42
43    /**
44     * If it is a Bitcoin order being displayed, print the payment details, via template.
45     *
46     * @hooked woocommerce_view_order
47     *
48     * @param int $order_id The id of the order being viewed.
49     */
50    public function print_status_instructions( int $order_id ): void {
51
52        if ( ! $this->api->is_order_has_bitcoin_gateway( $order_id ) ) {
53            return;
54        }
55
56        /**
57         * This is definitely an order object. For it to be a Bitcoin order, it first must be an order.
58         *
59         * @var WC_Order $order
60         */
61        $order = wc_get_order( $order_id );
62
63        try {
64            $template_args = $this->api->get_formatted_order_details( $order, false );
65        } catch ( Exception $exception ) {
66            // Exception occurs when an order has no Bitcoin address, e.g. if there was a problem fetching one as the
67            // order was created.
68            $this->logger->warning(
69                "Failed to get `shop_order:{$order_id}` details for my-account template: {$exception->getMessage()}",
70                array(
71                    'order_id'  => $order_id,
72                    'exception' => $exception,
73                )
74            );
75            return;
76        }
77
78        $template_args['template'] = self::TEMPLATE_NAME;
79
80        wc_get_template( self::TEMPLATE_NAME, $template_args );
81    }
82}