Set My Account custom items endpoints titles in WooCommerce - php

I customise my woocommerce account pages by adding new endpoints successfully.
The title oh this new endpoints is "My account" default title. I would like to customize the title as well.
I tried with the hook "woocommerce_endpoint_{endpoints}_title", which works perfectly on defaults endpoints, but it doesn't seem to work on custom endpoint :
My custom endpoint (not working):
add_filter( 'woocommerce_endpoint_favoris_title', 'change_my_account_favoris_title', 10, 2 );
function change_my_account_favoris_title( $title, $endpoint ) {
$title = __( "Tests", "woocommerce" );
return $title;
}
Custom endpoint
Default endpooint example working:
add_filter( 'woocommerce_endpoint_edit-account_title', 'change_my_account_edit_title', 10, 2 );
function change_my_account_edit_title( $title, $endpoint ) {
$title = __( "Tests", "woocommerce" );
return $title;
}
Default endpoint
Can't find anything on this for now,
Thanks for your time
EDIT:
My complete hooks of my woocommerce account section:
1/ Creation of endpoints :
add_filter ('woocommerce_account_menu_items', 'custom_log_history_link', 40);
function custom_log_history_link($menu_links){
$menu_links = array_slice( $menu_links, 0, 5, true )
+ array( 'favoris' => 'Mes favoris' )
+ array_slice( $menu_links, 5, NULL, true )
+ array( 'delete-account' => 'Supprimer mon compte' )
+ array_slice( $menu_links, 5, NULL, true );
return $menu_links;
}
add_action( 'init', 'custom_add_endpoint' );
function custom_add_endpoint() {
add_rewrite_endpoint( 'favoris', EP_PAGES );
add_rewrite_endpoint( 'delete-account', EP_PAGES);
}
2/ Content of new endpoints:
add_action( 'woocommerce_account_favoris_endpoint', 'custom_my_account_endpoint_content_favoris' );
function custom_my_account_endpoint_content_favoris() {
$user = wp_get_current_user();
$user_id=$user->ID;
$favoris=get_field('liste_des_favoris','user_'.$user_id);
$favoris_id=array();
echo "<h3>Vos produits favoris :</h3>";
if ($favoris!=''){
foreach ($favoris as $favori) {
$favoris_id[] = $favori['produit_favori'];
}
echo '<ul class="list-produits">';
foreach($favoris as $favori){
$product=wc_get_product($favori['produit_favori']);
$product = $product->get_data();
$sidebar = true;
ItemProduit($product,$sidebar,$favoris_id,$user_id);
}
echo '</ul>';
} else {
echo "<p>Vous n'avez pas de favoris pour le moment1.</p>";
}
}
add_action( 'woocommerce_account_delete-account_endpoint', 'custom_my_account_endpoint_content' );
function custom_my_account_endpoint_content() {
echo "<p>Etes-vous sûr de vouloir supprimer défintivement votre compte ?<br/>Attention ! Toute suppression de compte est définitive, vous perdrez tout l'historique de vos achats.<br/>En supprimant votre compte, toutes vos données personnelles seront définitivement effacées de notre base de données.</p>";
echo '<p class="status"></p>';
wp_nonce_field( 'ajax-delete-nonce', 'delete-security' );
echo '<div class="btns-delete">';
echo '<div class="btn btn-red" id="submit-delete">Supprimer</div>';
echo '</div>';
}
3/ Custom menu order :
add_filter ( 'woocommerce_account_menu_items', 'custom_sort_menu' );
function custom_sort_menu( $menu_links ){
$menu_links = array(
'dashboard' => 'Tableau de bord',
'orders' => 'Mes commandes',
'favoris' => 'Mes favoris',
'edit-address' => 'Mes adresses',
'edit-account' => 'Détails du compte',
'customer-logout' => 'Déconnexion',
'delete-account' => 'Supprimer mon compte',
);
return $menu_links;
}

