forked from oerdnj/woocommerce-zasilkovna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzasilkovna.php
175 lines (160 loc) · 8.32 KB
/
zasilkovna.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
<?php
/*
* @wordpress-plugin
* Plugin Name: Zásilkovna Shipping Method
* Plugin URI: https://github.com/oerdnj/woocommerce-zasilkovna
* Description: Zásilkovna Shipping Method
* Version: 0.1.1
* Author: Ondřej Surý
* Author URI: https://github.com/oerdnj/
* Text Domain: woocommerce-zasilkovna
* License: GPL-3+
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
* Domain Path: /languages
* GitHub Plugin URI: https://github.com/oerdnj/woocommerce-zasilkovna
* WC tested up to: 4.7.0
*/
/*
* Helper functions
*/
function wc_zasilkovna_get_pickup_point( $pickup_point_id ) {
if ( $pickup_point_id > 0 ) {
$package = WC()->shipping->get_packages()[0];
$shipping_methods = WC()->shipping->load_shipping_methods( $package );
$chosen_method_id = wc_get_chosen_shipping_method_instance_ids()[0];
$shipping_method = $shipping_methods[ $chosen_method_id ];
$pickup_point = $shipping_method->pickup_point( $pickup_point_id );
return $pickup_point;
}
return null;
}
/**
* Gets chosen shipping method instance IDs from chosen_shipping_methods session
*/
function wc_get_chosen_shipping_method_instance_ids() {
$method_instance_ids = array();
$chosen_methods = WC()->session->get( 'chosen_shipping_methods', array() );
foreach ( $chosen_methods as $chosen_method ) {
$chosen_method = explode( ':', $chosen_method );
$method_instance_ids[] = next( $chosen_method );
}
return $method_instance_ids;
}
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
add_filter( 'woocommerce_shipping_methods', 'add_zasilkovna_post_method' );
function add_zasilkovna_post_method( $methods ) {
$methods['zasilkovna'] = 'WC_Zasilkovna_Shipping_Method';
return $methods;
}
add_action( 'woocommerce_shipping_init', 'wc_zasilkovna_shipping_method_init' );
function wc_zasilkovna_shipping_method_init() {
require 'class-zasilkovna.php';
}
add_action( 'woocommerce_review_order_after_shipping', 'wc_zasilkovna_review_order_after_shipping' );
function wc_zasilkovna_review_order_after_shipping() {
$package = WC()->shipping->get_packages()[0];
$shipping_methods = WC()->shipping->load_shipping_methods( $package );
if (!function_exists('wc_get_chosen_shipping_method_ids')) {
require_once ABSPATH . PLUGINDIR . '/woocommerce/includes/wc-cart-functions.php';
}
if ( sizeof( array_intersect( wc_get_chosen_shipping_method_ids(), array( 'zasilkovna') ) ) > 0 ) {
$chosen_method_id = wc_get_chosen_shipping_method_instance_ids()[0];
$shipping_country = WC()->customer->get_shipping_country();
$zasilkovna_pickup_points = $shipping_methods[ $chosen_method_id ]->pickup_points( $shipping_country );
$zasilkovna_chosen_pickup_point = WC()->session->get( 'zasilkovna_chosen_pickup_point' );
$zasilkovna_logo = $shipping_methods[ $chosen_method_id ]->instance_settings['logo'];
?>
<tr class="wc-zasilkovna">
<td><?php
if ($zasilkovna_logo) { ?>
<img src="<?php echo $zasilkovna_logo; ?>" width="160" border="0"><?php
} ?>
</td>
<td>
<font size="2"><?php _e('Zásilkovna - choose a pickup point', 'woocommerce-zasilkovna-shipping-method'); ?></font>
<div id="woocommerce-zasilkovna-branch-select-options">
<select name="zasilkovna_pickup_point">
<option><?php _e("Choose a pickup point", 'woocommerce-zasilkovna-shipping-method'); ?></option>
<?php
foreach ( $zasilkovna_pickup_points as $pickup_point_id => $pickup_point ) {
$selected = ( !empty( $zasilkovna_chosen_pickup_point ) && $zasilkovna_chosen_pickup_point === $pickup_point_id ) ? ' selected="selected"' : '';
print( '<option value="' . $pickup_point_id . '"${selected}>' . $pickup_point->name . '</option>' );
}
?>
</select>
</div>
</td>
</tr>
<?php
}
return true;
}
add_action( 'woocommerce_add_shipping_order_item', 'wc_zasilkovna_save_pickup_point', 10, 2 );
function wc_zasilkovna_save_pickup_point( $order_id, $item_id ) {
if ( isset( $_POST["zasilkovna_pickup_point"] ) ) {
if (!function_exists('wc_get_chosen_shipping_method_ids')) {
require_once ABSPATH . PLUGINDIR . '/woocommerce/includes/wc-cart-functions.php';
}
if ( sizeof( array_intersect( wc_get_chosen_shipping_method_ids(), array( 'zasilkovna') ) ) > 0 ) {
$pickup_point_id = wc_clean( $_POST['zasilkovna_pickup_point'] );
$pickup_point = wc_zasilkovna_get_pickup_point( $pickup_point_id );
$pickup_point_name = wc_clean( $pickup_point->name ) ;
wc_add_order_item_meta( $item_id, 'zasilkovna-pickup-point-id', $pickup_point_id , true );
wc_add_order_item_meta( $item_id, 'zasilkovna-pickup-point-name', $pickup_point_name, true );
}
}
}
add_action( 'woocommerce_checkout_process', 'wc_zasilkovna_validate_pickup_point');
function wc_zasilkovna_validate_pickup_point() {
$shipping_method = current( explode( ':', $_POST["shipping_method"][0] ) );
$pickup_point_id = intval( $_POST["zasilkovna_pickup_point"] );
if ( $pickup_point_id <= 0 && $shipping_method == "zasilkovna" ) {
wc_add_notice( __( "You have to pick a pickup point, when Zásilkovna is chosen.", 'woocommerce-zasilkovna-shipping-method' ), 'error' );
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'wc_zasilkovna_show_pickup_point' );
add_action( 'woocommerce_email_after_order_table', 'wc_zasilkovna_show_pickup_point' );
add_action( 'woocommerce_order_details_after_order_table', 'wc_zasilkovna_show_pickup_point' );
function wc_zasilkovna_show_pickup_point( $order ) {
if ( $order->has_shipping_method( 'zasilkovna' ) ) {
foreach ( $order->get_shipping_methods() as $shipping_item_id => $shipping_item ) {
$pickup_point_name = $order->get_item_meta( $shipping_item_id, 'zasilkovna-pickup-point-name', true );
print ( "<p><strong>Zásilkovna:</strong> " . $pickup_point_name . "</p>" );
}
}
}
add_filter( 'woocommerce_pay4pay_cod_amount', 'wc_zasilkovna_cod_amount' );
function wc_zasilkovna_cod_amount( $amount ) {
if (!function_exists('wc_get_chosen_shipping_method_ids')) {
require_once ABSPATH . PLUGINDIR . '/woocommerce/includes/wc-cart-functions.php';
}
if ( sizeof( array_intersect( wc_get_chosen_shipping_method_ids(), array( 'zasilkovna') ) ) > 0 ) {
$package = WC()->shipping->get_packages()[0];
$shipping_methods = WC()->shipping->load_shipping_methods( $package );
$chosen_method_id = wc_get_chosen_shipping_method_instance_ids()[0];
$cod_rate = $shipping_methods[ $chosen_method_id ]->get_instance_option( 'cod_rate' );
if ( !empty( $cod_rate ) ) {
$amount = $cod_rate;
}
}
return $amount;
}
add_filter( 'woocommerce_available_payment_gateways', 'wc_zasilkovna_available_payment_gateways' );
function wc_zasilkovna_available_payment_gateways( $gateways ) {
if (!function_exists('wc_get_chosen_shipping_method_ids')) {
require_once ABSPATH . PLUGINDIR . '/woocommerce/includes/wc-cart-functions.php';
}
if ( !is_null( WC()->session ) && sizeof( array_intersect( wc_get_chosen_shipping_method_ids(), array( 'zasilkovna') ) ) > 0 ) {
$package = WC()->shipping->get_packages()[0];
$shipping_methods = WC()->shipping->load_shipping_methods( $package );
$chosen_method_id = wc_get_chosen_shipping_method_instance_ids()[0];
if (isset($shipping_methods[$chosen_method_id])) {
$cod_rate = $shipping_methods[ $chosen_method_id ]->get_instance_option( 'cod_rate' );
if ($cod_rate <= 0) {
unset($gateways['cod']);
}
}
}
return $gateways;
}
}