• Resolved plcexchange

    (@plcexchange)


    Hello Team,

    I am trying to change up how I am moving products from my store over to Google Merchant Center (as well as incorporating Ads/Analytics/etc) and have a small issue I am trying to resolve.

    My “legacy” feed would feed in product and info to Google Merchant Center with the “Product ID” field in Google Merchant Center matching the woo product ID, which would allow me to easily connect the “Checkout” option to my displays (https://site.com/checkout/?add-to-cart={id})

    With the new plugin, keeping in mind I have both feeds active AND its most likely doesn’t allow duplicate IDs*, the new products are imported into Google Merchant Center with the Product ID showing “gla_{id}” where ID is the id in woo, and “gla_” is a generic prefix appended to the front of all product ids.

    Why does it do this and/or how do I modify this so that it removes the prefix “gla_” or how do I modify the checkout URL to work with the “gla_” prefix that is added? I have checked the URL and using the “gla_1234” for example, does not work; however, “1234” does what you would expect (where 1234 is the woo product ID)

    Can anybody comment on this? Thanks!

Viewing 10 replies - 1 through 10 (of 10 total)
  • Hi.

    1. Remove all items for you sync before
    2. Add this code to functions.php The gla_ prefix is automatically added to products to help identify products synced by the?Google for WooCommerce?extension. If you’d like to remove the prefix, please use this code snippet:
    // Sync products without GLA prefix.
    add_action(
    'woocommerce_gla_get_google_product_offer_id',
    function ( $mc_id, $product_id ) {
    return (string) $product_id;
    },
    10,
    2
    );

    Please note:

    • This would cause all the products to be added again, as a different ID means a new product.
    • Historic reports for campaigns that used the gla_ prefix would no longer match.
    • To avoid having duplicate products, please remove all existing products in Google Merchant Center by following these steps:
    1. Go to the test connection page: example.com/wp-admin/admin.php?page=connection-test-admin-page (replace example.com with your domain name)
    2. Scroll to the bottom of the page, Under “Product Sync” click?Delete All Synced Products from Google Merchant Center?and allow a few minutes for all existing products to be removed from GMC. This will ensure that products synced afterwards don’t have the gla_ prefix.

    Solution 2: (This method allows you not to re-upload products again, but there is a caveat that this puts additional load on the server. However, it preserves the existing analytics with the gla_ prefix.)

    add_action('template_redirect', 'gla_remove_prefix_from_cart_url');
    
    function gla_remove_prefix_from_cart_url() {
        if (isset($_GET['add-to-cart']) && strpos($_GET['add-to-cart'], 'gla_') === 0) {
            // Remove the 'gla_' prefix
            $product_id = str_replace('gla_', '', $_GET['add-to-cart']);
    
            // Redirect to the new URL without the 'gla_' prefix
            $redirect_url = add_query_arg('add-to-cart', $product_id, remove_query_arg('add-to-cart'));
            wp_redirect($redirect_url);
            exit;
        }
    }

    This code works as follows:

    1. Uses the template_redirect hook to execute the function before the template is rendered.
    2. Checks if the add-to-cart parameter is present in the query and starts with gla_.
    3. If the prefix is found, it removes the gla_ prefix from the product ID.
    4. Creates a new URL without the prefix and redirects to it.

    Thus, when a request is made with the gla_ prefix, it automatically redirects to the URL without this prefix.

    From my perspective, it’s time for developers to add a new option in the settings that allows choosing a custom prefix, such as example_*, and also the ability to disable it altogether. If no rules are applied, simply use gla_ as the default. This way, people won’t have to look for solutions to such problems, as I have faced. Why is this necessary if gla_ is not supported when forming the checkout link? It’s ridiculous… Developers of WooCommerce should then add support so that gla_productID is supported in https://site.com/checkout/?add-to-cart={id}.”

    Thread Starter plcexchange

    (@plcexchange)

    Marcus,

    Thank you for the thorough solutions. I think at this time, solution 2 would be what I am going for. I am still in this “unknown” phase on what direction I want to go, and because my original products have been on free listings for 3+ years, I would hate to delete them this early in testing. For now, I am fine with the new product_id showing in google (it makes no difference to me at the moment) I just want the add-to-cart hotlink to work as it should in your second example.

    Question: Can you go into a little more detail on WHERE that hook should be added? I have a medium understanding of the backend of WordPress/Woo but am I adding that function to a specific template? Which one? is template_redirect one of the theme files? Probably one I need to add?

    Question: Can you go into a little more detail on WHERE that hook should be added? I have a medium understanding of the backend of WordPress/Woo but am I adding that function to a specific template? Which one? is template_redirect one of the theme files? Probably one I need to add?

    So you must add code to functions.php (Your Active Theme) At the beginning of the file or at the end, as you like. template_redirect – Hook this WordPress Hook..

    Add the code to the theme and then just try to go to https://yourdomain.com/checkout/?add-to-cart=gla_here_productID if code work you should get a link https://yourdomain.com/checkout/?add-to-cart=here_productID

    note that in the condition of the code, you can replace the prefix gla_ with whatever you use

    • This reply was modified 3 months, 3 weeks ago by Marcus Karlos.

    PS. You can replace add_action(‘template_redirect’ to add_action(‘init’ if will not work

    Thread Starter plcexchange

    (@plcexchange)

    Marcus, still working to find time to try this feature. I will let you know ASAP Thanks!

    Plugin Support Saravanan S, a11n

    (@simplysaru)

    Hey there @plcexchange , Any updates on this? Did the recommended solution work for your needs?

    Can you provide us with a picture to explain
    How can we remove the gla identifier and replace it with the sku identifier present in the product

    Plugin Support Saravanan S, a11n

    (@simplysaru)

    Hi @shakoor99 , do try the step by step instructions here.

    I wrote some code in the functions.php of my active theme and it worked for me.

    add_action('template_redirect', 'remove_gla_prefix_from_add_to_cart_url');

    function remove_gla_prefix_from_add_to_cart_url() {
    // Check if the 'add-to-cart' parameter is present in the URL
    if (isset($_GET['add-to-cart'])) {
    $add_to_cart = $_GET['add-to-cart'];

    // Check if the 'add-to-cart' parameter starts with 'gla_'
    if (strpos($add_to_cart, 'gla_') === 0) {
    // Remove the 'gla_' prefix and update the product ID
    $product_id = str_replace('gla_', '', $add_to_cart);

    // Redirect to the correct URL without the 'gla_' prefix
    wp_safe_redirect(add_query_arg('add-to-cart', $product_id));
    exit;
    }
    }
    }

    Explanation:

    1. template_redirect: This hook runs just before WooCommerce processes the URL and loads the template, giving us the chance to modify the URL.
    2. isset($_GET['add-to-cart']): Checks if the add-to-cart parameter is present in the URL.
    3. strpos($add_to_cart, 'gla_') === 0: Ensures that the add-to-cart value starts with the gla_ prefix.
    4. str_replace('gla_', '', $add_to_cart): Removes the gla_ prefix so that only the product ID is left.
    5. wp_safe_redirect(): Redirects the user to the same URL but with the corrected add-to-cart value without the prefix.

    This code should now handle the URL redirection in WooCommerce properly by removing the gla_ prefix before processing the add-to-cart parameter.

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.