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.
Related
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.
I work on site that use External products from Amazon, but want instead pointing users to that external URL, first to add to cart that product. I have this function, that change Default Button text for each product, to Add to cart.
function sv_wc_external_product_button( $button_text, $product ) {
if ( 'external' === $product->get_type() ) {
// enter the default text for external products
return $product->button_text ? $product->button_text : 'Add To Cart';
}
return $button_text;
}
add_filter( 'woocommerce_product_single_add_to_cart_text',
'sv_wc_external_product_button', 10, 2 );
But this function not add product to cart.
How to make this function to Add selected product to cart?
Thanks.
Updated 2020
This is a complete different way with simple products and a custom field external link.
In this answer we will use simple products instead of external products.
We add an "External URL" custom field in product option settings and we save the data.
Add a custom field on general product option settings for simple products only :
add_action( 'woocommerce_product_options_general_product_data', 'simple_product_with_external_url' );
function simple_product_with_external_url() {
global $product_object;
echo '<div class="options_group show_if_simple hidden">';
// External Url
woocommerce_wp_text_input( array(
'id' => '_ext_url_cust',
'label' => 'External Url',
'description' => 'Custom external URL',
'desc_tip' => 'true',
'placeholder' => 'Enter here your custom external URL'
) );
echo '</div>';
}
Save the custom field data if it's a simple product and not empty:
add_action( 'woocommerce_admin_process_product_object', 'save_simple_product_with_external_url' );
function save_simple_product_with_external_url( $product ) {
if( $product->is_type('simple') && isset($_POST['_ext_url_cust']) ) {
$product->update_meta_data( '_ext_url_cust', sanitize_url($_POST['_ext_url_cust']) );
}
}
2) This will not work on shop pages and archives pages, if we don't set in WooCommerce the cart redirection when adding a product to cart.
So we will replace add-to-cart button (just for our simple products with a custom link redirection) on shop pages and archives pages by a linked custom button to single product pages.
Replacing add-to-cart button in shop pages and archives pages (for simple products with custom external url):
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
$external_url = $product->get_meta('_ext_url_cust');
if ( ! empty($external_url) ) {
$html = sprintf( '%s', $product->get_permalink(), __("Read More", "woocommerce") );
}
return $html;
}
3) If the custom field value is not empty, the product is added to cart first and then redirected to the external URL (our custom field value in single product pages)
External URL redirection after adding to cart (when custom field is not empty in simple products):
add_filter( 'woocommerce_add_to_cart_redirect', 'redirect_simple_product_with_external_url' );
function redirect_simple_product_with_external_url( $url ) {
if( isset($_REQUEST['add-to-cart']) && absint( $_REQUEST['add-to-cart'] ) > 0 )
return get_post_meta( absint( $_REQUEST['add-to-cart'] ), '_ext_url_cust', true );
return $url;
}
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 on WooCommerce version 3+
Use https://stackoverflow.com/a/44036965/3730754 instead.
You should try to use woocommerce_product_add_to_cart_url filter hook to change the add-to-cart link (here for grouped products), this way:
add_filter( 'woocommerce_product_add_to_cart_url', 'override_external_product_url', 10, 2 );
function override_external_product_url( $url, $product ){
if ( 'external' === $product->get_type() ) {
//Get product ID -- WooCommerce compatibility
if ( method_exists( $product, 'get_id' ) ) {
$product_id = $product->get_id();
} else {
$product_id = $product->id;
}
// custom add to cart url example
$url = home_url( "/product/?add-to-cart=$product_id");
}
return $url;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Update: But this will not add to cart this external product before redirecting to an external url even if it works displaying the add-to-cart url (as add-to-cart is ajax driven).
I fixed myself. For External products, to replace default "Buy This Product" with other generic text, add this functions into functions.php file into theme:
add_filter( 'woocommerce_product_add_to_cart_text' ,
'wpf_custom_add_cart_text_archive',11);
function wpf_custom_add_cart_text_archive() {
global $product;
$product_type = $product->product_type;
switch ( $product_type ) {
case 'external':
return __( 'Add to Cart', 'woocommerce' );
break;
case 'grouped':
return __( 'View products', 'woocommerce' );
break;
case 'simple':
return __( 'Add to cart', 'woocommerce' );
break;
case 'variable':
return __( 'Select options', 'woocommerce' );
break;
default:
return __( 'Read more', 'woocommerce' );
}
}
add_filter( 'woocommerce_product_single_add_to_cart_text',
'wpf_custom_add_cart_text',11);
and this one:
function wpf_custom_add_cart_text() {
return __( 'Add to Cart', 'woocommerce' );
}
to replace text everywhere.
I'm trying to redirect people who click on the add to cart button to the checkout page directly. The add to cart button only is adding the product to cart but not redirecting at all.
The code I use is only working for product with no variations.
I'm using the latest version of woocommerce 3.0.1.
//REDIRECT TO CHECKOUT
add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout() {
return WC()->cart->get_checkout_url();
}
Try with this:
function add_to_cart_checkout_redirect() {
wp_safe_redirect( get_permalink( get_option( 'woocommerce_checkout_page_id' ) ) );
die();
}
add_action( 'woocommerce_add_to_cart', 'add_to_cart_checkout_redirect', 11 );
How can I change the "Add to cart" button text/link in woocommerce (v2.4)?
I tried to add this code to my functions.php, but it doesn´t seem to work:
add_action('woocommerce_after_shop_loop_item','replace_add_to_cart');
function replace_add_to_cart() {
global $product;
$link = $product->get_permalink();
$text = _( 'Learn More', 'woocommerce' );
echo 'Learn more';
}
It was asked for WC 2.4. If you need it for newer versions you should use
add_filter( 'woocommerce_product_add_to_cart_text',function(){
return __( 'Learn more','your-textdomain' );
} );
Tested with WooCOmmerce 5.6
You have written correct code, but before using woocommerce_after_shop_loop_item hook, you have to remove "add to cart" button using same hook as shown below.
Step 1 - Remove "add to cart" button from shop
function remove_loop_button(){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
add_filter( 'woocommerce_is_purchasable', false );
}
add_action('init','remove_loop_button');
Step 2 -Add new button that links to product page for each product
add_action('woocommerce_after_shop_loop_item','replace_add_to_cart');
function replace_add_to_cart() {
global $product;
$link = $product->get_permalink();
echo do_shortcode('Learn more');
}
To change you add to cart button text paste this code in your theme functions.php
add_filter( 'add_to_cart_text', 'woo_custom_single_add_to_cart_text' ); // < 2.1
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_single_add_to_cart_text' ); // 2.1 +
function woo_custom_single_add_to_cart_text() {
return __( 'Learn More', 'woocommerce' );
}
This line of code needs to be added to the functions.php
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');
This is for removing the product from product listing page.
#Lorenz I think you should go through this https://www.wpblog.com/add-to-cart-button-in-woocommerce-store/
// To change add to cart text on product Shop/archives(Collection) page
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text' );
function woocommerce_custom_product_add_to_cart_text() {
return __( 'Buy Now', 'woocommerce' );
}
// To change add to cart text on single product page
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text' );
function woocommerce_custom_single_add_to_cart_text() {
return __( 'Buy Now', 'woocommerce' );
}
Source: Webtalkhub
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');
}
}
} '