Modify simple product link in WooCommerce - php

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).

Related

How can I change the add to cart button text to an icon in Woocommerce? (Variable Products)

so I have been struggling on this problem.
I wanna change the variable add to cart Button to an Icon. This is what my code does
And what I wanna achieve:
I am using Elementor and the products are in a slider from Ultimate Addons for Elementor, which I customized for my needs. Link of the doc (https://ultimateelementor.com/docs/filters-actions-for-woocommerce-products)
function price_after( $product_id, $settings ) {
global $product;
$sprache = $product->get_attribute('sprache');
if( ! empty( $sprache ) ){
woocommerce_variable_add_to_cart();
//$link['url'] = apply_filters( 'woocommerce_variable_add_to_cart', get_permalink( $product->id ) );
/*$link['label'] = apply_filters( 'variable_add_to_cart_text', __( 'Warenkorb', 'woocommerce' ) );*/
} else {
if ( $product->is_in_stock() ) {
echo '<div class="add_to_cart_wishlist">';
echo "<button class='button product_type_simple add_to_cart_button ajax_add_to_cart' href='https://[siteurl]/?add-to-cart=".$product_id."'data-product_id='".$product_id."'>". file_get_contents('[siteurl]/wp-content/uploads/2021/06/cart-1-1-1.svg')."</button>";
echo do_shortcode("[ti_wishlists_addtowishlist]");
echo "</div>";
} }} add_action('uael_woo_products_price_after', 'price_after', 10, 2);
And this is what I use to change the text of the add to cart button
function filter_woocommerce_product_add_to_cart_text( $add_to_cart_text, $product ) {
$add_to_cart_text = 'Test';
return $add_to_cart_text;
}
add_filter( 'woocommerce_product_add_to_cart_text', 'filter_woocommerce_product_add_to_cart_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'filter_woocommerce_product_add_to_cart_text', 10, 2 );
I even deleted the esc_html in the template folder of woocommerce...
Now I dont know to do else, I need your help!!!
Thank you in advance!

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.

Change add to cart button and text based on WooCommerce product type

How to change a WooCommerce add to cart button in Product List loop but depending on the product type, like for example:
For products with Variations I want a text in add to cart button to: "Show product"
For Simple prodcuts "Show product"
For products Out of stock: "Unavailable"
I tried with below code but doesn't work:
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 ) {
$button_text = __( "Out of stock", "woocommerce" );
return '<a class="view-product" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
if( ! $product->managing_stock() && ! $product->is_in_stock() ) {
return $button;
}
if( $product->is_type( 'variable' ) ) return $button;
}
Try the following instead:
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 ) {
// Out of stock products
if( ! $product->is_in_stock() ) {
$button_text = __( "Unavailable", "woocommerce" );
}
// Simple and Variable products
elseif( $product->is_type( 'simple' ) || $product->is_type( 'variable' ) ) {
$button_text = __( "Show product", "woocommerce" );
}
// Other product types
else {
$button_text = add_to_cart_text();
}
return '<a class="view-product button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
Code goes in functions.php file of the active child theme (or active theme). It should work
To change woocommerce Book Now button I installed this plugin https://wordpress.org/plugins/button-customizer-for-woocommerce/ and added 2 filters to functions.php and finally, it works
add_filter( 'woocommerce_booking_single_check_availability_text', 'wooninja_booking_check_availability_text' );
function wooninja_booking_check_availability_text() {
return "your text";
}
add_filter( 'woocommerce_booking_single_add_to_cart_text', 'wooninja_woocommerce_booking_single_add_to_cart_text' );
function wooninja_woocommerce_booking_single_add_to_cart_text() {
return "your text";
}

Woocommerce Continue Shopping Button Link to Last Product Category Page

There are forum topics regarding custom continue shopping buttons, but there is nothing on how to link the custom button to the last product category page viewed. There is a plugin that gives the option for this; however, there is a flaw in that when the cart is refreshed, the button disappears. The following code works to create a custom message/button; however, I cannot figure out how to change the link from the shop page to the last product category page viewed:
/* Add Continue Shopping Button on Cart Page & Checkout page*/
add_action( 'woocommerce_before_cart_table',
'woo_add_continue_shopping_button_to_cart' );
add_action( 'woocommerce_before_checkout_form',
'woo_add_continue_shopping_button_to_cart' );
function woo_add_continue_shopping_button_to_cart() {
$shop_page_url = "get_permalink( wc_get_page_id( 'shop' ) )";
echo '<div class="woocommerce-message">';
echo ' <a href="'.$shop_page_url.'" class="button wc-forwards">Continue
Shopping →</a> Would you like some more prints?';
echo '</div>';
}
Thank you in advance!
J.
Here is the code that the plugin uses to link (I think):
add_action( 'woocommerce_cart_is_empty', 'hpy_cs_output_notice', 1 );
function hpy_cs_output_notice() {
$display_empty = get_option( 'hpy_cs_empty_cart_notice' );
if ( $display_empty == 'yes' ) {
$link = wc_custom_redirect_continue_shopping();
$message = sprintf('%s %s',
esc_url($link), esc_html__('Continue shopping', 'woocommerce'),
esc_html(get_option('hpy_cs_empty_cart_text', __('Your cart is empty.', '
$message = sprintf( \'%s %s\',
esc_url( $link ), esc_html__( \'Continue shopping\', \'woocommerce\' ),
esc_html( get_option( \'hpy_cs_empty_cart_text\', __( \'Your cart is
empty.\', \'hpy_cshpy_cshpy_cs\' ) ) ) );
'))));
wc_print_notice($message);
}
}
There is no built-in function to retrieve last category page viewed. But, I have some ideas on how this could be solved.
First is to create a custom action, check if page is category page, and set category ID to session data if so. Then on your button, use this to get proper URL.
/* Set up session */
add_action( 'init', 'wp_check_for_product_cat_sess');
function wp_check_for_product_cat_sess(){
if(!session_id()) {
session_start();
}
}
/* Set session variable when on Category or Product page */
add_action( 'wp', 'wp_check_for_product_cat');
function wp_check_for_product_cat(){
global $wp_query;
if( is_product_category() ){ // This is Category; use my ID
$_SESSION['wp_last_cat'] = $wp_query->get_queried_object()->term_id;
}
if( is_product() ){ // This is Product; use my ID to get my Categories
$cats = get_the_terms( $wp_query->get_queried_object(), 'product_cat' ) ;
if( count( $cats ) > 0 ){
foreach($cats as $one_cat ){
$_SESSION['wp_last_cat'] = $one_cat->term_id;
}
}
}
if( is_cart() ){
// Here we output only on Cart page, as debug tool */
var_dump( $_SESSION['wp_last_cat'] );
}
}
Then you replace in your action code:
$shop_page_url = "get_permalink( wc_get_page_id( 'shop' ) )";
Should become:
if( isset( $_SESSION['wp_last_cat'] ) ){
$shop_page_url = get_term_link( $_SESSION['wp_last_cat'], 'product_cat' );
} else {
$shop_page_url = get_permalink( wc_get_page_id( 'shop' ) );
}
Second is to retrieve last item added to cart, and use that item to retrieve its category URL.

Replace add to cart button with a read more linked to product page on shop pages in WooCommerce 3

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 );

Categories