Raul
Raul

Reputation: 21

Modify simple product link in WooCommerce

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?

Upvotes: 1

Views: 779

Answers (1)

Raul
Raul

Reputation: 21

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

Upvotes: 1

Related Questions