-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoocommerce-checkout-integration-example.php
196 lines (160 loc) · 4.09 KB
/
woocommerce-checkout-integration-example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* Plugin Name: WooCommerce Checkout Integration Example 1
* Plugin URI: https://woocommerce.com/
* Description: An example to help you integrate your WooCommerce plugin with the new, block-based checkout.
* Version: 1.0.0
* Author: WooCommerce
*
* Text Domain: woocommerce-checkout-integration-example
* Domain Path: /languages/
*
* Requires PHP: 7.0
*
* Requires at least: 5.9
* Tested up to: 5.9
*
* WC requires at least: 6.3
* WC tested up to: 6.4
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Main plugin class.
*
* @class WC_CIE
* @version 1.0.0
*/
class WC_CIE {
const VERSION = '1.0.0';
/**
* Plugin version getter.
*
* @return string
*/
public static function get_plugin_version() {
return self::VERSION;
}
/**
* Plugin URL getter.
*
* @return string
*/
public static function get_plugin_url() {
return untrailingslashit( plugins_url( '/', __FILE__ ) );
}
/**
* Initializes everything.
*
* @return void
*/
public static function initialize() {
// Define constants.
define( 'WC_CIE_ABSPATH', trailingslashit( plugin_dir_path( __FILE__ ) ) );
// Conditionally hide shipping options. Works with the shortcode-based and block-based checkout.
add_filter( 'woocommerce_package_rates', array( __CLASS__, 'filter_shipping_methods' ), 10, 2 );
// Load our integration.
add_action( 'init', array( __CLASS__, 'load_integration' ) );
}
/**
* Extends the Store API and loads the script that integrates with the Checkout block.
*
* @return void
*/
public static function load_integration() {
if ( ! did_action( 'woocommerce_blocks_loaded' ) ) {
return;
}
require_once WC_CIE_ABSPATH . 'includes/api/class-wc-cie-store-api-integration.php';
require_once WC_CIE_ABSPATH . 'includes/blocks/class-wc-cie-blocks-integration.php';
WC_CIE_Store_API_Integration::initialize();
add_action(
'woocommerce_blocks_checkout_block_registration',
function( $registry ) {
$registry->register( WC_CIE_Blocks_Integration::instance() );
}
);
}
/**
* Indicates whether a payment gateway is visible.
*
* @param array $gateway_id
* @return boolean
*/
public static function is_payment_gateway_visible( $gateway_id ) {
$is_visible = true;
if ( 'bacs' === $gateway_id ) {
if ( ! isset( WC()->cart ) ) {
return $is_visible;
}
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
if ( empty( $chosen_methods ) ) {
return $is_visible;
}
foreach ( $chosen_methods as $chosen_method ) {
if ( 0 === strpos( $chosen_method, 'free_shipping' ) ) {
$is_visible = false;
break;
}
}
}
return $is_visible;
}
/**
* Indicates whether a shipping rate is visible.
*
* @param array $method_id
* @return boolean
*/
public static function is_shipping_rate_visible( $method_id ) {
$is_visible = true;
if ( 'free_shipping' === $method_id ) {
if ( ! isset( WC()->cart ) ) {
return $is_visible;
}
if ( WC()->cart->get_cart_contents_total() < 100 ) {
$is_visible = false;
}
}
return $is_visible;
}
/**
* Retrieves all the payment gateways enabled in WooCommerce.
*
* @param boolean $visible_only Returns only the visible payment gateways.
*
* @return array
*/
public static function get_payment_gateways( $visible_only = false ) {
$gateways = WC()->payment_gateways->get_available_payment_gateways();
if ( $visible_only ) {
foreach ( $gateways as $gateway_id => $gateway ) {
if ( ! self::is_payment_gateway_visible( $gateway_id ) ) {
unset( $gateways[ $gateway_id ] );
}
}
}
return $gateways;
}
/**
* Hides shipping methods conditionally.
*
* @param array $rates
* @param array $package
* @return array
*/
public static function filter_shipping_methods( $rates, $package ) {
foreach ( $rates as $rate_id => $rate ) {
if ( ! self::is_shipping_rate_visible( $rate->get_method_id() ) ) {
unset( $rates[ $rate_id ] );
}
}
return $rates;
}
}
WC_CIE::initialize();