woocommerce / google-listings-and-ads

Sync your store with Google to list products for free, run paid ads, and track performance straight from your store dashboard.
https://woo.com/products/google-listings-and-ads/
GNU General Public License v3.0
46 stars 21 forks source link

Checkout URL can't be set due to _gla Prefix #2125

Open Babylon1999 opened 12 months ago

Babylon1999 commented 12 months ago

User story

zd-7122721 https://wordpress.org/support/topic/checkout-url-not-working-after-google-adding-gla_-to-product-id/

As mentioned in the thread, the merchant is trying to add a checkout link but since the ID is passed with the _gla prefix they can't use the ?add-to-cart= parameter.

There is also a way to add this in the product level, but from what I checked, this attribute isn't supported: https://support.google.com/merchants/answer/13580733?sjid=14960691092183889729-EU

How to reproduce the problem

Try to add a checkout URL as explained in the guide above.

Describe the solution you'd like

I'm not sure what's the best way to approach this, perhaps pass the product ID only in a dedicated attribute or add support for [checkout_link_template] in the product level?

puntope commented 11 months ago

Hello there,

One option for this user might be to hook into the add to cart event in WooCommerce woocommerce_add_to_cart_product_id

Example:

add_filter( 'woocommerce_add_to_cart_product_id', 'woocommerce_add_to_cart_product_id_remove_gla_prefix' );
function woocommerce_add_to_cart_product_id_remove_gla_prefix( $item_product_id ) {
      if (! isset( $_POST['product_id'] ) ) {
          return $item_product_id;
      }
      // Notice we don't use $item_product_id as it would be 0 because of a failing conversion to integer. 
      return absint( str_replace( 'gla_', '', $_POST['product_id'] ) );
}

The provided code is just an indicative example and it was not tested on a live site.

The second option is to add the checkout_link_template prop via code snippet and resync the products to send that property:

Example:

add_filter( 'woocommerce_gla_product_attribute_values', 'woocommerce_gla_product_attribute_values_add_checkout_link' ,10,3 );

function woocommerce_add_to_cart_product_id_remove_gla_prefix( $attributes, $wc_product, $gla_product ) {
      return [ 'checkout_link_template' => "https://example.com/?add-to-cart=" . $wc_product->get_id() ];
}

The provided code is just an indicative example and it was not tested on a live site.

Babylon1999 commented 11 months ago

one more zd-7217696

https://wordpress.org/support/topic/how-to-add-checkout-templale/

Babylon1999 commented 11 months ago

Thanks for the help @puntope!

I didn't know this filter existed: woocommerce_gla_product_attribute_values

Perhaps this should be added to the docs, or will there be a better way to handle this in the future?

Related feature request: https://woocommerce.com/feature-request/google-direct-checkout-option/#

kaoopro commented 10 months ago

url like this https://domain.com/checkout/?add-to-cart={id} here is code // 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 );

mikkamp commented 10 months ago

As suggested in the comment above, another option is to sync products without the prefix. This is also covered in the FAQ here: https://woo.com/document/google-listings-and-ads/faqs/#section-12

We do need to take note that this will submit products with completely different ID's so it would mostly be recommended for a new site that hasn't previously created campaigns with synced products. See the FAQ for more details.

bhannawa commented 8 months ago

https://github.com/woocommerce/google-listings-and-ads/issues/2125#issuecomment-1752676612

Just in case anyone is wondering, neither of these proposed solutions work. The second one had a small error that was easily fixed, but even then it doesn't work because GMC returns the error: ""Invalid JSON payload received. Unknown name \"checkout_link_template\" at 'body.entries[0].product'"

Seems like Google still isn't accepting product level checkout links, even in the US where they say it is an available attribute. Just putting this out there so anyone looking for a workaround knows not to waste their time. No shade to @puntope btw, he did say these were untested.

Mark-LohseMiranda commented 7 months ago

mikkamp's suggestion just worked for me

As suggested in the comment above, another option is to sync products without the prefix. This is also covered in the FAQ here: https://woo.com/document/google-listings-and-ads/faqs/#section-12

We do need to take note that this will submit products with completely different ID's so it would mostly be recommended for a new site that hasn't previously created campaigns with synced products. See the FAQ for more details.

OmarFPG commented 1 month ago

8653583-zd Hi @puntope, may I ask if there's a solution to this already built-in? I see it was simply closed. For now, I've provided the merchant with the workaround of removing the gla_ prefix.

mikkamp commented 1 month ago

may I ask if there's a solution to this already built-in?

We don't have anything built in to handle the Checkout URL. We'd suggest to either use one of the suggestions here such as setting it per product. Or alternatively use the workaround of removing the prefix. Which it seems you already suggested.

OmarFPG commented 1 month ago

Hi, @mikkamp, as mentioned by other users in this thread, the solution by @puntope seems like it doesn't work. The merchant from 8653583-zd doesn't want to do by removing the gla_ prefix as they don't want to lose their campaign data. Any thoughts on how to proceed?

puntope commented 3 weeks ago
add_filter( 'woocommerce_add_to_cart_product_id', 'woocommerce_add_to_cart_product_id_remove_gla_prefix' );
function woocommerce_add_to_cart_product_id_remove_gla_prefix( $item_product_id ) {
      if (! isset( $_POST['product_id'] ) ) {
          return $item_product_id;
      }

      // Notice we don't use $item_product_id as it would be 0 because of a failing conversion to integer. 
      return absint( str_replace( 'gla_', '', $_POST['product_id'] ) );
}

This one doesn't work because of this check in https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/class-wc-form-handler.php#L791

I think that check is not strictly necessary as we do absint after. So I believe we can attempt to get rid of that so this filter can work.

puntope commented 3 weeks ago

As for the second one, you're correct. Is not working as its.

This new version is tested in a way that I see the field is loaded successfully in the Content API Raw:

add_filter( 'woocommerce_gla_product_attribute_values', 'woocommerce_gla_product_attribute_values_add_checkout_link' ,10,3 );

function woocommerce_gla_product_attribute_values_add_checkout_link( $attributes, $wc_product, $gla_product ) {
    $attributes['customAttributes'][] = [
        "name" => "checkout_link_template",
        "value" => "https://example.com/?add-to-cart=" . $wc_product->get_id()
    ];

    return $attributes;
}
Screenshot 2024-09-03 at 17 24 02

cc: @OmarFPG @bhannawa

jsballarini commented 3 weeks ago

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: template_redirect: This hook runs just before WooCommerce processes the URL and loads the template, giving us the chance to modify the URL.

isset($_GET['add-to-cart']): Checks if the add-to-cart parameter is present in the URL.

strpos($add_tocart, 'gla') === 0: Ensures that the add-to-cart value starts with the gla_ prefix.

strreplace('gla', '', $add_to_cart): Removes the gla_ prefix so that only the product ID is left.

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.