Reputation: 13
In Woocommerce i need to skip the cart page when adding products and redirect directly to checkout only for subscription product types.
I found the below code somewhere else which works for skipping the cart page based on product ID, however I couldn't get it right to use product type instead (see further below for what I tried).
function woocommerce_skip_cart() {
global $woocommerce;
$product_id = (int) apply_filters( 'woocommerce_add_to_cart_product_id', $_POST['add-to-cart'] );
if ( $product_id == 31 || $product_id == 31 ) {
$checkout_url = WC()->cart->get_checkout_url();
return $checkout_url;
}
}
add_filter ('woocommerce_add_to_cart_redirect', 'woocommerce_skip_cart');
What I tried: (it breaks the whole site: becomes a blanc page)
function woocommerce_skip_cart() {
global $woocommerce;
$product_id = (int) apply_filters( 'woocommerce_add_to_cart_product_id', $_POST['add-to-cart'] );
$product = wc_get_product( $product_id );
if ( $product->is_type( 'subscription' ) ){
$checkout_url = WC()->cart->get_checkout_url();
return $checkout_url;
}
}
add_filter ('woocommerce_add_to_cart_redirect', 'woocommerce_skip_cart');
Any idea's how to achieve this?
Upvotes: 1
Views: 991
Reputation: 11282
I revised your code. try the below code.
function woocommerce_skip_cart( $url ) {
$product_id = (int) apply_filters( 'woocommerce_add_to_cart_product_id', $_POST['add-to-cart'] );
if( $product_id ){
$product = wc_get_product( $product_id );
if ( $product->is_type( 'subscription' ) || $product->is_type( 'variable-subscription' ) ){
$checkout_url = WC()->cart->get_checkout_url();
return $checkout_url;
}
}
return $url;
}
add_filter ('woocommerce_add_to_cart_redirect', 'woocommerce_skip_cart');
Upvotes: 1