PluginProbe
WooCommerce / 11.1.0-beta.2
WooCommerce v11.1.0-beta.2
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / src / Blocks / Payments / Integrations / CashOnDelivery.php
CashOnDelivery.php
103 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Automattic\WooCommerce\Blocks\Payments\Integrations;
3
4 use Automattic\WooCommerce\Blocks\Assets\Api;
5 use WC_Gateway_COD;
6
7 /**
8 * Cash on Delivery (COD) payment method integration
9 *
10 * @since 3.0.0
11 */
12 final class CashOnDelivery extends AbstractPaymentMethodType {
13 /**
14 * Payment method name/id/slug (matches id in WC_Gateway_COD in core).
15 *
16 * @var string
17 */
18 protected $name = WC_Gateway_COD::ID;
19
20 /**
21 * An instance of the Asset Api
22 *
23 * @var Api
24 */
25 private $asset_api;
26
27 /**
28 * Constructor
29 *
30 * @param Api $asset_api An instance of Api.
31 */
32 public function __construct( Api $asset_api ) {
33 $this->asset_api = $asset_api;
34 }
35
36 /**
37 * Initializes the payment method type.
38 */
39 public function initialize() {
40 $this->settings = get_option( 'woocommerce_cod_settings', [] );
41 }
42
43 /**
44 * Returns if this payment method should be active. If false, the scripts will not be enqueued.
45 *
46 * @return boolean
47 */
48 public function is_active() {
49 return filter_var( $this->get_setting( 'enabled', false ), FILTER_VALIDATE_BOOLEAN );
50 }
51
52 /**
53 * Return enable_for_virtual option.
54 *
55 * @return boolean True if store allows COD payment for orders containing only virtual products.
56 */
57 private function get_enable_for_virtual() {
58 return filter_var( $this->get_setting( 'enable_for_virtual', false ), FILTER_VALIDATE_BOOLEAN );
59 }
60
61 /**
62 * Return enable_for_methods option.
63 *
64 * @return array Array of shipping methods (string ids) that allow COD. (If empty, all support COD.)
65 */
66 private function get_enable_for_methods() {
67 $enable_for_methods = $this->get_setting( 'enable_for_methods', [] );
68 if ( '' === $enable_for_methods ) {
69 return [];
70 }
71 return $enable_for_methods;
72 }
73
74
75 /**
76 * Returns an array of scripts/handles to be registered for this payment method.
77 *
78 * @return array
79 */
80 public function get_payment_method_script_handles() {
81 $this->asset_api->register_script(
82 'wc-payment-method-cod',
83 'assets/client/blocks/wc-payment-method-cod.js'
84 );
85 return [ 'wc-payment-method-cod' ];
86 }
87
88 /**
89 * Returns an array of key=>value pairs of data made available to the payment methods script.
90 *
91 * @return array
92 */
93 public function get_payment_method_data() {
94 return [
95 'title' => $this->get_setting( 'title' ),
96 'description' => $this->get_setting( 'description' ),
97 'enableForVirtual' => $this->get_enable_for_virtual(),
98 'enableForShippingMethods' => $this->get_enable_for_methods(),
99 'supports' => $this->get_supported_features(),
100 ];
101 }
102 }
103