This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cocart-check-cart-items.php
215 lines (179 loc) · 6.81 KB
/
cocart-check-cart-items.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/*
* Plugin Name: CoCart - Check Cart Items
* Plugin URI: https://cocart.xyz
* Description: Checks items in the cart for validity and stock. Updates the cart before the cart returns what remains and returns a notice for each item changed.
* Author: Sébastien Dumont
* Author URI: https://sebastiendumont.com
* Version: 1.1.0
* Text Domain: cocart-check-cart-items
* Domain Path: /languages/
*
* Requires at least: 5.3
* Requires PHP: 7.0
* WC requires at least: 4.3
* WC tested up to: 4.8
*
* Copyright: © 2020 Sébastien Dumont, ([email protected])
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
if ( ! class_exists( 'CoCart_Check_Cart_Items' ) ) {
class CoCart_Check_Cart_Items {
/**
* Load the plugin.
*
* @access public
*/
public function __construct() {
// Load translation files.
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
// Check cart items.
add_filter( 'cocart_return_cart_contents', array( $this, 'check_cart_items' ), 97 );
// Check cart coupons.
add_filter( 'cocart_return_cart_contents', array( $this, 'check_cart_coupons' ), 98 );
// Plugin activation and deactivation.
register_activation_hook( __FILE__, array( $this, 'activated' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivated' ) );
} // END __construct()
/**
* Make the plugin translation ready.
*
* Translations should be added in the WordPress language directory:
* - WP_LANG_DIR/plugins/cocart-check-cart-items-LOCALE.mo
*
* @access public
* @return void
*/
public function load_plugin_textdomain() {
load_plugin_textdomain( 'cocart-check-cart-items', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Check all cart items for validity and stock.
*
* @access public
* @return $cart_contents
*/
public function check_cart_items( $cart_contents = array() ) {
$result = $this->check_cart_item_validity();
if ( is_wp_error( $result ) ) {
wc_add_notice( $result->get_error_message(), 'error' );
$return = false;
}
$result = $this->check_cart_item_stock();
if ( is_wp_error( $result ) ) {
wc_add_notice( $result->get_error_message(), 'error' );
}
return $cart_contents;
} // END check_cart_items()
/**
* Looks through cart items and checks the products are not trashed or deleted.
*
* @access public
* @return bool|WP_Error
*/
public function check_cart_item_validity() {
$return = true;
$cart = WC()->cart;
foreach ( $cart->get_cart() as $cart_item_key => $values ) {
$product = $values['data'];
if ( ! $product || ! $product->exists() || 'trash' === $product->get_status() ) {
$cart->set_quantity( $cart_item_key, 0 );
$return = new WP_Error( 'invalid', __( 'An item which is no longer available was removed from your cart.', 'cocart-check-cart-items' ) );
}
}
return $return;
} // END check_cart_item_validity()
/**
* Looks through the cart to check each item is in stock. If not, add an error.
*
* @access public
* @return bool|WP_Error
*/
public function check_cart_item_stock() {
$cart = WC()->cart;
$error = new WP_Error();
$product_qty_in_cart = $cart->get_cart_item_quantities();
$current_session_order_id = isset( WC()->session->order_awaiting_payment ) ? absint( WC()->session->order_awaiting_payment ) : 0;
foreach ( $cart->get_cart() as $cart_item_key => $values ) {
$product = $values['data'];
// Check stock based on stock-status.
if ( ! $product->is_in_stock() ) {
/* translators: %s: product name */
$error->add( 'out-of-stock', sprintf( __( 'Sorry, "%s" is not in stock. Please edit your cart and try again. We apologize for any inconvenience caused.', 'cocart-check-cart-items' ), $product->get_name() ) );
return $error;
}
// We only need to check products managing stock, with a limited stock qty.
if ( ! $product->managing_stock() || $product->backorders_allowed() ) {
continue;
}
// Check stock based on all items in the cart and consider any held stock within pending orders.
$held_stock = wc_get_held_stock_quantity( $product, $current_session_order_id );
$required_stock = $product_qty_in_cart[ $product->get_stock_managed_by_id() ];
/**
* Allows filter if product have enough stock to get added to the cart.
*
* @param bool $has_stock If have enough stock.
* @param WC_Product $product Product instance.
* @param array $values Cart item values.
*/
if ( apply_filters( 'cocart_cart_item_required_stock_is_not_enough', $product->get_stock_quantity() < ( $held_stock + $required_stock ), $product, $values ) ) {
/* translators: 1: product name 2: quantity in stock */
$error->add( 'out-of-stock', sprintf( __( 'Sorry, we do not have enough "%1$s" in stock to fulfill your order (%2$s available). We apologize for any inconvenience caused.', 'cocart-check-cart-items' ), $product->get_name(), wc_format_stock_quantity_for_display( $product->get_stock_quantity() - $held_stock, $product ) ) );
return $error;
}
}
return true;
} // END check_cart_item_stock()
/**
* Check cart coupons for errors.
*
* @access public
* @since 1.1.0
*/
public function check_cart_coupons() {
$cart = WC()->cart;
foreach ( $cart->get_applied_coupons() as $code ) {
$coupon = new WC_Coupon( $code );
if ( ! $coupon->is_valid() ) {
$coupon->add_coupon_message( 101 );
$cart->remove_coupon( $code );
}
}
} // END check_cart_coupons()
/**
* Runs when the plugin is activated.
*
* Adds plugin to list of installed CoCart add-ons.
*
* @access public
*/
public function activated() {
$addons_installed = get_site_option( 'cocart_addons_installed', array() );
$plugin = plugin_basename( __FILE__ );
// Check if plugin is already added to list of installed add-ons.
if ( ! in_array( $plugin, $addons_installed, true ) ) {
array_push( $addons_installed, $plugin );
update_site_option( 'cocart_addons_installed', $addons_installed );
}
} // END activated()
/**
* Runs when the plugin is deactivated.
*
* Removes plugin from list of installed CoCart add-ons.
*
* @access public
*/
public function deactivated() {
$addons_installed = get_site_option( 'cocart_addons_installed', array() );
$plugin = plugin_basename( __FILE__ );
// Remove plugin from list of installed add-ons.
if ( in_array( $plugin, $addons_installed, true ) ) {
$addons_installed = array_diff( $addons_installed, array( $plugin ) );
update_site_option( 'cocart_addons_installed', $addons_installed );
}
} // END deactivated()
} // END class
} // END if class exists
new CoCart_Check_Cart_Items();