customizing woocommerce address fields - wordpress

I want to edit, or add some fields to, woocommerce billing and shipping address in checkout page, profile page and everywhere in which the fields are displayed.
can anybody help me?

revving an old question but
function override_default_address_fields( $fields ) {
unset($fields['company']);
foreach($fields as $key => $value) {
$fields[$key]['label_class'] = 'sr-only';
}
return $fields;
}
add_filter( 'woocommerce_default_address_fields', 'override_default_address_fields' );
all the available fields can be found in includes/class-wc-countries.php -> public function get_default_address_fields()

It's very simple to edit or add new fields in woo commerce checkout page you can try with hard code and with woo commerce checkout field editor plugin

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.

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

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