WooCommerce remove shopping cart by user role - php

In Woocommerce, I have a function that replace add to cart button by a linked button to the product in shop and archive pages:
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( ! current_user_can('customer') ) {
$link = get_permalink($product_id);
$button_text = __( "View product", "woocommerce" );
$html = ''.$button_text.'';
}
return $html;
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'conditionally_change_loop_add_to_cart_link', 10, 2 );
I would like to remove the add to cart button on all pages if a user is not logged in as a customer.
Can anyone help please?

Instead of your actual code, try the following that will do everything everywhere and will remove add to cart button when user is not logged in:
add_filter('woocommerce_is_purchasable', 'woocommerce_is_purchasable_filter_callback', 10, 2 );
function woocommerce_is_purchasable_filter_callback( $purchasable, $product ) {
if ( ! is_user_logged_in() )
$purchasable = false;
return $purchasable;
}
Code goes in function.php file of your active child theme (or active theme).

Related

How to Hide/show Sections made by elementor using class or id after purchase?

I want to hide section and show other after customer purchase state completed !
i have woocommerce php code do that on add to cart button
can we chang it to make it do what i want ?
add_filter( 'woocommerce_is_purchasable', 'bbloomer_hide_add_cart_if_already_purchased', 9999, 2 );
function bbloomer_hide_add_cart_if_already_purchased( $is_purchasable, $product ) {
if ( wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) {
$is_purchasable = false;
}
return $is_purchasable;
}

Change WooCommerce Order pay page title

Attempting to change the title of the "pay for order" page / "customer payment page"
https://url.com/checkout/order-pay/753/?pay_for_order=true&key=wc_order_xxxxxx
Below is not currently working, it is modified from changing the title on the thank you page.
add_filter( 'the_title', 'woo_title_pay_order', 10, 2 );
function woo_title_pay_order( $title, $id ) {
if ( function_exists( 'is_pay_for_order_page' ) &&
is_pay_for_order_page() && get_the_ID() === $id ) {
$title = "Checkout";
}
return $title;
}
Updated
You can use the following to change "Pay for order" page title:
add_filter( 'the_title', 'change_pay_for_order_title' );
function change_pay_for_order_title( $title ) {
if ( is_wc_endpoint_url( 'order-pay' ) ) {
return __('Checkout', 'woocommerce');
}
return $title;
}
Or also the following code based on 'order-pay' endpoint (but changes the breadcrumb):
add_filter( 'woocommerce_endpoint_order-pay_title', 'change_checkout_order_pay_title' );
function change_checkout_order_pay_title( $title ) {
return __( "Checkout", "woocommerce" );
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Related: Set My Account custom items endpoints titles in WooCommerce

Product additionnal buttons in Woocommerce

In Woocommerce, how do I add a "Continue" button inked to the the product page just after?
And inside the product page how to add a checkout button just under the add to cart button?
Any help is appreciated.
The following code will:
In woocommerce archive pages: Add an additional button "Continue" linked to the product (for simple products)
In single product pages: Add an additional button linked to checkout page.
The code:
// Archives pages: Additional button linked to the product
add_action( 'woocommerce_after_shop_loop_item', 'loop_continue_button', 15 );
function loop_continue_button(){
global $product;
if( $product->is_type('simple') ){
$link = $product->get_permalink();
$text = __("Continue", "woocommerce");
echo '' . $text . '';
}
}
// Single product pages: Additional button linked to checkout
add_action( 'woocommerce_single_product_summary', 'product_additional_checkout_button', 1 );
function product_additional_checkout_button() {
global $product;
// For variable product types
if( $product->is_type( 'variable' ) ) {
add_action( 'woocommerce_single_product_summary', 'custom_checkout_button', 21 );
}
// For all other product types
else {
add_action( 'woocommerce_single_product_summary', 'custom_checkout_button', 31 );
}
}
function custom_checkout_button() {
$link = wc_get_checkout_url();
$text = __("Proceed to checkout", "woocommerce");
echo '' . $text . '';
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.

WooCommerce Avoid add to cart for non logged user

In my WooCommerce shop, when a customers is not logged in I would like to avoid him adding to cart asking him to either login or register an account…
Is it possible?
Updated - You could use this 2 little snippets of code, that will:
Replace add-to-cart button/link in shop pages and archives pages (for non logged in users).
Avoid non logged in customers adding to cart and displaying a custom message.
Here is the code:
// Replacing add-to-cart button in shop pages and archives pages (forn non logged in users)
add_filter( 'woocommerce_loop_add_to_cart_link', 'conditionally_change_loop_add_to_cart_link', 10, 2 );
function conditionally_change_loop_add_to_cart_link( $html, $product ) {
if ( ! is_user_logged_in() ) {
$link = get_permalink($product_id);
$button_text = __( "View product", "woocommerce" );
$html = ''.$button_text.'';
}
return $html;
}
// Avoid add to cart for non logged user (or not registered)
add_filter( 'woocommerce_add_to_cart_validation', 'logged_in_customers_validation', 10, 3 );
function logged_in_customers_validation( $passed, $product_id, $quantity) {
if( ! is_user_logged_in() ) {
$passed = false;
// Displaying a custom message
$message = __("You need to be logged in to be able adding to cart…", "woocommerce");
$button_link = get_permalink( get_option('woocommerce_myaccount_page_id') );
$button_text = __("Login or register", "woocommerce");
$message .= ' '.$button_text.'';
wc_add_notice( $message, 'error' );
}
return $passed;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested ad works.

Skip cart page for a few products redirecting to checkout

I would like to bypass cart page and redirect user to checkout page for a few products.
I have created the add to cart link for the product
Product Name
And I have the code below
add_filter( 'woocommerce_add_to_cart_redirect', 'woo_redirect_checkout' );
function woo_redirect_checkout() {
global $woocommerce;
$desire_product = 1461;
//Get product ID
$product_id = (int) apply_filters( 'woocommerce_add_to_cart_product_id', $_POST['add-to-cart'] );
//Check if current product is subscription
if ( $product_id == $desire_product ){
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
exit;
} else {
$cart_url = $woocommerce->cart->get_cart_url();
return $cart_url;
exit;
}
}
from How to skip cart page on woocomerce for certain products only?. But the url redirect me to homepage instead. Just wondering where is the issue,
I have unchecked the add to cart behaviour at woocommerce settings as well.
Thanks in advance.
I chose another approach and a WordPress hook instead of woocommerce. This is based on this answer: WooCommerce - Skip cart page redirecting to checkout page
This is that code:
function skip_cart_page_redirection_to_checkout() {
// desired product id redirection
$product_id = 1461;
$items_ids = array();
// Get all items IDs that are in cart
foreach( WC()->cart->get_cart() as $item ) {
$items_ids[] = $item['product_id'];
}
// If is cart page and the desired peoduct is in cart => redirect to checkout.
if( is_cart() && in_array($product_id, $items_ids) )
// WooCommerce 3.0 compatibility added
if ( version_compare( WC_VERSION, '2.7', '<' ) ) {
wp_redirect( WC()->cart->get_checkout_url() ); // Older than 3.0
} else {
wp_redirect( wc_get_checkout_url() ); // 3.0+ (Thanks to helgatheviking)
}
}
add_action('template_redirect', 'skip_cart_page_redirection_to_checkout');
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The code is tested and fully functional.

Categories