-
Notifications
You must be signed in to change notification settings - Fork 0
/
woocommerce-force-sells.0001.fix.fatal-error-cart-create-programmatic.patch
84 lines (77 loc) · 3.1 KB
/
woocommerce-force-sells.0001.fix.fatal-error-cart-create-programmatic.patch
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
From 422fb2809f24d7db96f6598e7efcb9dab178139b Mon Sep 17 00:00:00 2001
From: Creative Andrew <[email protected]>
Date: Wed, 9 Feb 2022 16:36:30 +0100
Subject: [PATCH] Fixed fatal error in woocommerce-force-sells if cart instance
is created programmatically. (#795)
---
woocommerce-force-sells.php | 32 +++++++++++++++++++++++---------
1 file changed, 23 insertions(+), 9 deletions(-)
diff --git a/woocommerce-force-sells.php b/woocommerce-force-sells.php
index 7c8856667..89442a86f 100644
--- a/woocommerce-force-sells.php
+++ b/woocommerce-force-sells.php
@@ -68,8 +68,8 @@ if ( ! class_exists( 'WC_Force_Sells' ) ) :
// Keep force sell data in the cart.
add_filter( 'woocommerce_get_cart_item_from_session', array( $this, 'get_cart_item_from_session' ), 10, 2 );
add_filter( 'woocommerce_get_item_data', array( $this, 'get_linked_to_product_data' ), 10, 2 );
- add_action( 'woocommerce_cart_loaded_from_session', array( $this, 'remove_orphan_force_sells' ) );
- add_action( 'woocommerce_cart_loaded_from_session', array( $this, 'maybe_remove_duplicate_force_sells' ) );
+ add_action( 'woocommerce_cart_loaded_from_session', array( $this, 'remove_orphan_force_sells' ));
+ add_action( 'woocommerce_cart_loaded_from_session', array( $this, 'maybe_remove_duplicate_force_sells' ));
// Don't allow synced force sells to be removed or change quantity.
add_filter( 'woocommerce_cart_item_remove_link', array( $this, 'cart_item_remove_link' ), 10, 2 );
@@ -102,14 +102,21 @@ if ( ! class_exists( 'WC_Force_Sells' ) ) :
/**
* Looks to see if a product with the key of 'forced_by' actually exists and
* deletes it if not.
+ *
+ * @param WP_Cart $cart WC_Cart object.
*/
- public function remove_orphan_force_sells() {
- $cart_contents = WC()->cart->get_cart();
+ public function remove_orphan_force_sells( $cart ) {
+
+ if ( !$cart ) {
+ return;
+ }
+
+ $cart_contents = $cart->get_cart();
foreach ( $cart_contents as $key => $value ) {
if ( isset( $value['forced_by'] ) ) {
if ( ! array_key_exists( $value['forced_by'], $cart_contents ) ) {
- WC()->cart->remove_cart_item( $key );
+ $cart->remove_cart_item( $key );
}
}
}
@@ -120,9 +127,16 @@ if ( ! class_exists( 'WC_Force_Sells' ) ) :
* have duplicated force sell products.
*
* @since 1.1.19
+ *
+ * @param WP_Cart $cart WC_Cart object.
*/
- public function maybe_remove_duplicate_force_sells() {
- $cart_contents = WC()->cart->get_cart();
+ public function maybe_remove_duplicate_force_sells( $cart ) {
+
+ if ( !$cart ) {
+ return;
+ }
+
+ $cart_contents = $cart->get_cart();
$product_ids = array();
foreach ( $cart_contents as $key => $value ) {
@@ -131,9 +145,9 @@ if ( ! class_exists( 'WC_Force_Sells' ) ) :
}
}
- foreach ( WC()->cart->get_cart() as $key => $value ) {
+ foreach ( $cart->get_cart() as $key => $value ) {
if ( ! isset( $value['forced_by'] ) && in_array( $value['product_id'], $product_ids, true ) ) {
- WC()->cart->remove_cart_item( $key );
+ $cart->remove_cart_item( $key );
}
}
}
--
2.30.1 (Apple Git-130)