Replace add to cart button with a read more linked to product page on shop pages in WooCommerce 3
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:
Replace add to cart button based on WooCommerce product weight
I have a solution for replacing add to cart button for particular WooCommerce products(based on weight). so, you can use the below code and put it in the functions.php
// 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() {
global $product;
$weight = $product->get_weight();
preg_replace('/\D/', '', $weight);
if ( $product->has_weight() && ($weight > '9') ) {
return __( 'Custom text', 'text-domain' );
}
return __( 'Add to Cart', 'text-domain' );
}
// To change add to cart text on product 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() {
global $product;
$weight = $product->get_weight();
preg_replace('/\D/', '', $weight);
if ( $product->has_weight() && ($weight > '9')) {
return __( 'Custom text', 'text-domain' );
}
return __( 'Add to Cart', 'text-domain' );
}
Changing Woocommerce Add to cart button to view product button
Try this alternative that will replace add to cart button by a linked button to the product in Shop and archives pages
// Replace add to cart button by a linked button to the product in Shop and archives pages
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 ) {
// Not needed for variable products
if( $product->is_type( 'variable' ) ) return $button;
// Button text here
$button_text = __( "View product", "woocommerce" );
return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
Code goes in function.php file of your active child theme (or theme). Tested and works.
Woocommerce product page redirect breaks add to cart button
Solution was to use the wp_safe_redirect()
function of wordpress instead of a permanent 303 redirect (with WP Plugin called Redirects). To do this i build the new url by getting the product url and building it again without the "product" directory and use it as parameter in the wp function wp_safe_redirect($newUrl)
Add Extra button under Add to cart on shop page of Woocommerce 3
The following code will add a custom button under default exiting button in Woocommerce archives as shop (I have increased the hook priority as other plugins seems to make trouble):
add_action( 'woocommerce_after_shop_loop_item', 'add_loop_custom_button', 1000 );
function add_loop_custom_button() {
global $product;
$product_link = $product->get_permalink(); // Link to the product (if needed)
// Define your button link
$custom_link = home_url( "/something/" ) ;
// Output
echo '<div class="product_meta wcdp-preview-btn-div">
<a class="button thickbox" href="' . esc_url( $custom_link ) .'">' . __( "Citește fragment" ) . '</a>
</div>';
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
Addition - Usage with Woo Document Preview plugin:
To get the preview document link for the button, you will use the following…
But this plugin seems to enable a special Javascript on single product pages only, that is not active on shop and archives pages, so the link doest open a preview lightbox, but opens the preview in Google docs instead.
add_action( 'woocommerce_after_shop_loop_item', 'add_loop_custom_button', 1000 );
function add_loop_custom_button() {
global $product;
// The PDF doc preview button link
if( $pdf_doc = $product->get_meta('wcdp_preview_attachment') ){
$preview_link = "https://docs.google.com/viewer?url=" . urlencode($pdf_doc['url']);
$preview_link .= "&embedded=true&TB_iframe=true&width=600&height=550";
// Output
echo '<div class="product_meta wcdp-preview-btn-div">
<a class="button alt thickbox wcdp-preview-btn" href="' . esc_url( $preview_link ) .'">' . $pdf_doc['name'] . '</a>
</div>';
}
}
Code goes in function.php file of your active child theme (or active theme). The link works.
Customize Add to Cart button on specific WooCommerce product categories
You can use has_term()
conditional function to target a product category, this way:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
if ( has_term( 'Classes', 'product_cat', $product->get_id() ) ) {
$button_text = __("Read more", "woocommerce");
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
return $button;
}
Code goes in functions.php file of your active child theme (or active theme) or also in any plugin file.
Related Topics
Stream_Socket_Client Unable to Connect (Connection Timed Out)
Sending Data Along with a Redirect in Codeigniter
How to Specify the Parent Query Field from Within a Subquery in MySQL
Verify Password Hash in Nodejs Which Was Generated in PHP
Simulate File Structure with PHP
How to Get All Checkbox Variables Even If Not Checked from HTML to PHP
Reading from Comma or Tab Delimited Text File
Doctrine2 Association Mapping with Conditions
How to Specify Multiple Return Types on PHP 7
Next Business Day of Given Date in PHP
How to Check If a MySQL Query Using the Legacy API Was Successful
Preg_Replace with Array Replacements