|
1
|
|
|
<?php |
|
2
|
|
|
namespace lsx_health_plan\classes\integrations\woocommerce; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Contains the downloads functions post type |
|
6
|
|
|
* |
|
7
|
|
|
* @package lsx-health-plan |
|
8
|
|
|
*/ |
|
9
|
|
|
class Checkout { |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Holds class instance |
|
13
|
|
|
* |
|
14
|
|
|
* @since 1.0.0 |
|
15
|
|
|
* |
|
16
|
|
|
* @var object \lsx_health_plan\classes\integrations\woocommerce\Checkout() |
|
17
|
|
|
*/ |
|
18
|
|
|
protected static $instance = null; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
public $plan_id = ''; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Constructor |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct() { |
|
29
|
|
|
add_filter( 'woocommerce_order_button_text', array( $this, 'checkout_button_text' ), 10, 1 ); |
|
30
|
|
|
|
|
31
|
|
|
// Cart Messages. |
|
32
|
|
|
add_action( 'lsx_content_wrap_before', array( $this, 'cart_notices' ) ); |
|
33
|
|
|
add_filter( 'wc_add_to_cart_message_html', array( $this, 'add_to_cart_message' ), 10, 3 ); |
|
34
|
|
|
|
|
35
|
|
|
// Thank you page links. |
|
36
|
|
|
add_filter( 'woocommerce_memberships_thank_you_message', array( $this, 'memberships_thank_you_links' ), 10, 3 ); |
|
37
|
|
|
return (string) apply_filters( 'woocommerce_memberships_thank_you_message', $message, $order_id, $memberships ); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Return an instance of this class. |
|
42
|
|
|
* |
|
43
|
|
|
* @since 1.0.0 |
|
44
|
|
|
* |
|
45
|
|
|
* @return object \lsx_health_plan\classes\integrations\woocommerce\Checkout() A single instance of this class. |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function get_instance() { |
|
48
|
|
|
// If the single instance hasn't been set, set it now. |
|
49
|
|
|
if ( null === self::$instance ) { |
|
50
|
|
|
self::$instance = new self(); |
|
51
|
|
|
} |
|
52
|
|
|
return self::$instance; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Return the Place Order Text |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $label |
|
59
|
|
|
* @return void |
|
60
|
|
|
*/ |
|
61
|
|
|
public function checkout_button_text( $label = '' ) { |
|
62
|
|
|
$label = __( 'Place order', 'lsx-health-plan' ); |
|
63
|
|
|
return $label; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Saves the Plan ID to the cart data, so we can attach it to the order later. |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $cart_item_data |
|
70
|
|
|
* @param string $product_id |
|
71
|
|
|
* @param string $variation_id |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
|
|
public function add_plan_id_to_cart( $cart_item_data, $product_id, $variation_id ) { |
|
75
|
|
|
$plan_id = filter_input( INPUT_GET, 'plan_id' ); |
|
76
|
|
|
if ( empty( $plan_id ) || '' === $plan_id ) { |
|
77
|
|
|
return $cart_item_data; |
|
78
|
|
|
} |
|
79
|
|
|
$cart_item_data['plan_id'] = $plan_id; |
|
80
|
|
|
return $cart_item_data; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Output the WooCommerce Cart Notices. |
|
85
|
|
|
* |
|
86
|
|
|
* @return void |
|
87
|
|
|
*/ |
|
88
|
|
|
public function cart_notices() { |
|
89
|
|
|
if ( function_exists( 'woocommerce_output_all_notices' ) && is_post_type_archive( 'plan' ) ) { |
|
90
|
|
|
echo wp_kses_post( '<div class="col-md-12 col-sm-12 woocommerce-notices-wrapper">' ); |
|
91
|
|
|
wc_print_notices(); |
|
|
|
|
|
|
92
|
|
|
echo wp_kses_post( '</div>' ); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Changes the add to cart message and adds our course name. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $message |
|
100
|
|
|
* @param array $products |
|
101
|
|
|
* @param boolean $show_qty |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
public function add_to_cart_message( $message, $products, $show_qty ) { |
|
105
|
|
|
if ( isset( $_GET['plan_id'] ) ) { // @codingStandardsIgnoreLine. |
|
106
|
|
|
$this->plan_id = sanitize_text_field( wp_slash( $_GET['plan_id'] ) ); // @codingStandardsIgnoreLine. |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
$title = '<strong>' . get_the_title( $this->plan_id ) . '</strong>'; |
|
|
|
|
|
|
109
|
|
|
$title = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', 1, 'lsx-health-plan' ), $title ); |
|
110
|
|
|
|
|
111
|
|
|
// Output success messages. |
|
112
|
|
|
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) { |
|
113
|
|
|
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) ); |
|
|
|
|
|
|
114
|
|
|
$message = sprintf( '<a href="%s" tabindex="1" class="btn button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue shopping', 'lsx-health-plan' ), $title ); |
|
115
|
|
|
} else { |
|
116
|
|
|
$message = sprintf( '<a href="%s" tabindex="1" class="btn button wc-forward">%s</a> %s', esc_url( wc_get_cart_url() ), esc_html__( 'View cart', 'lsx-health-plan' ), $title ); |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
return $message; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Replaces the links on the thank you page. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $message |
|
126
|
|
|
* @param int $order_id |
|
127
|
|
|
* @param object $memberships |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
public function memberships_thank_you_links( $message, $order_id, $memberships ) { |
|
131
|
|
|
$plan_slug = \lsx_health_plan\functions\get_option( 'my_plan_slug', false ); |
|
132
|
|
|
if ( false !== $plan_slug && '' !== $plan_slug ) { |
|
133
|
|
|
$message = preg_replace( '/<a(.*)href="([^"]*)"(.*)>/', '<a$1href="' . home_url( $plan_slug ) . '"$3>', $message ); |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
return $message; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|