-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-to-cart-modal.php
146 lines (122 loc) · 4.05 KB
/
add-to-cart-modal.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
<?php
/**
* Plugin Name: Add to Cart Modal Window for WooCommerce
* Description: Display a modal window after adding an item to the cart.
* Version: 1.0.0
* Author: Diego Giraldo
* Author URI: https://dgiraldo.co
* Developer: Diego Giraldo
* Developer URI: http://dgiraldo.co/
* Text Domain: add-to-cart-modal
* Domain Path: /languages
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Check if WooCommerce is active
**/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
// Definitions.
define( 'ACMW_WC_VERSION', '1.0.0' );
define( 'ACMW_WC_DIR', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
define( 'ACMW_WC_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
add_action( 'wp', 'init' );
// Main filter.
add_action( 'woocommerce_add_to_cart', 'acmw_hooks' );
function init() {
if ( function_exists( 'is_shop' ) && is_shop() ) {
add_action( 'wp_enqueue_scripts', 'acmw_general_script' );
add_action( 'woocommerce_after_main_content', 'acmw_echo_modal_boilerplate', 20 );
}
add_action( 'wp_enqueue_scripts', 'acmw_enqueue_styles' );
}
function acmw_hooks() {
add_filter( 'wc_add_to_cart_message_html', 'acmw_message_html', 10, 2 );
add_action( 'wp_enqueue_scripts', 'acmw_enqueue_product_script' );
}
/**
* Main function to generate modal window.
*
* @param mixed $message Default add to cart message.
* @param int|array $products Product ID list or single product ID.
*
* @return mixed
**/
function acmw_message_html( $message, $products ) {
$body = '';
foreach ( $products as $product_id => $qty ) {
$product = wc_get_product( $product_id );
if ( ! $product ) {
continue;
}
$body .= acmw_product_template( $product, $qty);
break;
}
if ( ! $body ) {
return $message;
}
$product_name = $product->get_name();
ob_start();
include ACMW_WC_DIR . '/templates/add-to-cart-modal.tpl.php';
$body = ob_get_clean();
return $body;
}
function acmw_general_script() {
wp_enqueue_script(
'acmw-wc-added-to-cart-js',
ACMW_WC_URL . '/assets/js/added_to_cart.js',
array(),
ACMW_WC_VERSION,
true
);
acmw_enqueue_product_script();
}
function acmw_enqueue_product_script() {
wp_enqueue_script(
'acmw-wc-modal-js',
ACMW_WC_URL . '/assets/js/script.js',
array(),
ACMW_WC_VERSION,
true
);
}
function acmw_enqueue_styles() {
wp_enqueue_style(
'acmw-wc-styles',
ACMW_WC_URL . '/assets/css/main.css',
array(),
ACMW_WC_VERSION
);
}
/**
* Main function to generate modal window.
*
* @param int $product_id Single product ID added to the cart.
* @param int $qty Product quantity added to the cart.
*
* @return mixed
**/
function acmw_product_template( $product, $qty ) {
if ( ! $qty ) {
return '';
}
$product_name = $product->get_name();
$product_price = $product->get_price();
$product_img = $product->get_image();
ob_start();
include ACMW_WC_DIR . '/templates/product.tpl.php';
return ob_get_clean();
}
function acmw_echo_modal_boilerplate( $product_name = '', $product_price = '', $product_img = '', $qty = 1, $hidden = true ) {
ob_start();
include ACMW_WC_DIR . '/templates/product.tpl.php';
$body = ob_get_clean();
ob_start();
include ACMW_WC_DIR . '/templates/add-to-cart-modal.tpl.php';
echo ob_get_clean();
}
}