I have a jQuery script which successfully retrieves data from local storage and then returns to a PHP function (within a child theme). Within this function I have an if/else block which I want to use in order to change all buttons on all shop pages and linked product pages when opened. depending on the result obtained from the jQuery script.
There are 3 options : default "check coverage", standard "add to cart" and finally "register your interest"
I have 3 categories and want to exclude the 'hardware category
How can I apply a filter to all product pages matching a tag? The code below applies this to all the shop pages items on the store, but not the linked product page for the current customer without overwriting other customers views.
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
$button_text = __("Check Coverage", "woocommerce");
$button = '<a class="button" href="******/coverage">' . $button_text . '</a>';
return $button;
}
Any guidance, criticisms or thoughts are appreciated.
EDIT - The Ajax code that I use:
jQuery script enqueued from functions.php in child theme:
jQuery.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php",
data: {
action: 'dynamic_product_links',
// add your parameters here
cat1: localStorage.getItem("cat1"),
cat2: localStorage.getItem("cat2"),
},
success: function (output) {
console.log(output);
}
});
functions.php
add_action('wp_enqueue_scripts', 'woocommerce_ajax_check_coverage_js', 99);
function woocommerce_ajax_check_coverage_js(){
//echo "<script type='text/javascript'>alert('called woocommerce_ajax_check_coverage_js ');</script>";
wp_register_script( 'woocommerce-ajax-check-coverage-js', get_stylesheet_directory_uri() . "/assets/ajax-check-coverage.js", array('jquery'), null, true );
wp_enqueue_script('woocommerce-ajax-check-coverage-js');
}
// register the ajax action for authenticated users
add_action('wp_ajax_dynamic_product_links', 'dynamic_product_links');
// register the ajax action for unauthenticated users
add_action('wp_ajax_nopriv_dynamic_product_links', 'dynamic_product_links');
function dynamic_product_links() {
// ajax data received from jQuery script which retrieves data from local storage
$cat1_speed = $_REQUEST['cat1']; // can be either 'Check Coverage','No Coverage' or a number linked to a custom atttribute
$cat2_speed = $_REQUEST['cat2'];
if ($cat1_speed == 'check ooverage' || $cat2_speed == 'check coverage') {
// set all matching categories except hardware to check coverage
}
if ($cat1_speed == 'No Coverage' ) {
// remove this catogory item set from the store (hide?) -> change any product/* pages add to cart button to "register your interest"
}
if ($cat1_speed == '20' || $cat2_speed == 'check coverage') {
// remove this catogory item set from the store (hide?) if the products atttribute is greater than the number
}
// in the end, returns success json data
wp_send_json_success([/* some data here */$wireless_speed ,$fusion_fibre_speed]);
// or, on error, return error json data
wp_send_json_error([/* some data here */]);
}