Product additionnal buttons in Woocommerce - php

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.

Related

WooCommerce remove shopping cart by user role

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

Add a new step between cart and checkout in Woocommerce

Is there any way to add a new step between cart and checkout in WooCommerce?
I wanted to add a new step called (mode) between cart and checkout on woocommerce
Edit:
I have 4 steps, when I click on the button to pass into the 2 step then it doesn't, it just redirect me into a new page (custom page)
You need first to add a new page in WordPress for your custom Step. Then the code below will replace the button name and link in cart page. You will have to set in this code the desired button name and link to your custom page on both buttons (cart page and Minicart widget too)
// For cart page: replacing proceed to checkout button
add_action( 'woocommerce_proceed_to_checkout', 'change_proceed_to_checkout', 1 );
function change_proceed_to_checkout() {
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
add_action( 'woocommerce_proceed_to_checkout', 'custom_button_proceed_to_custom_page', 20 );
}
// For mini Cart widget: Replace checkout button
add_action( 'woocommerce_widget_shopping_cart_buttons', 'change_widget_shopping_cart_button_view_cart', 1 );
function change_widget_shopping_cart_button_view_cart() {
remove_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_proceed_to_checkout', 20 );
add_action( 'woocommerce_widget_shopping_cart_buttons', 'custom_button_to_custom_page', 20 );
}
// Cart page: Displays the replacement custom button linked to your custom page
function custom_button_proceed_to_custom_page() {
$button_name = esc_html__( 'Custom page step', 'woocommerce' ); // <== button Name
$button_link = get_permalink( 168 ); // <== Set here the page ID or use home_url() function
?>
<a href="<?php echo $button_link;?>" class="checkout-button button alt wc-forward">
<?php echo $button_name; ?>
</a>
<?php
}
// Mini cart: Displays the replacement custom button linked to your custom page
function custom_button_to_custom_page() {
$button_name = esc_html__( 'Custom page', 'woocommerce' ); // <== button Name
$button_link = get_permalink( 168 ); // <== Set here the page ID or use home_url() function
?>
<a href="<?php echo $button_link;?>" class="checkout button wc-forward">
<?php echo $button_name; ?>
</a>
<?php
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Add a custom button for a specific product category in Woocommerce

I want to display an additional button below the description to a specific product category: "bracelets"
so I developed a piece of code, which does not work:
add_action( 'woocommerce_single_product_summary', 'my_extra_button_on_product_page', 30 );
function my_extra_button_on_product_page() {
if ( is_product_category('bracelets')) {
global $product;
echo 'Extra Button';
}
}
Any idea about what's wrong here?
Your question is not really clear.
1) If you want to display a custom button for a specific product category on product category archives pages below the description of this product category you will use:
add_action( 'woocommerce_archive_description', 'extra_button_on_product_category_archives', 20 );
function extra_button_on_product_category_archives() {
if ( is_product_category('bracelets') ) {
echo '<a class="button" href="www.test.com">Extra Button</a>';
}
}
2) If you want to display a custom button in single product pages for a specific product category below the short description of this product you will use:
add_action( 'woocommerce_single_product_summary', 'extra_button_on_product_page', 22 );
function extra_button_on_product_page() {
global $post, $product;
if ( has_term( 'bracelets', 'product_cat' ) ) {
echo '<a class="button" href="www.test.com">Extra Button</a>';
}
}
3) If you want to display a custom button in single product pages for a specific product category below the description (in the product tab) of this product you will use:
add_filter( 'the_content', 'add_button_to_product_content', 20, 1 );
function add_button_to_product_content( $content ) {
global $post;
if ( is_product() && has_term( 'bracelets', 'product_cat' ) )
$content .= '<a class="button" href="www.test.com">Extra Button</a>';
// Returns the content.
return $content;
}
4) If you want to display a custom button in single product pages for a specific product category below the product tabs, you will use:
add_action( 'woocommerce_after_single_product_summary', 'extra_button_on_product_page', 12 );
function extra_button_on_product_page() {
global $post, $product;
if ( has_term( 'bracelets', 'product_cat' ) ) {
echo '<a class="button" href="www.test.com">Extra Button</a>';
}
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works.
For product category archives pages use is_product_category().
For all other cases has_term().
add_action( 'woocommerce_after_single_product_summary', 'my_extra_button_on_product_page', 30 );
the following code adds the button before the loop on a product category archive
add_action( 'woocommerce_archive_description', 'extra_button_on_product_category_archives', 20 );
function extra_button_on_product_category_archives() {
if ( is_product_category('bracelets') ) {
echo '<a class="button" href="www.test.com">Extra Button</a>';
}
}

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.

woocommerce re-add "add to cart" button for single post upsell item

I need help!
I removed the "add to cart" button from my main store page because "see items" makes more sense in this case.
here is the code I added to my child functions.php theme:
`
function remove_loop_button(){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}
add_action('init','remove_loop_button');
add_action('woocommerce_after_shop_loop_item','replace_add_to_cart');
function replace_add_to_cart() {
global $product;
$link = $product->get_permalink();
$text = _( 'See Items');
echo 'See Items';
}
`
It swaps out the button correctly on the main shop page but in the single post, the upsell also shows "see items" when I want it to say "add to cart" ... I have tried everything.
UPDATE: solved i think? it works, anyway :-)
// add add to cart back to upsell
' remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_upsells', 15 );
if ( ! function_exists( 'woocommerce_output_upsells' ) ) {
if ( ! function_exists( 'woocommerce_output_upsells' ) ) {
function woocommerce_output_upsells() {
remove_action('woocommerce_after_shop_loop_item', 'replace_add_to_cart');
add_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
}
}
} '

Categories