Created
April 11, 2023 14:35
-
-
Save sajdoko/69ba6d1ee4b41a9d7dcfc5290c1ef68e to your computer and use it in GitHub Desktop.
Remove "Add to Cart" button and replace it with a "Request Quote" button based on the user's location.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) { | |
// WooCommerce plugin is active class is loaded | |
function replace_add_to_cart_button() { | |
// Output the "Request Quote" button | |
echo '<a href="/contatti/" class="request-quote-button single_add_to_cart_button button alt wp-element-button">Richiedi un preventivo</a>'; | |
return; | |
} | |
if (! class_exists( 'WC_Geolocation' ) ) { | |
require_once( WP_PLUGIN_DIR . '/woocommerce/includes/class-wc-geolocation.php' ); | |
} | |
// Get an instance of the WC_Geolocation object class | |
$geo_instance = new WC_Geolocation(); | |
// Get geolocated visitor geo data. | |
$visitor_location = $geo_instance->geolocate_ip(); | |
$visitor_country = $visitor_location['country'] ?? false; | |
// List of countries to show request quote button instead of add to cart. | |
$countries = array("GB", "CH", "AL"); | |
// Check if visitor is from one of the specified countries | |
if ( in_array( $visitor_country, $countries ) ) { | |
// Remove the add to cart button | |
add_filter( 'woocommerce_is_purchasable', '__return_false'); | |
//add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_add_to_cart_button', 10, 2 ); | |
add_action( 'woocommerce_after_shop_loop_item', 'replace_add_to_cart_button', 20 ); | |
add_action( 'woocommerce_single_product_summary', 'replace_add_to_cart_button', 30 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment