Site logo

[Support request] Hook code and placement for Woocommerce button

Home Forums Support [Support request] Hook code and placement for Woocommerce button

Home Forums Support Hook code and placement for Woocommerce button

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1836028
    Mirela

    Hi,

    I am working on a staging multi-site WP installation that I would like to have live next week.
    I have the GP Premium with Elements activated.

    I want to use a code to open a link in a new tab when somebody clicks on the Add to Cart Button.
    (Preferably for external products if I’m thinking long-term, but for now it will do for all.)

    Since not all the subfolders have WooCommerce active, I’d like to use a hook rather than a function to insert this code.
    I’ve just started using Elements and I would appreciate telling me the hook location for obtaining the result I need.

    TIA!

    #1836067
    David
    Staff
    Customer Support

    Hi there,

    the Hook Element only works for Action Hooks (add_action) – so you could hook in your own button function but you cannot change the behaviour of the existing button.

    This article shows the Hook locations for woocommerce shop:

    https://www.businessbloomer.com/woocommerce-visual-hook-guide-archiveshopcat-page/

    And under the Default Actions you can see the Hook for the default add to cart is added:

    add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );

    And another visual guide foe the single product page:

    https://www.businessbloomer.com/woocommerce-visual-hook-guide-single-product-page/

    However – to remove the default add to cart button you can do it for the shop/archives but not the single product:

    https://docs.generatepress.com/article/woocommerce-overview/

    So you would still require a remove_action function to take out the default button.

    Ideally you should use the filter hook to adjust the buttons code – see here:

    https://github.com/woocommerce/woocommerce/blob/b19500728b4b292562afb65eb3a0c0f50d5859de/templates/loop/add-to-cart.php#L25

    And on the same site as the above visual guides is a function that will do what you require:

    https://www.businessbloomer.com/woocommerce-add-cart-url-open-new-tab/

    You may just need to wrap that up inside a condition so it only fires if Woocommerce is active:

    https://wordpress.stackexchange.com/a/159179

    #1836220
    Mirela

    Hi,

    I bookmarked this DIY method for later, I was really hoping for a quick fix.
    Is it possible to implement this in the theme at one point? Because Woo Commerce doesn’t want to do it, they just suggest other plugins, without even testing them…

    #1836379
    David
    Staff
    Customer Support

    Its not something we would likely add to the theme as an option. As it means interfering with that Woo function which is a bit of minefield when it comes to the various product types and 3rd party plugins that hook into it.

    The filter method would be the best way to go.

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