Skip to content

Instantly share code, notes, and snippets.

@Acephalia
Last active April 11, 2024 07:46
Show Gist options
  • Save Acephalia/d8020ac7fffa569d8aa77d74c572baae to your computer and use it in GitHub Desktop.
Save Acephalia/d8020ac7fffa569d8aa77d74c572baae to your computer and use it in GitHub Desktop.
Woocommerce add to cart for product thumbnail link
//Don't use this it is a bad idea. This code replaces all acrhcive and shop page thumbnail links to work as add to cart urls. When clicked the item will be added to cart instead of opening the single product page. Works only for simple products (obviously).
function wc_custom_shop_thumbnail_link( $link, $product ) {
if ( $product->is_type( 'simple' ) && ( is_shop() || is_product_category() || is_product_tag() ) ) {
$link = '?add-to-cart='. $product->get_id();
}
return $link;
}
add_filter( 'woocommerce_loop_product_link', 'wc_custom_shop_thumbnail_link', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment