• Resolved GrayHunter

    (@grayhunter)


    Hello,
    I use the Divi theme with WooCommerce. And I am looking for ways to add two buttons to the shop/Woo Products module (on an archive/catalog page): “Add to cart” and “Details”.
    I need a help of a programmer. I will be grateful if anyone can help me out.

    P.S.:
    I found the way how to add “Add to cart” button via functions.php:
    add_action( ‘woocommerce_after_shop_loop_item’, ‘woocommerce_template_loop_add_to_cart’, 20 );
    But now I need help with the “Details” button (which should direct to the product page). Please help.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator threadi

    (@threadi)

    I would recommend asking this question in the WooCommerce support forum: https://wordpress.org/support/plugin/woocommerce/

    Hi @grayhunter

    Could you use these codes to your child theme’s functions.php file or to this Code Snippets plugiin to add a View Product button to your shop poges?

    /**
    * Add ‘View Product’ button in WooCommerce
    
    */
    add_action('woocommerce_after_shop_loop_item', 'wc_custom_button_view_product', 10 );
    function wc_custom_button_view_product() {
        global $product;
    
        // Ignore for Variable and Group products
        if( $product->is_type('variable') || $product->is_type('grouped') ) return;
    
        // Display the custom button
        echo '<a style="margin-left:5px" class="button wc-custom-button-view-product" href="' . esc_attr( $product->get_permalink() ) . '">' . __('View product') . '</a>';
    }
    

    I would recommend using the Code Snippets plugin if you are not much familiar with PHP since adding the wrong codes to the child theme’s functions.php file may make the site offline.

    And it’s best to ask WooCommerce related questions to the WooCommerce plugin support page: https://wordpress.org/support/plugin/woocommerce/

    Thanks

    Thread Starter GrayHunter

    (@grayhunter)

    Thank you so much!
    For others – here is some more useful info about the solution.

    The last question, please: What order should I use for the “Add to cart” and “View product” buttons?
    In your sample it is 10 (‘wc_custom_button_view_product’, 10) for the “View product” button.

    There are some plugins which have a position setting (“Above add to cart button” or “Under add to cart button”) – but this does not work if I have “View product” set to 10 and “Add to cart” to 15.
    Please advise the correct position settings for the buttons.

    @grayhunter ,

    You can use this final code below to show Add To Cart first and View Product button last.

    // Add "Add to Cart" buttons on Divi shop pages
    add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 5  );
    
    /**
    * Add ‘View Product’ button after ‘Add to Cart’ in WooCommerce
    
    */
    add_action('woocommerce_after_shop_loop_item', 'wc_custom_button_view_product', 20 );
    function wc_custom_button_view_product() {
        global $product;
    
        // Display the custom button
        echo '<a style="margin-left:5px" class="button wc-custom-button-view-product" href="' . esc_attr( $product->get_permalink() ) . '">' . __('View product') . '</a>';
    } 

    And this will be the result: https://prnt.sc/Qnt_VCKkkkkj

    Hope this helps! 🙂

    Thread Starter GrayHunter

    (@grayhunter)

    Thank you!

    You are most welcome @grayhunter 🙂 Take Care 🙂

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘How can I add a custom ‘add to cart’ button to Woo?’ is closed to new replies.