Skip to content

Instantly share code, notes, and snippets.

@topmask
Created October 27, 2021 05:18
Show Gist options
  • Save topmask/d7f2415e8a607801bb06160d5da783a4 to your computer and use it in GitHub Desktop.
Save topmask/d7f2415e8a607801bb06160d5da783a4 to your computer and use it in GitHub Desktop.
Fix for “Sold Individually” Products
add_filter( 'woocommerce_product_add_to_cart_url', 'elftoy_fix_for_individual_products', 10, 2 );
function elftoy_fix_for_individual_products( $add_to_cart_url, $product ){
if( $product->get_sold_individually() // if individual product
&& WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product->id ) ) // if in the cart
&& $product->is_purchasable() // we also need these two conditions
&& $product->is_in_stock() ) {
$add_to_cart_url = wc_get_checkout_url();
}
return $add_to_cart_url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment