Hi guys im trying to add custom button next to "Add to Cart" button. I added it but i want to change button link for every product how can i do that here is my code: ( i took from another question, it works )
add_action('woocommerce_after_shop_loop_item', 'add_a_custom_button', 5 );
function add_a_custom_button() {
global $product;
// Not for variable and grouped products that doesn't have an "add to cart" button
if( $product->is_type('variable') || $product->is_type('grouped') ) return;
// Output the custom button linked to the product
echo '<div style="margin-bottom:10px;">
<a class="button custom-button" href="' . esc_attr( $product->get_permalink() ) . '">' . __('View product') . '</a>
</div>';
}
Another way:
Create a new Wordpress custom field for the pages you want to use a custom url link on:
Custom field name: nothanks_link_redirect
Custom field value: https://yourlink.com
Tweak as you need and put this in your child theme's functions.php :
/** WooCommerce custom field - 'No Thanks' Button **/
function nothanks_redirect_button() {
global $post;
$product_id = $post->ID;
$NoThanksLinkRedirectValue = get_post_meta($product_id,'nothanks_link_redirect',true);
if(!$NoThanksLinkRedirectValue) return;
echo '<a class="nothanks-button" style="margin-left: 20px" href="'.$NoThanksLinkRedirectValue.'" target="_self">No Thanks</a>';
}
add_action('woocommerce_after_add_to_cart_button','nothanks_redirect_button');
Enjoy!
Related
I have used following shortcodes in the Home page to display set of products :-
[products limit="15" orderby="menu_order" columns="5" category="shirt, shoe"]
However when any product image is clicked on the Home page, the specific link is opened on the same tab. I would like to open that link in a separate tab. How to achieve this?
Thanks!
Note: I am using Oceanwp as my active theme
Funny, i just had the same issue few days ago. Here's how i've solved it:
if ( ! function_exists( 'woocommerce_template_loop_product_link_open' ) ) {
/**
* Insert the opening anchor tag for products in the loop.
*/
function woocommerce_template_loop_product_link_open() {
global $product;
$link = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );
echo '<a href="' . esc_url($link) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link" target="_blank">';
}
}
Tested and works. You can just paste this into your functions.php or if you've made a custom plugin just include this at the very beginning.
You might need to add "is_home()" or "is_front_page()" to make it hit just the home page and not the whole site
Since I am using Oceanwp theme, I have to modify in different places than what is suggested by #Diego. But his answer gives me the clue for finding the solution.
This is the modification I did to make it work for Oceanwp.
if ( ! function_exists( 'ocean_woo_img_link_open' ) ) {
function ocean_woo_img_link_open() {
global $product;
$woo_img_link = get_the_permalink( $product->get_id() );
echo '<a href="' . esc_url( $woo_img_link ) . '" class="woocommerce-LoopProduct-link" target="_blank" >';
}
}
I have this piece of code which I use for my checkout/order button. Which is pretty convenient, I can style the button and add custom text and classes. I know this won't work with translated content but that is of no importance here because the website is and will stay in only 1 language.
// Filter for adding extra custom line to order button
add_filter('woocommerce_order_button_html', 'mbm_custom_button_html');
function mbm_custom_button_html($button_html)
{
$button_html = '<button type="submit" class="button alt" name="woocommerce_checkout_place_order" id="place_order">Lidmaatschap starten<br /><span class="extra-text-checkout-button">Betaal pas na gratis proefperiode</span></button>';
return $button_html;
}
I was wondering can I also use the same method for the add to cart button? But then something like add_filter('woocommerce_add_to_cart_button_html', 'mbm_custom_atc_button_html');
I tried to search it in the docs but could not find my answer.
For "Add to Cart" part, you may try this hooks: woocommerce_loop_add_to_cart_link and woocommerce_product_single_add_to_cart_text.
I believe, the first one provides a better solution for you. You may check the detailed usage via: https://stackoverflow.com/a/56179393/11003615 and http://hookr.io/filters/woocommerce_loop_add_to_cart_link/
Hope those helps. Best regards.
for loop page
add_filter('woocommerce_product_add_to_cart_text', 'custom_woocommerce_product_add_to_cart_text', 100);
for product single page
add_filter('woocommerce_product_single_add_to_cart_text', 'custom_woocommerce_product_add_to_cart_text', 100);
function custom_woocommerce_product_add_to_cart_text(){
return 'Add to catd 111';
}
There does not exists filter to change the 'ADD TO CART' button HTML.
If you need to do changes in HTML of 'ADD TO CART' button, you need to override the templates from plugin to your theme.
For example, for simple product 'ADD TO CART' button HTML, you need to override /plugins/woocommerce/templates/single-product/add-to-cart/simple.php to /theme/woocommerce/single-product/add-to-cart/simple.php and do changes in simple.php which is in a theme folder.
Add below code in your active theme's function.php
add_filter( 'woocommerce_order_button_html', 'ro_custom_cart_button_html' );
function ro_custom_cart_button_html( $button_html ) {
$order_button_text = 'Submit';
//add your html below where there is button tag
$button_html = '<button type="submit" class="button alt"
name="woocommerce_checkout_place_order" id="place_order" value="' . esc_attr(
$order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '">' .
esc_html( $order_button_text ) . '</button>';
$button_html = str_replace( 'Place order', 'Submit', $button_html );
return $button_html;
}
Tested and works well
I'd like to add a button next to "Add to Cart" on the product page that adds "-sample" to the product URL when clicked.
Example:
You're viewing Product 1's page and the URL is "http://www.example.com/shop/product-1/"
When you click on the button, it adds "-sample" to the URL
"http://www.example.com/shop/product-1-sample/"
How can I achieve this?
Thanks
For woocommerce 3+ (only):
In woocommerce 3 you will use woocommerce_after_shop_loop_item action hook instead, as the hook woocommerce_after_add_to_cart_button will not work anymore.
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0 );
function add_custom_button() {
global $product;
$product_link = $product->get_permalink();
$sample_link = substr($product_link, 0, -1) . '-sample/';
echo '<a class="button alt btn-sample" href="' . esc_url( $sample_link ) .'">' . __( "Get a sample", "my_theme_slug" ) . '</a>';
}
Code goes on function.php file of your active child theme (or active theme). Tested and works.
Before woocommerce 3:
This is possible using hook woocommerce_after_add_to_cart_button to add your additional button on product pages, using this custom function:
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0 );
function add_custom_button() {
global $product;
$product_link = get_permalink( get_the_id() );
$sample_link = substr($product_link, 0, -1) . '-sample/';
echo '<a class="button alt btn-sample" href="' . esc_url( $sample_link ) .'">' . __( "Get a sample", "my_theme_slug" ) . '</a>';
}
This code goes on function.php file of your active child theme or theme.
This code is tested and fully functional.
Based on this: Add a button after add to cart and redirect it to some custom link in WooCommerce
And this: PHP - How to remove all specific characters at the end of a string?
It's been a long time since the original question, but here's a recent update that works for me, WooCommerce 6.3.1:
/* WooCommerce customization */
add_action( 'woocommerce_after_shop_loop_item', 'custom_select_link', 11 );
function custom_select_link() {
global $product;
// Custom "Select" button.
echo '<a class="custom-button" href="' . esc_url( get_permalink( $product->id ) ) . '"><button class="custom-button"> </button></a>';
}
This answer cites an answer in wordpress.stackexchange.
I want to disallow place order if cart item is more than two on click of proceed to checkout or Place Order button click in woocommerce.
I do not want to check it on add to cart validation check, please any one can guide me about it?
You can use WooCommerce Min/Max Quantities extension to set a minimum and maximum quantity required to checkout.
or
You can set custom code to your functions.php theme file by using "woocommerce_after_checkout_validation" filter and can use $posted array to check value and set validation,
add_action('woocommerce_after_checkout_validation', 'rei_after_checkout_validation');
function rei_after_checkout_validation( $posted ) {
// do all your logics here...
}
function custom_checkout_button_action(){
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$total = 0;
foreach($items as $item => $values) {
$total = $values['quantity'];
}
if($total>=2){
?>
<?php _e( 'Check On Out', 'woocommerce' ); ?>
<?php
}
else{
?>
<?php _e( 'Check On Out', 'woocommerce' ); ?>
<?php
}
}
add_action('woocommerce_proceed_to_checkout', 'custom_checkout_button_action');
If you are using butto you can change your HTML accordingly to disable button instead of link in above code on check out page before place an order page.
I Need to make a event list in wordpress, show up by: "today" - "week" - "month" and all this show just my post. Witch is the best way to do that?
The author must just add the date and time of event and its appear in the current category us well.
If you just want to add the possibility for the author to add the date of an event, use the add_meta_boxes_{$post_slug} action.
This action allows to configure and print a new metabox in any post (choosen) post screen.
add_action( 'add_meta_boxes_post', 'adding_custom_meta_boxes');
add_action('save_post', 'save_sticked_metabox');
function adding_custom_meta_boxes(){
add_meta_box(
'sticked-list',
__( 'Featured list', $this->plugin_text_domain ),
'sticked_list_Render',
'post',
'side',
'default'
);
}
function sticked_list_Render(){
global $post;
$sticked = get_post_meta($post->ID, 'sticked', true);
$checked = '';
if($sticked != '' && isset($sticked)){
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="sticked" value="true" <?php echo $checked;?> > <?php _e('Check to set as pre-defined list', $this->plugin_text_domain);?>
<?
}
function save_sticked_metabox($post_id, $post){
if(isset($_POST['sticked'])){
update_post_meta($post_id, 'sticked', 'true');
}
else{
delete_post_meta($post_id, 'sticked');
}
}
This is just an example to show how to print a new metabox in the edit-post admin screen. You'll need to change the "sticked_list_render" to print a date & time picker.