Wordpress single page product and checkout - wordpress

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

Related

Woocommerce direct checkout for subscriptions

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.

Remove some features of WooCommerce

I'm modifying a theme for WordPress and i can't find a solution for:
   1. How can I delete the WooCommerce product image inside the gallery? Because this image is added automatically and cannot be deleted.
IMAGES: https://imgur.com/a/qd0INEX
   2. How can I deactivate the cart page, is it possible? I'm only interested in the Checkout page. I've been looking at some codes but they don't allow to select more than 2 products.
Greetings to all who comment, I hope these issues can help more people in the future.
Redirect to Checkout when a Product has been added to the Cart
Second question answer:
How can I deactivate the cart page, is it possible? I'm only interested in the Checkout page. I've been looking at some codes but they don't allow to select more than 2 products.
Step 1.
// Disable AJAX add to cart buttons
First of all, we have to do some small configurations in WooCommerce Settings – Uncheck the “Enable AJAX add to cart buttons on archives” checkbox.
Step 2.
// Change text on add to cart buttons
/*
* Change button text on Product Archives
*/
add_filter( 'woocommerce_loop_add_to_cart_link', 'nik_add_to_cart_text_1' );
function nik_add_to_cart_text_1( $add_to_cart_html ) {
return str_replace( 'Add to cart', 'Buy now', $add_to_cart_html );
}
/*
* Change button text on product pages
*/
add_filter( 'woocommerce_product_single_add_to_cart_text', 'nik_add_to_cart_text_2' );
function nik_add_to_cart_text_2( $product ){
return 'Buy now';
}
I decided that str_replace() for this situation is the most simple and easy solution, but if you do not want to use it, you can replace the first part of the code with this one:
/*
* Change button text on Product Archives
*/
add_filter( 'woocommerce_product_add_to_cart_text', 'nik_add_to_cart_text_1', 10, 2 );
function nik_add_to_cart_text_1( $text, $product ){
return $product->is_purchasable() && $product->is_in_stock() ? 'Buy Now' : 'Read more';
}
Step 3.
// Redirect to Checkout Page
add_filter( 'woocommerce_add_to_cart_redirect', 'nik_skip_cart_redirect_checkout' );
function nik_skip_cart_redirect_checkout( $url ) {
return wc_get_checkout_url();
}
Step 4.
// Remove “The product has been added to your cart” message
add_filter( 'wc_add_to_cart_message_html', 'nik_remove_add_to_cart_message' );
function nik_remove_add_to_cart_message( $message ){
return '';
}

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;
}

How to remove add to cart button from shop page in woocommerce

I want to remove add to cart button from the shop page, product category page and home page in woocommerce. But i want to show it on the product page. That meance i want my costumers to open product page to add the product to cart.
To remove add to cart button from product page. you need to override the product page template .
So locate to woocommerce directory find the below path .
wp-content/plugins/woocommerce/templates/content-product.php
and remove the text which says - It will remove the Add To Cart Button for each product in the Shop page, retaining the Button in the Single Product Page.
This worked for me:
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
if( is_product_category() || is_shop()) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}
}

Woocommerce customize the product name in the cart and order pages

Actually, I already got hook to customize the price in the cart page and all other page.
This is the hook to customize the product price
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
$custom_price = 10; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
}
}
Actually, I am using woocommerce plugin. I want a hook to customize the product name displayed in the cart page and all other pages next to cart page.
I want to customize the Product name , i want to add the some static attributes to product name displayed in the cart page, order page ,order details page and all the pages next to cart page.
Thanks in advance
I wanted to share this as I managed to figure out something for my needs. (I only ever have a single item in the order as I've customised WooCommerce for holiday bookings.)
<?php
add_action( 'woocommerce_product_title', 'add_custom_name' );
function add_custom_name( $title ) {
return 'test name';
}
?>
Hopefully someone can elaborate on this further or take what has been done with the custom price code an properly re-purpose it for product names.

Resources