Sharing a short product link using a shortcut - the fourth technique to increase online sales

The technique of increasing online store income by shortening the product subscription link

Another method to increase the online sales of stores is the use of a quick product sharing button with a short product link. Sharing the short product link on social media improves the appearance and readability of your website link.

view Other methods of increase Online store income


 

You should put this code at the end of your function file and save it


// Display a short product link after the "Add to Cart" button"
function display_shortlink_after_add_to_cart() {
    global $product;
    if ( is_product() ) {

        $shortlink = wp_get_shortlink($product->get_id());


        if ( $shortlink ) {
            echo '<div class="product-shortlink">';
            echo '<p>Link: <input type="text" id="shortlink" value="' . esc_url($shortlink) . '" readonly> <button id="copyShortlinkButton">Copy</button></p>';
            echo '</div>';
        }
    }
}
add_action('woocommerce_after_add_to_cart_button', 'display_shortlink_after_add_to_cart');


function enqueue_custom_scripts() {
    if ( is_product() ) {
        wp_enqueue_script('jquery');
        wp_enqueue_script('custom-ajax', get_template_directory_uri() . '/js/custom-ajax.js', array('jquery'), null, true);
        wp_localize_script('custom-ajax', 'ajax_object', array('ajax_url' => admin_url('admin-ajax.php')));
    }
}
add_action('wp_enqueue_scripts', 'enqueue_custom_scripts');


function get_shortlink_ajax() {
    $product_id = intval($_POST['product_id']);
    $shortlink = wp_get_shortlink($product_id);
    echo esc_url($shortlink);
    wp_die();
}
add_action('wp_ajax_get_shortlink', 'get_shortlink_ajax');
add_action('wp_ajax_nopriv_get_shortlink', 'get_shortlink_ajax');

function add_copy_shortlink_script() {
    if ( is_product() ) {
        ?>
        <script type="text/javascript">
            function copyShortlink() {
                var copyText = document.getElementById("shortlink");
                copyText.select();
                copyText.setSelectionRange(0, 99999); // For mobile devices
                document.execCommand("copy");
                alert("copied: " + copyText.value);
            }

            document.addEventListener("DOMContentLoaded", function() {
                document.getElementById("copyShortlinkButton").addEventListener("click", copyShortlink);
            });
        </script>
        <?php
    }
}
add_action('wp_footer', 'add_copy_shortlink_script');

Leave a Reply

Your email address will not be published. Required fields are marked *

 
Online-Chat

We are ready to answer your questions