-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-to-cart-pricing.php
142 lines (115 loc) · 5.39 KB
/
add-to-cart-pricing.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
<?php
/*
Plugin Name: FFL Cockpit Add-to-Cart Pricing Add-On
Plugin URI: https://garidium.com
Description: Add-to-Cart pricing when list price is below MAP
Version: 1.0.0
Author: Garidium LLC
Author URI: https://garidium.com
*/
// If this file is called directly, abort.
if (!defined('WPINC')) {
die;
}
// Prevent direct access
if (!defined('ABSPATH')) exit;
add_filter('woocommerce_get_price_html', 'custom_map_price_check', 10, 2);
function custom_map_price_check($price, $product) {
// Exit early if we are in the admin area to prevent changes in the backend
if (is_admin()) {
return $price;
}
// Get the product ID
$product_id = $product->get_id();
// Get the map_price meta value
$map_price = get_post_meta($product_id, 'map_price', true);
// Get the regular price and sale price of the product
$regular_price = floatval($product->get_regular_price());
$sale_price = floatval($product->get_sale_price());
// Determine the display price (sale price if available, otherwise regular price)
$display_price = $sale_price ?: $regular_price;
// Check if the display price is below the MAP price
if ($map_price && $display_price < floatval($map_price)) {
// Check if we're in a dynamic search result or AJAX request
if (
(function_exists('is_ajax') && is_ajax()) || // Standard AJAX check
(isset($_REQUEST['wc-ajax']) && $_REQUEST['wc-ajax'] === 'product_search') || // WooCommerce AJAX search
(isset($_REQUEST['action']) && $_REQUEST['action'] === 'ajax_search') // Custom AJAX search
) {
return '$'.number_format($map_price, 2). ' <span class="cockpit-map-message-inner">(View Product to See Lower Price)</span>';
}
// Add unique identifiers for each product
$unique_id = 'product-' . $product_id;
// Custom message with MAP price
return sprintf(
'<div id="%s" class="cockpit-map-section">
<span class="cockpit-map-price" style="display: none;">%s</span>
<span class="cockpit-map-message">%s <span class="cockpit-map-message-inner">is the lowest price we can show publicly,</span> <a href="#" class="cockpit-map-link" style="text-decoration: underline;">Click Here</a> <span class="cockpit-map-message-inner">to show the real price.</span></span>
</div>
<script>
document.addEventListener("DOMContentLoaded", function() {
const mapSection = document.querySelector("#%s");
const mapLink = mapSection.querySelector(".cockpit-map-link");
const mapMessage = mapSection.querySelector(".cockpit-map-message");
const mapPrice = mapSection.querySelector(".cockpit-map-price");
mapLink.addEventListener("click", function(event) {
event.preventDefault(); // Prevent the default anchor behavior
mapMessage.style.display = "none";
mapPrice.style.display = "inline";
});
});
</script>',
$unique_id,
wc_price($display_price),
wc_price($map_price),
$unique_id
);
}
// Return the original price if it doesn't meet the condition
return $price;
}
/*
add_filter('woocommerce_get_price_html', 'custom_map_price_check', 10, 2);
function custom_map_price_check($price, $product) {
// Get the product ID
$product_id = $product->get_id();
// Get the map_price meta value
$map_price = get_post_meta($product_id, 'map_price', true);
// Get the regular price and sale price of the product
$regular_price = floatval($product->get_regular_price());
$sale_price = floatval($product->get_sale_price());
// Determine the display price (sale price if available, otherwise regular price)
$display_price = $sale_price ?: $regular_price;
// Check if the display price is below the MAP price
if ($map_price && $display_price < floatval($map_price)) {
// Custom message with MAP price
return sprintf(
'<div class="cockpit-map-section"><span class="cockpit-map-price">%s</span><span class="cockpit-map-message"> is the lowest price we can show. Click Here to see the real price.</span></div>',
wc_price($map_price)
);
}
// Return the original price if it doesn't meet the condition
return $price;
}
*/
/*
// Add a custom message to the product grid as well
add_action('woocommerce_after_shop_loop_item_title', 'custom_map_price_on_product_grid', 15);
function custom_map_price_on_product_grid() {
global $product;
// Get the map_price meta value
$map_price = get_post_meta($product->get_id(), 'map_price', true);
// Get the regular price and sale price of the product
$regular_price = floatval($product->get_regular_price());
$sale_price = floatval($product->get_sale_price());
// Determine the display price (sale price if available, otherwise regular price)
$display_price = $sale_price ?: $regular_price;
// Check if the display price is below the MAP price
if ($map_price && $display_price < floatval($map_price)) {
echo sprintf(
'<span class="map-message">%s is the lowest price we can show. Add to cart to see the real price.</span>',
wc_price($map_price)
);
}
}
*/