nshelper

WPGlobalCart - adjust the product url add-to-cart link if the page is a search.

Apr 29th, 2026
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. add_filter ( 'woocommerce_product_add_to_cart_url', 'custom_woocommerce_product_add_to_cart_url', 10, 2 );
  2. function custom_woocommerce_product_add_to_cart_url( $url, $product )
  3. {
  4. if ( ! is_search ( ) )
  5. return $url;
  6.  
  7.  
  8. $url = $product->is_purchasable() && $product->is_in_stock() ? remove_query_arg(
  9. 'added-to-cart',
  10. add_query_arg(
  11. array(
  12. 'add-to-cart' => $product->get_id(),
  13. ),
  14. $product->get_permalink()
  15. )
  16. ) : $product->get_permalink();
  17.  
  18. return $url;
  19.  
  20. }
Advertisement
Add Comment
Please, Sign In to add comment