PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 9, 2022

[FIXED] How to add custom button to WooCommerce product archives

 May 09, 2022     hook-woocommerce, php, product, woocommerce, wordpress     No comments   

Issue

I would like to add a custom button ("SEE ALL") on product archive. I have already this custom button displayed on single product pages.

What I want is to remove it from single product pages and add it to WooCommerce product archives instead.

Edited: There is another issue with this code. when i remove the link from the backend and make the field empty and then click update button. The link remains there. means if i replace the link with another it works fine but the field does not get empty once filled with something. if i save the product with the empty field the previous value remains there after page load.

Here is the related code:

// This function gets the value for the the custom fields from the database and adds it to the frontend output function
function wpse_add_custom_link_output() {
    $external_link = get_post_meta(get_the_ID(), '_custom_product_text_field', true);
    $html = '<a href="'.$external_link.'" class="custom-button-class" target="_blank" title="'.__('External product link','woocommerce').'">SEE ALL</a>';
    echo $html;
};
add_action( 'woocommerce_after_add_to_cart_button', 'wpse_add_custom_link_output', 10, 0 ); 
// This function creates the field in the backend
function wpse_add_custom_link_field(){
    global $woocommerce, $post;
    echo '<div class="product_custom_field">';
    // Custom Product Text Field
    woocommerce_wp_text_input(
        array(
            'id' => '_custom_product_text_field',
            'placeholder' => __('Paste product link here', 'woocommerce'),
            'label' => __('Custom product link', 'woocommerce'),
            'desc_tip' => 'true'
        )
    );
    echo '</div>';
}
add_action('woocommerce_product_options_general_product_data', 'wpse_add_custom_link_field');
// this function saves the link/text field
function wpse_save_custom_link_field($post_id){
    // Custom Product Text Field
    $woocommerce_custom_product_text_field = $_POST['_custom_product_text_field'];
    if (!empty($woocommerce_custom_product_text_field))
    update_post_meta($post_id, '_custom_product_text_field', 
    esc_attr($woocommerce_custom_product_text_field));
}
add_action('woocommerce_process_product_meta', 'wpse_save_custom_link_field');

Solution

To have this custom button on loop archives instead of single product pages, in your code simply replace the line:

add_action( 'woocommerce_after_add_to_cart_button', 'wpse_add_custom_link_output', 10, 0 ); 

the following code line:

add_action( 'woocommerce_after_shop_loop_item_title', 'wpse_add_custom_link_output', 20 ); 

It's working. I Tested it



Answered By - Fazil Mahesania
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing