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
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Return an instance of this class. |
38
|
|
|
* |
39
|
|
|
* @since 1.0.0 |
40
|
|
|
* |
41
|
|
|
* @return object \lsx_health_plan\classes\integrations\woocommerce\Checkout() A single instance of this class. |
42
|
|
|
*/ |
43
|
|
|
public static function get_instance() { |
44
|
|
|
// If the single instance hasn't been set, set it now. |
45
|
|
|
if ( null === self::$instance ) { |
46
|
|
|
self::$instance = new self(); |
47
|
|
|
} |
48
|
|
|
return self::$instance; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Return the Place Order Text |
53
|
|
|
* |
54
|
|
|
* @param string $label |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function checkout_button_text( $label = '' ) { |
58
|
|
|
$label = __( 'Place order', 'lsx-health-plan' ); |
59
|
|
|
return $label; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Saves the Plan ID to the cart data, so we can attach it to the order later. |
64
|
|
|
* |
65
|
|
|
* @param array $cart_item_data |
66
|
|
|
* @param string $product_id |
67
|
|
|
* @param string $variation_id |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function add_plan_id_to_cart( $cart_item_data, $product_id, $variation_id ) { |
71
|
|
|
$plan_id = filter_input( INPUT_GET, 'plan_id' ); |
72
|
|
|
if ( empty( $plan_id ) || '' === $plan_id ) { |
73
|
|
|
return $cart_item_data; |
74
|
|
|
} |
75
|
|
|
$cart_item_data['plan_id'] = $plan_id; |
76
|
|
|
return $cart_item_data; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Output the WooCommerce Cart Notices. |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function cart_notices() { |
85
|
|
|
if ( function_exists( 'woocommerce_output_all_notices' ) && is_post_type_archive( 'plan' ) ) { |
86
|
|
|
echo wp_kses_post( '<div class="col-md-12 col-sm-12 woocommerce-notices-wrapper">' ); |
87
|
|
|
wc_print_notices(); |
|
|
|
|
88
|
|
|
echo wp_kses_post( '</div>' ); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Changes the add to cart message and adds our course name. |
94
|
|
|
* |
95
|
|
|
* @param string $message |
96
|
|
|
* @param array $products |
97
|
|
|
* @param boolean $show_qty |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function add_to_cart_message( $message, $products, $show_qty ) { |
101
|
|
|
if ( isset( $_GET['plan_id'] ) ) { // @codingStandardsIgnoreLine. |
102
|
|
|
$this->plan_id = sanitize_text_field( wp_slash( $_GET['plan_id'] ) ); // @codingStandardsIgnoreLine. |
|
|
|
|
103
|
|
|
|
104
|
|
|
$title = '<strong>' . get_the_title( $this->plan_id ) . '</strong>'; |
|
|
|
|
105
|
|
|
$title = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', 1, 'lsx-health-plan' ), $title ); |
106
|
|
|
|
107
|
|
|
// Output success messages. |
108
|
|
|
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) { |
109
|
|
|
$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' ) ); |
|
|
|
|
110
|
|
|
$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 ); |
111
|
|
|
} else { |
112
|
|
|
$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 ); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
return $message; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|