To allow the composite filter hook woocommerce_endpoint_{$endpoint}_title to work with custom My Account endpoints, there is something missing from your code. You need to declare those endpoints as query vars in woocommerce_get_query_vars filter hook as follows:
add_filter( 'woocommerce_get_query_vars', 'myaccount_custom_endpoints_query_vars' );
function myaccount_custom_endpoints_query_vars( $query_vars ) {
$query_vars['favoris'] = 'favoris';
$query_vars['delete-account'] = 'delete-account';
return $query_vars;
}
Then to change that custom endpoints titles you can use effectively:
add_filter( 'woocommerce_endpoint_favoris_title', 'change_my_account_favoris_title' );
function change_my_account_favoris_title( $title ) {
return __( "Favoris", "woocommerce" );
}
add_filter( 'woocommerce_endpoint_delete-account_title', 'change_my_account_delete_account_title' );
function change_my_account_delete_account_title( $title ) {
return __( "Supprimer mon compte", "woocommerce" );
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Remember to refresh rewrite rules by saving changes on Wordpress settings "Permalinks" section.
Other notes:
In your actual code, you are using 2 times the hook woocommerce_account_menu_items in 2 functions… You need just one of them

One way of changing the title of the custom WooCommerce endpoints is to use the_title filter with the in_the_loop conditional
Here is an example that you'll need to modify per your own requirements.
function wpb_woo_endpoint_title( $title, $id ) {
if ( is_wc_endpoint_url( 'downloads' ) && in_the_loop() ) { // add your endpoint urls
$title = "Download MP3s"; // change your entry-title
}
elseif ( is_wc_endpoint_url( 'orders' ) && in_the_loop() ) {
$title = "My Orders";
}
elseif ( is_wc_endpoint_url( 'edit-account' ) && in_the_loop() ) {
$title = "Change My Details";
}
return $title;
}
add_filter( 'the_title', 'wpb_woo_endpoint_title', 10, 2 );

Related

Heading empty lines in html docs - WordPress

I just launched my new website. Everything is working ok, HOWEVER, I just noticed the HTML docs (source code of the page) has 15 blank lines before "<!doctype html>". This is causing some issues for SEO and to generate automatic sitemaps.
I searched all over the web, and finally find the issue: the functions.php in my child theme (storefront). When I delete or rename the functions.php in the child theme, the HTML doc is shown normally without blank spaces. I tried deleting one by one the functions coded in the file, but the issue persists until I delete all functions together. Also, I disabled all plugins and nothing solves, only deleting the mentioned file. Any ideas? Thank you very much!
below is the current content of functions.php
add_filter( 'woocommerce_product_tabs', 'my_remove_all_product_tabs', 98 );
function my_remove_all_product_tabs( $tabs ) {
unset( $tabs['description'] ); // Remove the description tab
unset( $tabs['reviews'] ); // Remove the reviews tab
unset( $tabs['additional_information'] ); // Remove the additional information tab
return $tabs;
}
?>
<?php // Este snippet regula el tiempo que se muestra el cart widget del WCPT
function wcpt_cart_js() {
?>
<script type="text/javascript">
// your javscript code goes here
jQuery(function($){
$('body').on('wcpt_cart', function(){
setTimeout(function(){
$('.wcpt-cart-widget ').fadeOut();
}, 2000); // delay
})
})
</script>
<?php
}
add_action('wp_head', 'wcpt_cart_js');
?>
<?php
/* quitar "Menu" del hamburger en moviles */
add_filter( 'storefront_menu_toggle_text', 'storefront_menu_toggle_text' );
function storefront_menu_toggle_text( $text ) {
$text = __( '' );
return $text;
}
?>
<?php
/* quitar la barra menu inferior nativa storefront en moviles
add_action( 'init', 'jk_remove_storefront_handheld_footer_bar' );
function jk_remove_storefront_handheld_footer_bar() {
remove_action( 'storefront_footer', 'storefront_handheld_footer_bar', 999 );
}*/
?>
<?php
/**
* Custom Filter for Gallery Image Captions
*
* Note: Avoid altering captioning, selector, and item tag.
*/
function mlc_gallery_image_caption($attachment_id, $captiontag, $selector, $itemtag) {
$id = $attachment_id;
// Grab the meta from the GIC plugin.
$my_image_meta = galimgcaps_get_image_meta($id);
/**
* Here's where to customize the caption content.
*
* This example uses the meta title, caption, and description.
*
* You can display any value from the $my_image_meta array.
* You can add your HTML too.
*/
/*
return "<{$captiontag} class='wp-caption-text gallery-caption' id='{$selector}-{$id}'>" .
"Title: " . $my_image_meta['title'] . "<br>" .
"Caption: " . $my_image_meta['caption'] . "<br>".
"Description: ". $my_image_meta['description'] .
"</{$captiontag}></{$itemtag}>";
*/
return "<{$captiontag} class='wp-caption-text gallery-caption' id='{$selector}-{$id}'>" .
$my_image_meta['caption'] . "<br>".
"</{$captiontag}></{$itemtag}>";
}
add_filter('galimgcaps_gallery_image_caption', 'mlc_gallery_image_caption', 10, 4);
?>
<?php
/**
* Cambia el texto del boton "anadir al carrito"
*/
add_filter('woocommerce_product_single_add_to_cart_text','QL_customize_add_to_cart_button_woocommerce');
function QL_customize_add_to_cart_button_woocommerce(){
return __('Comprar', 'woocommerce');
}
?>
<?php
/**
* Override theme default specification for product # per row
*/
function loop_columns() {
return 4; // n products per row
}
add_filter('loop_shop_columns', 'loop_columns', 999);
?>
<?php
add_filter('woocommerce_product_add_to_cart_text', 'archive_custom_cart_button_text');
function archive_custom_cart_button_text()
{
return __('Comprar', 'woocommerce');
}
?>
<?php
add_filter( 'woocommerce_get_price_html', 'add_price_prefix', 99, 2 );
function add_price_prefix( $price, $product ){
$price = '' . $price;
return $price;
}
?>
<?php
if (wp_is_mobile()){
add_filter( 'the_title', 'shorten_woo_product_title_mobile', 10 , 2 );
/*mobile_storefront_page_header();*/
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 6 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_show_product_images', 7 );
}
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
?>
<?php
function shorten_woo_product_title_mobile( $title, $id ) {
if ( ! is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' && strlen( $title ) > 20 ) {
return substr( $title, 0, 42) . '..'; // change last number to the number of characters you want
} else {
return $title;
}
}
?>
<?php
function mobile_storefront_page_header() {
if ( is_single() ) {
return;
}
?>
<header class="entry-header">
<?php
the_title( '<h1 class="entry-title">', '</h1>' );
storefront_post_thumbnail( 'full' );
?>
</header><!-- .entry-header -->
<?php
}
?>
<?php
/**
* Redirecciona el productos de los resultados en tabla a la pagina seo optimizada del producto agrupado
*/
add_action('template_redirect','custom_shop_page_redirect');
function custom_shop_page_redirect(){
global $post;
if (class_exists('WooCommerce')){
if(is_product()){
$product = get_product( $post->ID );
if( $product->is_type( 'grouped' ) ){
$slug = get_queried_object()->post_name;
$nslug = str_replace("tienda/","",$slug);
for ($x = 0; $x <= 20; $x++) {
$nslug = rtrim($nslug,$x);}
$nslug = rtrim($nslug,"-");
wp_safe_redirect(home_url($nslug));
exit();
}
}
}
return;
}
?>
<?php
/**
* cambia el formato del precio de agrupados
*/
add_filter( 'woocommerce_grouped_price_html', 'grouped_price_range_delete', 'grouped_price_prefix', 10, 3, 2 );
function grouped_price_range_delete( $price, $product, $child_prices) {
$price = '';
return $price;
}
?>
<?php
/**
* actualiza automaticamente el carrito al cambiar la cantidad
*/
add_action( 'wp_footer', 'cart_update_qty_script' );
function cart_update_qty_script() {
if (is_cart()) :
?>
<script>
jQuery('div.woocommerce').on('change', '.qty', function(){
jQuery("[name='update_cart']").trigger("click");
});
</script>
<?php
endif;
}```
I'm very sorry. Is now solved, i found the problem was the blank lines between each php open/close. Sorry for the useless post.
Regards!

WooCommerce: Change add to cart button text for multiple products with a redirection to checkout

In WooCommerce, I need to change some things for two products:
Add to cart button (different for both products)
Skip cart to checkout (same for two products)
I've found this code, that works. But just for one product id:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_cart_button_text' );
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_cart_button_text' );
function custom_cart_button_text($text) {
global $woocommerce;
global $post;
$post_id = $post->ID;
if($post_id == 11359){
$text = __( 'Ja ik word kompaan', 'woocommerce' );
}
return $text;
}
add_filter ('woocommerce_add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout($url) {
global $woocommerce;
if ( isset( $_POST['add-to-cart'] ) ) {
$product_id = (int)$_POST['add-to-cart'];
if($product_id == 11359){
$url = $woocommerce->cart->get_checkout_url();
}
}
return $url;
}
How would I make the first part of the snippet (renaming the add to cart text) so that I can use it two times; every product id needs a different add to cart text.
And how can I add a second product id to the second part of the snippet (so it skips the cart for both product id's)
Thanks!
You can use the following for 2 different products button texts and a redirection to checkout for both (where you will define your 2 product ids):
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_addtocart_button_text', 10, 2 );
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_addtocart_button_text', 10, 2 );
function custom_addtocart_button_text( $button_text, $product ) {
// 1st product
if ( $product->get_id() == 11359 ) {
$button_text = __( 'Ja ik word kompaan', 'woocommerce' );
}
// 2nd product
elseif ( $product->get_id() == 11362 ) {
$button_text = __( 'Something else', 'woocommerce' );
}
return $button_text;
}
add_filter ( 'woocommerce_add_to_cart_redirect', 'custom_redirect_to_checkout' );
function custom_redirect_to_checkout( $url ) {
if ( isset( $_POST['add-to-cart'] ) && $_POST['add-to-cart'] > 0 ) {
$product_id = intval( $_POST['add-to-cart'] );
if( in_array( $product_id, array( 11359, 11362 ) ) ){
$url = wc_get_checkout_url();
}
}
return $url;
}
Code goes in functions.php file of the active child theme (or active theme). It should works.

Hide all products with a specific stock status from WooCommerce catalog

I'm using Flatsome template on Wordpress and Woocommerce site. I also create custom stock status (example noproduzione, used when a product is not created anymore from manufacturer). But I don't want to show this products (noproduzione stock status) on my site (only in admin pages).
I'm using this code that I write here (I put it in my functions.php file), but it seems that on flatsome does not works. How to apply it on this template?
add_action( 'woocommerce_product_query', 'qc_action_product_query', 10, 2 );
function qc_action_product_query( $q, $query ) {
// Get any existing meta query
$meta_query = $q->get( 'meta_query');
// Define an additional meta query
$q->set( 'meta_query', array( array(
'key' => '_stock_status',
'value' => 'noproduzione',
'compare' => 'NOT LIKE',
) ) );
// Set the new merged meta query
$q->set( 'meta_query', $meta_query );
}
To create custom code I used some of codes found on StackOverflow
// Add new stock status options
add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );
function filter_woocommerce_product_stock_status_options( $status ) {
// Add new statuses
$status['10days'] = __( 'Disponibile entro 10 giorni', 'woocommerce' );
$status['inarrivo'] = __( 'In arrivo', 'woocommerce' );
$status['noproduzione'] = __( 'Fuori produzione', 'woocommerce' );
return $status;
}
// Availability text
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );
function filter_woocommerce_get_availability_text( $availability, $product) {
switch( $product->get_stock_status() ) {
case '10days':
$availability = __( 'Disponibile entro 10 giorni', 'woocommerce' );
break;
case 'inarrivo':
$availability = __( 'In arrivo', 'woocommerce' );
break;
case 'noproduzione':
$availability = __( 'Fuori produzione', 'woocommerce' );
break;
}
return $availability;
}
I added code to show the text of availability, and also to hide the cart button if the product is noproduzione stock status
// Display the stock status on other page
add_action( 'woocommerce_after_shop_loop_item_title', 'wcs_stock_text_shop_page', 25 );
function wcs_stock_text_shop_page() {
//returns an array with 2 items availability and class for CSS
global $product;
$availability = $product->get_stock_status();
//check if availability in the array = string 'noproduzione'
//if so display on page.//if you want to display the 'in stock' messages as well just leave out this, == 'Out of stock'
if ( $product->get_stock_status() === 'noproduzione') {
echo '<span style="color:#b20000;">Fuori produzione!</span>';
}
else if ( $product->get_stock_status() === 'onbackorder') {
echo '<span style="color:#13b200;">Disponibile su ordinazione</span>';
}
else if ( $product->get_stock_status() === '10days') {
echo '<span style="color:#13b200;">Disponibile in 10 giorni</span>';
}
else if ( $product->get_stock_status() === 'inarrivo') {
echo '<span style="color:#e0c81d;">In arrivo</span>';
}
else if ( $product->get_stock_status() === 'outofstock') {
echo '<span style="color:#b20000;">Terminato!</span>';
}
else {
echo '<span style="color:#53af00;">Disponibile!</span>';
}
}
// Hide the cart button if stock status is 'noproduzione'
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
add_filter('woocommerce_variation_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
function filter_is_purchasable_callback( $purchasable, $product ) {
if ( $product->get_stock_status() === 'noproduzione' ) {
return false;
}
return $purchasable;
}
Last, I also added this code, is useful because order the RELATED PRODUCT by "instock" status, and for me is good because do not show the other custom stock status. But I want to apply this to all of my frontend site.
//show, in the related products, only instock products!
add_filter( 'woocommerce_related_products', 'filter_woocommerce_related_products', 10, 3 );
function filter_woocommerce_related_products( $related_posts, $product_id, $args ) {
foreach( $related_posts as $key => $related_post ) {
// Get product
$related_product = wc_get_product( $related_post );
// Is a WC product
if ( is_a( $related_product, 'WC_Product' ) ) {
// Stock status
$stock_status = $related_product->get_stock_status();
// NOT instock
if ( $stock_status != 'instock' ) {
unset( $related_posts[$key] );
}
}
}
return $related_posts;
}
So, the question is: how to hide this products (noproduzione stock status) on my site (and show only in admin pages)? Noproduzione is different from OUTOFSTOCK!
You should better use dedicated woocommerce_product_query_meta_query filter hook as follows, to hide, from your store, all products that have a specific stock status (works with custom stock status):
add_action( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 1000 );
function custom_product_query_meta_query( $meta_query ) {
if ( ! is_admin() ) {
$meta_query[] = array(
'key' => '_stock_status',
'value' => 'noproduzione',
'compare' => '!=',
);
}
return $meta_query;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works on all versions since WooCommerce 3.

Show Woocommerce product pages default description for empty descriptions

Is it possible to add Woocommerce Product Default Description for all new added product pages in the backend product description area as a default text for example ( free of charge ) or a default function which will generate a certain action but only for the products who doesn't have a product description value
All that is needed is to get the description have a default added value for the new added products in the description below
Can anyone help in this ?
I found this which do the magic but for the product short description, but i want it to the product description itself
add_action( 'woocommerce_single_product_summary', 'bbloomer_echo_short_desc_if_empty', 21 );
function bbloomer_echo_short_desc_if_empty() {
global $post;
if ( empty ( $post->post_excerpt ) ) {
$post_excerpt = '<p class="default-short-desc">';
$post_excerpt .= 'This is the default, global, short description.<br>It will show if <b>no short description has been entered!</b>';
$post_excerpt .= '</p>';
echo $post_excerpt;
}
}
function woocommerce_default_description($content) {
$empty = empty($content) || trim($content) === '';
if(is_product() && $empty) {
$content = 'default text content';
}
return $content;
}
add_filter( 'the_content', 'woocommerce_default_description' );
function rmg_woocommerce_default_product_tabs($tabs) {
if (empty($tabs['description'])) {
$tabs['description'] = array(
'title' => __( 'Description', 'woocommerce' ),
'priority' => 10,
'callback' => 'woocommerce_product_description_tab',
);
}
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'rmg_woocommerce_default_product_tabs' );
something like above should work.
Try this:
add_action( 'woocommerce_single_product_summary', 'bbloomer_echo_desc_if_empty', 21 );
function bbloomer_echo_desc_if_empty() {
global $post;
if ( empty ( $post->post_content ) ) {
$post_description = '<p class="default-content">';
$post_description .= 'This is the default, global, description.<br>It will show if <b>description has been entered!</b>';
$post_description .= '</p>';
echo $post_description;
}
}

Change add to cart button based on IP-adress (GeoLocation) in Woocommerce

I have a question about a WooCommerce shop I am currently working on. The shop only contains only two languages, Dutch and English.
Is it possible when somebody from Poland visits the English version of the webshop and then navigates to the WooCommerce Products page it does not show the "Add to Cart" option but displays a different button with another link based on IP-adres (Geo-Location)?
EDIT
Ok, I managed to get it to work but not with your example:
// Wijzigingen Links en teksten knoppen toevoegen
function custom_product_button(){
// GEOLocatie aanroepen en verschillende distrubiteurs toevoegen
$geoip = geoip_detect2_get_info_from_current_ip();
$country = $geoip->raw[ 'country' ][ 'iso_code' ];
$button_text = __( "To distributor's website", "woocommerce" );
$button_usa = 'https://google.com';
$button_singapore = 'https://www.google.com.sg';
// Tonen van buttons met verschillende linkjes
?>
<form class="cart">
<input type="submit" value="<?php echo $button_text; ?>" onClick="window.open('<?php if ( 'US' === $country ) {echo $button_usa;}if ( 'SG' === $country ) {echo $button_singapore;}?>');"class="single_add_to_cart_button button alt">
</form>
<?php
}
// Vervangen van de button op single product pagina
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
global $product;
$geoip = geoip_detect2_get_info_from_current_ip();
$country = $geoip->raw[ 'country' ][ 'iso_code' ];
if ( 'US' === $country || 'SG' === $country)
{
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'custom_product_button', 30 );
}
}
// Vervangen van de button op loop pagina en categorie / archief pagina
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
$geoip = geoip_detect2_get_info_from_current_ip();
$country = $geoip->raw[ 'country' ][ 'iso_code' ];
if ( 'US' === $country || 'SG' === $country) {
$button_text = __( "View product", "woocommerce" );
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
return $button;
}
Maybe not the best coded piece but it is working in combination with the Plug-in GeoIP Detection.
Your coding looks much better but doen't seem te work. No errors just always the add to cart button.
Updated: on April 21th, 2018
Woocommerce has a geolocation enabled tool (and WC_Geolocation related class) by default.
The first below uses that geolocation in a custom conditional function, where you will defined the related countries that are going to enabled your demo button.
In that function you will set all the related country codes in the array (I have defined 'US' and 'SG' just for testing purpose).
The code:
// Updated: check for the defined country codes based over user IP geolocation country code
function country_geo_ip_check(){
// ==> HERE define the countries codes in the array
$non_allowed_countries = array('US', 'SG');
// Get an instance of the WC_Geolocation object class
$geolocation_instance = new WC_Geolocation();
// Get user IP
$user_ip_address = $geolocation_instance->get_ip_address();
// Get geolocated user IP country code.
$user_geolocation = $geolocation_instance->geolocate_ip( $user_ip_address );
return in_array( $user_geolocation['country'], $non_allowed_countries ) ? false : true;
}
// Shop and other archives pages (replacing add to cart by a linked button to the product)
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
if ( country_geo_ip_check() ) return $button; // Exit for non defined countries
if ( $product->is_type( 'variable' ) ) return $button; // Excluding variable products
$button_text = __( "View product", "woocommerce" );
$button_link = $product->get_permalink();
return '<a class="button" href="' . $button_link . '">' . $button_text . '</a>';;
}
// Single producct pages
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
global $product;
if ( country_geo_ip_check() ) return; // Exit for non defined countries
// For variable product types (keeping attribute select fields)
if( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_single_product_summary', 'custom_demo_button', 20 );
}
// For all other product types
else {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'custom_demo_button', 30 );
}
}
// Your custom button replacement
function custom_demo_button() {
global $post, $product;
$style = 'style="padding-right: 0.75em;padding-left: 0.75em;margin-left: 8px; background-color: #0ebc30;"';
$button_text = __("View Demo", "woocommerce");
// Get demo Url
if( function_exists('get_field') )
$url_demo = get_field( "url_demo" );
if( ! isset($url_demo) ){ // If the Url is not defined
$button_text = __("Missing URL", "woocommerce");
$style = 'style="color: red; background-color: white; border: solid 1px red"';
$url_demo = '#';
}
// Output
echo '<a href="'.$url_demo.'" target="_blank" class="button demo_button"'.$style.'>'.$button_text.'</a>';
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.

Categories