• Resolved Jazz

    (@prgemini)


    Hi everyone! I’m hoping someone can please help me with this.

    I’m trying to add custom social media share buttons under the “add to cart” button in my product pages. The social buttons are currently showing under our” product description”. This is the code that I am using:

    function add_social_share_buttons($content) {
    // Get the current page URL
    $url = esc_url(get_permalink());

    // Get the current page title
    $title = urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8'));

    // Create an array of social networks and their respective sharing URLs
    $social_networks = array(
    'Facebook' => 'https://www.facebook.com/sharer/sharer.php?u=' . $url,
    'Twitter' => 'https://twitter.com/intent/tweet?url=' . $url . '&text=' . $title,
    'LinkedIn' => 'https://www.linkedin.com/shareArticle?url=' . $url . '&title=' . $title,
    'Pinterest' => 'https://pinterest.com/pin/create/button/?url=' . $url . '&description=' . $title,
    );

    // Initialize the share buttons HTML
    $share_buttons = '<div class="social-share-buttons">';

    // Loop through the social networks and generate the share buttons HTML
    foreach ($social_networks as $network => $share_url) {
    $share_buttons .= '<a href="' . $share_url . '" target="_blank" rel="noopener">' . $network . '</a>';
    }

    // Close the share buttons HTML
    $share_buttons .= '</div>';

    // Append the share buttons HTML to the content
    $content .= $share_buttons;

    return $content;
    }

    // Add the social share buttons after the content
    add_filter('the_content', 'add_social_share_buttons');

    Thank you!

    • This topic was modified 6 months, 2 weeks ago by Jazz.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi @prgemini,

    Thank you for reaching out.

    Helping out with custom coding of this nature is outside the scope of support, although I would recommend the following:

    1. Running the exact question you’re asking, along with the code provided, through an AI platform like ChatGPT for recommendations/changes to your code;
    2. Checking whether there are existing plugins in the WordPress plugin repository that might be doing that already.
    3. Joining our WooCommerce Slack community (it does have a developer channel where you can ask coding questions): https://woo.com/community-slack/

    Hope it helps!

    Thank you for your inquiry, @prgemini.

    Upon reviewing your code, I noticed that the issue stems from an incorrect implementation of the WooCommerce hook. Specifically, the hook you’ve used doesn’t properly target the method within the class context, which is why the social media share buttons are not appearing as intended below the “Add to Cart” button on product pages.

    To resolve this, I’ve corrected the hook and ensured that it references the class method appropriately. By doing so, WooCommerce can correctly identify and trigger the function after the “Add to Cart” button is rendered.


    add_action( 'woocommerce_after_add_to_cart_button',array($this,'add_social_share_buttons_after_add_to_cart' ) );

    public function add_social_share_buttons_after_add_to_cart() {
    $url = esc_url(get_permalink());

    $title = urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8'));

    $social_networks = array(
    'Facebook' => 'https://www.facebook.com/sharer/sharer.php?u=' . $url,
    'Twitter' => 'https://twitter.com/intent/tweet?url=' . $url . '&text=' . $title,
    'LinkedIn' => 'https://www.linkedin.com/shareArticle?url=' . $url . '&title=' . $title,
    'Pinterest' => 'https://pinterest.com/pin/create/button/?url=' . $url . '&description=' . $title,
    );

    $share_buttons = '<div class="social-share-buttons">';

    foreach ( $social_networks as $network => $share_url ) {
    $share_buttons .= '<a href="' . $share_url . '" target="_blank" rel="noopener" class="social-share-link ' . strtolower( $network ) . '-share">' . $network . '</a> ';
    }

    $share_buttons .= '</div>';

    echo $share_buttons;
    }



    Thread Starter Jazz

    (@prgemini)

    Hi @proshantamr Thank you for the help. I got this error when applying your code:

    syntax error, unexpected token “public”, expecting end of file

    Thread Starter Jazz

    (@prgemini)

    @ckadenge Wow, I had forgotten about the Chatgpt! Thank you!

    @proshantamr I think this is the correct code. Everything seems to be working fine, for now:

    function get_social_share_buttons() {
    // Get the current page URL
    $url = esc_url(get_permalink());

    // Get the current page title
    $title = urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8'));

    // Create an array of social networks and their respective sharing URLs
    $social_networks = array(
    'Facebook' => 'https://www.facebook.com/sharer/sharer.php?u=' . $url,
    'Twitter' => 'https://twitter.com/intent/tweet?url=' . $url . '&text=' . $title,
    'LinkedIn' => 'https://www.linkedin.com/shareArticle?url=' . $url . '&title=' . $title,
    'Pinterest' => 'https://pinterest.com/pin/create/button/?url=' . $url . '&description=' . $title,
    );

    // Initialize the share buttons HTML
    $share_buttons = '<div class="social-share-buttons">';

    // Loop through the social networks and generate the share buttons HTML
    foreach ($social_networks as $network => $share_url) {
    $share_buttons .= '<a href="' . $share_url . '" target="_blank" rel="noopener">' . $network . '</a> ';
    }

    // Close the share buttons HTML
    $share_buttons .= '</div>';

    return $share_buttons;
    }
    add_action('woocommerce_after_add_to_cart_button', 'display_social_share_buttons');

    function display_social_share_buttons() {
    echo get_social_share_buttons();
    }

    • This reply was modified 6 months, 2 weeks ago by Jazz.

    Hi @prgemini,

    We’re glad to be of help.

    If you’re happy with our assistance, we’d love if you could leave us a review.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Move Social Media Share Buttons’ is closed to new replies.