Woocommerce direct checkout for subscriptions - wordpress

I know it’s possible to send customer directly to the checkout page by appending “add-to-car=product_id” to the checkout endpoint like this:
yourwebsite.com/checkout/?add-to-cart=12345
I was wondering if it’s a way to do the same when a product has subscription options? I use Woocommerce Subscriptions plugin to manage subscriptions and it doesn't create a separate variant for subscription product.

Based on these answers:
Woocommerce add to cart button redirect to checkout
WooCommerce Subscriptions - Check if a product is a subscription product
You can create a PHP script to change the redirect url after adding a product to the cart (by checking the product class type).
Furthermore, using the woocommerce_add_to_cart_redirect hook you have two parameters: $url and $adding_to_cart (product object). Here you find the documentation.
Then:
// redirects to checkout if the product added to the cart is a subscription
add_filter( 'woocommerce_add_to_cart_redirect', 'redirect_to_checkout_if_product_is_subscription' );
function redirect_to_checkout_if_product_is_subscription( $url, $product ) {
if ( class_exists( 'WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $product ) ) {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}
return $url;
}
The code must be added to your active theme's functions.php file.

Related

How To Hide/Remove Additional Checkout Fields for Specific Products On Checkout Page?

I have installed WooCommerce Checkout & Account Field Editor plugin on my WordPress website for adding additional fields to my checkout page. The purpose of installing this plugin is to add three additional fields like how_you_heard_about_our_store, user_membership_level and user_refferer_name to the checkout page for getting additional details from my users.
This is working fine and the user can provide the required information as needed during checkout. However, there is one product Gift Certificate that I need to exclude these additional fields on it when this product is on the checkout page.
The purpose is to hide these fields on this product only. I have 4 different variations of this product and I need these fields to be hidden for each of its variations.
I have tried my following techniques but this is actually for default WooCommerce fields.
https://www.liquidweb.com/kb/way-conditionally-show-hide-checkout-fields-specific-products-product-categories-store/
Also, I have tried the following as well with no luck:
function custom_override_checkout_fields( $fields ) {
unset($fields['order']["how_heard"]);
unset($fields['order']["member_level"]);
return $fields;
}
Is there any specific action or filter for removing additional fields from my checkout page? Any help would highly be appreciated.
add_filter( 'woocommerce_checkout_fields' , 'hide_checkout_fields' );
function hide_checkout_fields( $fields ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$id = $product->get_id();
$products = array(2516, 584, 2454); // Product Ids
if (in_array($id, $products))
{
unset($fields['order']['how_heard']);
unset($fields['order']['member_level']);
}
}
return $fields;
}

Wordpress single page product and checkout

I'm new to woocommerce and trying to combine the single product page and checkout page together. For eg: I want to move the billing details from this page
https://exthemes.net/wootour/checkout/
to
https://exthemes.net/wootour/product/new-york-philadelphia-washington/
How can I achieve this?
From my research there is only a premium plugin from woocommerce which provides this functionality which is not an option and [woocommerce_checkout] shows checkout page on product page. I need to move the billing form from checout page to product page
Please provide pointers or free plugins if available on how to achieve this.
First create child theme
Check the code of form-checkout.php and place it on products page.
You can see form-billing.php and form-shipping.php codes. You can add this your products page
Note: Woocommerce templates can be overridden in your child themes
Best way I found is to empty cart and add product to cart when visiting a single product page from a specific category. I also added the checkout form on my single product page using the plugin Woocommerce Single Page Checkout.
/**
* #snippet Add Product to Cart When Visiting a Single Product Page from a Specific Category - WooCommerce
* #author Zaa Normandin
* #author Rodolfo Melogli
* #compatible WooCommerce 3.4.3
*/
add_action( 'wp', 'bbloomer_add_product_to_cart_on_page_id_load' );
function bbloomer_add_product_to_cart_on_page_id_load() {
// first, bail if WC isn't active since we're hooked into a general WP hook
if ( ! function_exists( 'WC' ) ) {
return;
}
$id = get_the_ID();
if (is_product() && has_term( 'information', 'product_cat' ) || is_product_category( 'information' )) {
WC()->cart->empty_cart();
WC()->cart->add_to_cart( $id );
}
}
Ref.: https://businessbloomer.com/woocommerce-add-product-to-cart-when-visiting-a-specific-page/
I added the code at the end of functions.php

Woocommerce Hook for Custom Behaviour for Place Order

I wonder if there's a hook for changing Place Order Button behaviour upon click. I am trying to replace/change a product upon placing order as the product selection (all available products) are also in the Checkout Page.
So far I have been trying to manipulate woocommerce_checkout_order_review hook & failed.
add_action('woocommerce_checkout_order_review', 'remove_woocommerce_product');
function remove_woocommerce_product(){
if (isset($_POST['woocommerce_checkout_place_order'])){
global $woocommerce;
$woocommerce->cart->empty_cart(); // Empty the cart
$selectedproduct = $_POST['selectedproductid']; // Get the selected product
WC()->cart->add_to_cart( $selectedproduct ); // Insert the selected product in the the cart
return esc_url( wc_get_checkout_url() ); // Redirect to Payment Gateway Page
}
}
The hook above is not triggered upon Place Order. Maybe there's something wrong with my code or maybe what I suspected is wrong hook applied. Any ideas?
Nevermind... found the answer...
add_action('woocommerce_checkout_process', 'change_product_upon_submission');
function change_product_upon_submission() {
if ( !empty( $_POST['_wpnonce'] ) && !empty($_POST['selectedproductid']) ) {
$selectedproduct = $_POST['selectedproductid']; // Get the selected product
WC()->cart->empty_cart(); //Empty the cart
WC()->cart->add_to_cart( $selectedproduct ); // Insert the selected product in the cart
}
}
For more explanation, see Woocommerce Replace Product in Cart Upon Place Order in Checkout Page

Woo commerce change titles on checkout page

Can anyone direct me to where I can find the file where I can change the "State/County" and "Zip Code" titles in a woocommerce checkout page?
WooCommerce offers hooks for this:
please check Customizing checkout fields using actions and filters
as you'll see on that page you can hook a function (that you'll save in your childs functions.php for example ) to WooCommerce checkout page and change the data that is available.
so your code will look something like:
/ Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['shipping']['shipping_postcode']['label'] = 'My new postcode title';
$fields['shipping']['shipping_state']['label'] = 'My new state title';
return $fields;
}
other examples and fields on the linked page.

Woocommerce conditionally redirect to cart after clicking "add to cart" button

I'm beginner with woocommerce api. I'm looking for a conditional filter when user click "add to cart". I'd like that it automatically redirect to the checkout for a specific product category. Is that possible?
I already know how to redirect, but it does it for all products.
I found a little code snippets to do so :
add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout() {
global $woocommerce;
//Get product ID
$product_id = (int) apply_filters('woocommerce_add_to_cart_product_id', $_POST['product_id']);
//Check if product ID is in a certain taxonomy
if( has_term( 'membership-donations', 'product_cat', $product_id ) ){
//Get cart URL
$checkout_url = get_permalink(get_option('woocommerce_checkout_page_id'));
//Return the new URL
return $checkout_url;
};
}
This is the default option that is build into WooCommerce. You can find the option in the WooCommerce -> Settings -> Products -> Display area. When the option “Redirect to the cart page after successful addition” is checked it will redirect all users to the cart after adding a product to the cart.

Resources