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.
Related
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.
I have an online store with WooCommerce where the user who is not registered as a "Subscribed Customer" and tries to add a product to the cart is redirected to the page to subscribe.
In the case of the general store where all the products appear, it works perfectly with this code:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 30, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
if ( !is_user_logged_in() || !current_user_can('tienda') ){
if( $product->is_type( 'simple' ) ){
$button_text = __( "Debes suscribirte", "woocommerce" );
$button = __('<a class="button" href="https://glancingeye.com/modelado-3d/#suscripcion-modelado">' . $button_text . '</a>', "woocommerce");
}}
return $button;
}
But once inside a product I can not achieve that if the user is not registered he will be redirected to the subscription page.
So far I have tried with this script, I modified the text of the button but not the link. This is the code for this side:
if ( !is_user_logged_in() || !current_user_can('tienda') ){
add_filter( 'woocommerce_add_to_cart_form_action', 'boton_single' );
function boton_single( $product_permalink ){
// filter...
return _e('<a class="button" href="https://glancingeye.com/modelado-3d/#suscripcion-modelado">' . $button_text . '</a>', "woocommerce");
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 'texto_boton_single' );
function texto_boton_single() {
return _e( 'Debes suscribirte', 'boton_suscribete' );
}
}
What am I doing wrong?
I have made modifications to the coding.
add_filter( 'woocommerce_add_to_cart_form_action', 'boton_single' );
if ( !is_user_logged_in() || !current_user_can('tienda') ){
function boton_single( $product_permalink ){
// filter...
return _e( 'https://glancingeye.com/modelado-3d/#suscripcion-modelado', 'boton_suscribete' );
return $product_permalink;
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 'texto_boton_single' );
function texto_boton_single() {
return _e( 'Debes suscribirte', 'boton_suscribete' );
}
}
and now when you click on "Add to cart", the system adds the product to the cart (should not I am not a subscribed user) but in the url if the URL appears where I want it to redirect.
Once the product is added to the cart, if I try to add it again, this time it sends me to the subscription page (this should be done from the start).
I am using woocommerce and I have the following issue:
The products are displayed in the homepage with their price and add to cart button.
Add to cart button redirects to cart page.
The image of each product redirects to product page.
An important thing is to allow customers to be able to read the description of the product before adding it to cart.
Is there a way to replace add to cart button with read more in order to redirect from homepage to each product's page where the add to cart button will appear?
Replacing the button add to cart by a link to the product in Shop and archives pages for woocommerce 3+:
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 = __("View product", "woocommerce");
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
return $button;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested on WooCommerce 3+ and works. You can customize the text of button and you will get something like:
This code is working. But at this place for my theme woocommerce/include/class-wc-product-simple.php
// changes the "select options" text. Forget who to give credit to for this.
add_filter( 'woocommerce_product_add_to_cart_text', function( $text ) {
global $product;
if ( $product->is_type( 'variable' ) ) {
$text = $product->is_purchasable() ? __( 'More Options', 'woocommerce' ) : __( 'Read more', 'woocommerce' );
}
return $text;
}, 10 );
/**
* remove add to cart buttons on shop archive page
*/
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
if ( $product->is_type( 'simple' ) ) {
$button_text = __("View product", "woocommerce");
$button = '<a class="button" href="' . $product->get_permalink() . '">' .
$button_text . '</a>';
}
return $button;
}
Add these to your functions.php file in the theme folder
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
In case it helps, this worked for me to apply this solution only to simple products, since variable products don't "add to cart" on archive pages anyway. This way it can be clearer that a product has more options (if it is variable). I also changed the text of "select options" to "more options" in the example below (since not all products in my case would be purchasable even after viewing the single product page's url, which is another non-topical idea for this thread):
// changes the "select options" text. Forget who to give credit to for this.
add_filter( 'woocommerce_product_add_to_cart_text', function( $text ) {
global $product;
if ( $product->is_type( 'variable' ) ) {
$text = $product->is_purchasable() ? __( 'More Options', 'woocommerce' ) : __( 'Read more', 'woocommerce' );
}
return $text;
}, 10 );
/**
* remove add to cart buttons on shop archive page
*/
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
if ( $product->is_type( 'simple' ) ) {
$button_text = __("View product", "woocommerce");
$button = '<a class="button" href="' . $product->get_permalink() . '">' .
$button_text . '</a>';
}
return $button;
}
I got the same problem i had a donation product, so it had a custom price i.e. you could make any amount of donation so in the shop page i replaced the add to cart button for that product to "Details" which would redirect it to product single page from where user could make any donation. I used this code.
The code goes in your theme's or child theme's functions.php file
function filter_woocommerce_loop_add_to_cart_link( $quantity,$product ) {
$product_id = $product->get_id();
$title = $product->get_title();
$sku = $product->get_sku();
if($product_id == get_option( 'woocommerce_donations_product_id' )){
//var_dump($title);
//var_dump($sku);
//var_dump($quantity);
$simpleURL = get_permalink();
//var_dump($simpleURL);
$quantity='<span class="filter-popup">Détails</span>';
//var_dump($quantity);
}
return $quantity;
//exit();
};
// add the filter
add_filter( 'woocommerce_loop_add_to_cart_link','filter_woocommerce_loop_add_to_cart_link', 10, 2 );
This is my code:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' ); // 2.1 +
function woo_custom_cart_button_text( $text ) {
if( has_term( 'liners', 'product_cat' ) ){
$text = __( ' ', 'your-plugin' );
echo do_shortcode('Request а Quote');
}
return $text;
}
I need to make function to replace the "Add to cart" button Url and Text for just one specific product category.
This button will trigger a Lightbox with a contact form and the text for this button will be: Request a Quote.
How can I make it work as expected?
Here is how it works actually on this link.
Updated: for 2 different product categories (2 different buttons)
A global and complete solution for your products from 'liners' product category:
If one of your products (in 'liners' product category) is not a variable product, you need first to replace the add-to-cart button in shop and archives pages by a simple button linked to the product.
In single product pages you need to remove add-to-cart button and quantities fields, to replace it by your custom button.
Here is that code:
// Replacing the button add to cart by a link to the product in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'conditionally_replacing_add_to_cart_button', 10, 2 );
function conditionally_replacing_add_to_cart_button( $button, $product ) {
$categories = array('liners','custom-classics');
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
// For 'liners' product category
if( has_term( $categories, 'product_cat', $product_id ) ){
$button_text = __("View product", "woocommerce");
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
return $button;
}
// replacing add to cart button and quantities by your custom button in Single product pages
add_action( 'woocommerce_single_product_summary', 'conditionally_replacing_template_single_add_to_cart', 1, 0 );
function conditionally_replacing_template_single_add_to_cart() {
global $product;
$categories = array('liners','custom-classics');
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
function custom_button_replacement(){
global $product;
$categories = array('liners','custom-classics');
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
if( has_term( $categories[0], 'product_cat', $product_id ) )
$class_id = "923"; // liners
elseif( has_term( $categories[1], 'product_cat', $product_id ) )
$class_id = "925"; // custom-classics
else $class_id = ""; // none
// set below your custom text
$button_text = __('Request а Quote', 'woocommerce');
// Output your custom text
echo ''.$button_text.'';
}
// Only for 'liners' and 'custom-classics' product categories
if( has_term( $categories, 'product_cat', $product_id ) ):
// For variable product types
if( $product->is_type( 'variable' ) ){
// Removing add to cart button and quantities
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
// The button replacement
add_action( 'woocommerce_single_variation', 'custom_button_replacement', 20 );
}
else // For all other product types
{
// Removing add to cart button and quantities
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
// The button replacement
add_action( 'woocommerce_single_product_summary', 'custom_button_replacement', 30 );
}
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works for all product types (simple, variable…). You will get (example):
Here is my solution to replace the Add to Cart button with a Read More button, with class and id in a certain category.
/* // Replace the Add to Cart Btn in Category Ammunition with View Product Btn */
add_filter('woocommerce_loop_add_to_cart_link','change_simple_shop_add_to_cart',10,2);
function change_simple_shop_add_to_cart( $html, $product ){
$category_ammunition = $product->get_categories();
if (strstr($category_ammunition, 'Ammunition')) { // Add Your Category Here 'Ammuntion'
$html = sprintf( '<a id="read-more-btn" rel="nofollow" href="%s" data-product_id="%s" class="button vp-btn">%s</a>',
esc_url( get_the_permalink() ),
esc_attr( $product->get_id() ),
esc_html( __( 'Read More', 'woocommerce' ) )
);
$category_ammunition = $product->get_categories();
}
return $html;
}
I would like to Removal/disable/hide of "bid" button from product pages in WooCommerce for post authors of the post.
I am using WC vendors pro + Woocommerce + Wp Geine Auctions + WC Vendors Auction.
Please find the link of the screen shot below:
The Live Link to the product
How can I do it please?
As this buttons are already customized by you or some plugins, I am not sure at 100% that it will work for you, even if it works on my test server.
The first function is a conditional function that detects for a product if the current user is the author (the vendor) of this product.
Then on shop and archives pages the add to cart button is replace by a custom button liked to the product.
To finish on single product page the button is replace by a fake button with a custom text (here "Not Allowed")…
Here is the code:
// Custom conditional function (detecting the vendor of a product)
if( ! function_exists( 'is_the_vendor' ) ){
function is_the_vendor( $product ){
$current_user_id = get_current_user_id();
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
// Get the product post object to get the post author
$post_obj = get_post( $product_id );
$post_author = $post_obj->post_author;
if( $post_author == $current_user_id ) return true;
else return false;
}
}
// Shop and archives pages: we replace the button add to cart by a link to the product
add_filter( 'woocommerce_loop_add_to_cart_link', 'custom_text_replace_button', 10, 2 );
function custom_text_replace_button( $button, $product ) {
if( is_the_vendor( $product ) ){
$button_text = __("View product", "woocommerce");
return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
} else {
return $button;
}
}
// replacing add to cart button and quantities by a custom inactive button
add_action( 'woocommerce_single_product_summary', 'replacing_template_single_add_to_cart', 1, 0 );
function replacing_template_single_add_to_cart() {
global $product;
if( is_the_vendor( $product ) ):
// Removing add to cart button and quantities
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
// The text replacement
add_action( 'woocommerce_single_product_summary', function(){
// set below your custom text
$text = __('Not allowed', 'woocommerce');
// Temporary style CSS
$style_css = 'style="border: solid 1px red; padding: 0 6px; text-align: center;"';
// Output your custom text
echo '<a class="button custom-button" style="background-color: grey !important;">'.$text.'</a>';
}, 30 );
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works. you will get this:
And this: