GeoIP integration – Hide pricing & cart functionality
-
Hello fellow WPengine GeoIP users. As someone who has limited PHP dev skills, I had a hard time finding the solution to integrating this plugin with my site. I ended up putting together a hodgepodge of code I found on various sites to get this to work the way I wanted. I am posting this here in case anyone else finds a use for this and I am looking for feedback on how I could have done this better. It is currently operating as intended but I am sure it could have been coded more optimally. For example, I would like to find a better way to hide products from shop & search that doesn’t involve me adding a tag to the product, but this works for now. Also, I don’t think setting elements to “display: none !important;” is best practice, but again, this is functioning currently.
My goal was to have my site function as an e-commerce site inside the US and to function as a catalog to anyone outside the US.
I am using the code below to hide pricing, add to cart, certain CSS elements on specific pages, hide specific products from search and shop loop, and various other plugin functionality.
Plugins that were involved: ConvertPlug, Affirm, WooCommerce, WooCommerce MSRP pricing, WooCommerce Catalog Visibility Options, WooCommerce Advanced Product Labels.
I tried to comment on the code to explain what does what, if anyone has questions I will answer if possible.
I used this plugin to add the code to my site Code Snippets. It was my first experience with it and it seems a lot easier than adding code to your functions.php file, especially if you don’t have a child theme. Can’t recommend this plugin enough if you’re doing any custom work like this.
TLDR: Heres some code.
add_action('init', 'custom_visibility_in'); function custom_visibility_in() { $country = do_shortcode('[geoip-country]'); if ($country !== 'US') { remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 ); // Removes price from shop loop remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 ); // Removes add to cart button from shop loop add_action( 'woocommerce_single_product_summary', 'hide_single_product_prices', 1 ); // Function to hide price on product pages based on product type (variable or simple) add_action( 'woocommerce_single_product_summary', 'custom_visibility', 31 ); // Adds "Request a Quote" in place of "Add to Cart" add_action( 'wp_enqueue_scripts', 'my_styles_method' ); // Adds the display: none for Advanced Labels plugin add_action( 'wp_enqueue_scripts', 'my_styles_method_home' ); // Home & Tank page specific custom styles (.css) to hide certain divs. add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' ); // Hides select products with a specified tag from the loop, eg 'USA' add_action( 'pre_get_posts', 'tq_pre_get_posts' ); // Hides select products from search with a specified tag from the loop, eg 'USA' // add_action( 'woocommerce_after_shop_loop_item', 'custom_visibility', 11 ); //commented out to keep orange button off of product loop global $woocommerce_msrp_frontend; // Hides MSRP pricing on shop loop and product page remove_action( 'woocommerce_single_product_summary', array( $woocommerce_msrp_frontend, 'show_msrp' ), 7 ); remove_action( 'woocommerce_after_shop_loop_item_title', array( $woocommerce_msrp_frontend, 'show_msrp' ), 9 ); global $wc_affirm_loader; // Hides Affirm messaging on product page remove_action( 'woocommerce_single_product_summary', array( $wc_affirm_loader, 'woocommerce_single_product_summary' ), 15 ); remove_action( 'wp_enqueue_scripts', array( $wc_affirm_loader, 'possibly_enqueue_scripts' ) ); global $Smile_Slide_Ins; // Hides Convert Plug Slide in from Displaying remove_action( 'wp_enqueue_scripts',array($Smile_Slide_Ins,'enqueue_front_scripts' ), 100); remove_action( 'wp_footer', array( $Smile_Slide_Ins, 'load_slide_in_globally' ) ); remove_action( 'admin_enqueue_scripts',array($Smile_Slide_Ins,'enqueue_admin_scripts' ) ); remove_action( 'admin_menu',array($Smile_Slide_Ins,'add_admin_menu_page' ), 999); remove_action( 'admin_head',array($Smile_Slide_Ins,'load_customizer_scripts' ) ); remove_action( 'init', array( $Smile_Slide_Ins, 'register_theme_templates') ); remove_filter( 'admin_body_class', array( $Smile_Slide_Ins, 'cp_admin_body_class') ); global $Smile_Modals; // Hides Convert Plug Modals + styles and scripts in head and footer remove_action( 'wp_enqueue_scripts',array($Smile_Modals,'enqueue_front_scripts' ), 100); remove_action( 'admin_enqueue_scripts',array($Smile_Modals,'enqueue_admin_scripts' ) ); remove_action( 'admin_menu',array($Smile_Modals,'add_admin_menu_page' ), 999); remove_action( 'admin_head',array($Smile_Modals,'load_customizer_scripts' ) ); remove_action( 'wp_footer', array( $Smile_Modals, 'load_modal_globally' ) ); remove_action( 'init', array( $Smile_Modals, 'register_theme_templates') ); remove_filter( 'admin_body_class', array( $Smile_Modals, 'cp_admin_body_class') ); } } function hide_single_product_prices(){ global $product; remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 ); // Removes price from woocommerce product page if( ! $product->is_type('variable') ){ remove_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart', 30 ); // If product IS NOT a variable product, remove add to cart button } else { remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 10); // If product IS variable type, remove price remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 ); // If product IS variable type, remove add to cart button } } function custom_visibility() { echo '<div id="mk-button-2" class="mk-button-container _ relative inline left"><a id="request-quote-btn" class="request-quote-btn mk-button js-smooth-scroll mk-button--dimension-flat mk-button--size-large mk-button--corner-rounded text-color-light _ relative text-center font-weight-700 no-backface letter-spacing-2 inline" href="https://www.torquefitness.com/sales-quotes/" target="_self"><span class="mk-button--text">REQUEST A QUOTE</span></a></div><br><br>'; //echo '<a href="' . get_permalink(wc_get_page_id('myaccount')) . '">' . __('Login to see prices', 'theme_name') . '</a>'; // Could be used in the future to show pricing for logged in users. Would need to add to function custom_visibility_in() to enable this. } function my_styles_method() { wp_enqueue_style( 'custom-style', get_stylesheet_directory_uri() . '/style.css' ); $custom_css = ".br_alabel{display: none !important;}"; $custom_css_two = ".wpb_row.vc_row.vc_row-fluid.mk-fullwidth-true.attched-false.vc_custom_1491585914197.js-master-row.mk-in-viewport{display: none !important;}"; wp_add_inline_style( 'custom-style', $custom_css ); wp_add_inline_style( 'custom-style', $custom_css_two ); } function my_styles_method_home() { if( is_page('Home') ) { wp_enqueue_style( 'custom-style-home', get_stylesheet_directory_uri() . '/style.css' ); $custom_css = " .triple-section{ display: none !important;}"; wp_add_inline_style( 'custom-style-home', $custom_css ); } elseif( is_page('tank-all-surface-sled') ) { wp_enqueue_style( 'custom-style-home', get_stylesheet_directory_uri() . '/style.css' ); $custom_css_tank = " .tank-title-and-paragraph .single{ display: none !important;}"; wp_add_inline_style( 'custom-style-home', $custom_css_tank ); } } function custom_pre_get_posts_query( $q ) { $tax_query = (array) $q->get( 'tax_query' ); $tax_query[] = array( 'taxonomy' => 'product_tag', 'field' => 'slug', 'terms' => array( 'USA' ), // Don't display products with the tag "USA" 'operator' => 'NOT IN' ); $q->set( 'tax_query', $tax_query ); } /* // Only works for logged in admin users, not for anyone else... Not sure why. SD 9/25/17 function custom_pre_get_posts_query( $q ) { if ( ! is_admin() && $q->is_main_query() ) { $q->set('post__not_in', array(8342, 8341, 8177, 8175)); // Enter product IDs seperated with a comma to hide from International Customers } } */ function tq_pre_get_posts( $query ) { if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) { $query->set( 'post_type', array( 'product' ) ); $tax_query = array( array( 'taxonomy' => 'product_tag', 'field' => 'slug', 'terms' => array( 'USA' ), // put tags here you want excluded from search results 'operator' => 'NOT IN', ), ); $query->set( 'tax_query', $tax_query ); } }
- The topic ‘GeoIP integration – Hide pricing & cart functionality’ is closed to new replies.