• When a WooCommerce product is added to cart (POST) with the product URL being redirected from in the Redirection plugin, the product is not added to cart.
    The POST request for adding the product to cart gets a 301 HTTP response instead.

    That to the old URL is posted the actual underlying issue – the <form action URL still points to the old URL from which the Redirection plugin already redirected away to the new URL.
    So why does the WooCommerce add to cart form still use the old URL in action?

    Edit: Things become more clear now – the template for the add to cart form uses the permalink of the product (post):

    
    <form class="cart" action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>" method="post" enctype='multipart/form-data'>
    

    But the Redirection plugin doesn’t filter the product permalinks.
    IMHO the best solution would be if the Redirection plugin could allow processing of those POST requests and then do nothing as these specific POST requests are invoked by ajax and not visible to the user.

    There is no filter for is_protected_url yet, so I can’t just add a hook to prevent redirection of WooCommerce POST requests.

    • This topic was modified 3 years, 3 months ago by strarsis.
    • This topic was modified 3 years, 3 months ago by strarsis.
    • This topic was modified 3 years, 3 months ago by strarsis.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author John Godley

    (@johnny5)

    I’m not quite sure I understand, and I’m not familiar with WooCommerce.

    Are you saying that you have a redirect for a URL that you want redirected when it is a GET request, but not when it is a POST request?

    Thread Starter strarsis

    (@strarsis)

    @johnny5: Correct. WooCommerce uses POST requests to add product items to cart.
    Redirection also redirects these requests, preventing processing by WooCommerce.

    This workaround fixes the issue:

    
    if(!empty($_REQUEST['add-to-cart'])) {
        define( 'REDIRECTION_DISABLE', true );
    }
    

    Would it be a good idea to add these workarounds for WooCommerce to the plugin or add an extension plugin for improved compatibility?

    Reproducing the issue:
    1. Install + enable Redirection plugin
    2. Install + enable WooCommerce plugin.
    3. Configure Redirection plugin to watch URL changes of products (very useful feature).
    4. Create a new product.
    5. /Rename the product, cause its permalink to change (e.g. title/slug/etc), update product.
    6. Ensure Redirection plugin added a redirection of old product URL to new one.
    Request the old product URL, notice that Redirection plugin redirects to the new one.
    7. On product page, add product to cart. Disable AJAX add-to-cart for easier debugging.
    Note that no product items were added to cart for this particular product.
    See in Developer Tools, Network tab, that the first request (POST) is redirected by Redirection plugin – before WooCommerce can process it (changing cart state).
    8. Disable the redirection for that product. Note that now product items can be added to cart.

    • This reply was modified 3 years, 3 months ago by strarsis.
    • This reply was modified 3 years, 3 months ago by strarsis.
    • This reply was modified 3 years, 3 months ago by strarsis.
    Plugin Author John Godley

    (@johnny5)

    Why do you need to redirect the URL if WooCommerce is using them? Could you not redirect the URL at all?

    There are filters you can use if you require custom rules. For example, redirection_url_source

    https://redirection.me/developer/wordpress-hooks/

    Thread Starter strarsis

    (@strarsis)

    @johnny5: It is very useful that this Redirection plugin redirects old product URLs to new ones. But it should skip redirection for POST requests, at least for those with add-to-cart parameter, otherwise WooCommerce won’t see these requests and that product can’t be added to cart.

    New workaround using the dedicated redirection_url_source filter:

    
    // Redirection plugin doesn't only redirect GET requests for product permalink URLs that 
    // are redirected to new URL (e.g. after a post slug change), 
    // but also POST requests, as for adding product items to cart, 
    // which would prevent adding product items of these products to cart.
    // Workaround:
    add_action ('redirection_url_source', function( $original_url ) {
        if(!empty( $_REQUEST['add-to-cart'] )) {
            return false; // skip
        }
    
        return $original_url;
    }, 10, 1 );
    • This reply was modified 3 years, 3 months ago by strarsis.
    • This reply was modified 3 years, 3 months ago by strarsis.
    • This reply was modified 3 years, 3 months ago by strarsis.
    Plugin Author John Godley

    (@johnny5)

    Why do you need to add an old product to the cart? As I said, I’m not familiar with WooCommerce and I may be missing something here.

    Thread Starter strarsis

    (@strarsis)

    OK, so it seems that the actual issue is that the permalink of the post is not correctly updated, while the Redirection redirect uses the new URL.

    For whatever reason a redirect was added that doesn’t reflect the current permalink anymore. Maybe the categories/terms structure.

    • This reply was modified 3 years, 3 months ago by strarsis.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘[WooCommerce] Prevents products being added to cart’ is closed to new replies.