Change Shipping Address title based on selected shipping method in Woocommerce - wordpress

I want to change the Shipping Address title based on the selected shipping method. So for example when someone chooses flat_rate:1 (or a different shipping method -> wcsdm:7 in the code below) the Shipping Address title should change to "title X".
I already tried some code but without any luck.
add_action( 'woocommerce_before_checkout_shipping_form', 'change_shipment_title' );
function change_shipment_title($method, $index) {
// Targeting checkout page only:
if( is_cart() ) return; // Exit on cart page
if( 'wcsdm:7' === $method->id ) {
echo __("<span>Title X</span>");
}
}
Hope some of you could help me with this

Related

Getting product stock quantity isn't working

On single product page I'm trying to get stock quantity or atleast stock status from woocommerce product but it seems not working properly, even if it's simple product (manage stock status is checked) or variable product the button is not showing. And strange thing happens - I've got Yith Product Bundle plugin and even if every item is in stock it shows that button but whatever, that's not main problem.
So here is code I'm using
add_action('woocommerce_before_add_to_cart_button', 'show_wishlist');
function show_wishlist() {
global $product;
$stockQ=$product->get_stock_quantity();
if ( $stockQ < 1 ) {
echo "<button>Hello</button>";
}
}
I've also tried but completely nothing happens.
add_action('woocommerce_before_add_to_cart_button', 'show_wishlist');
function show_wishlist() {
global $product;
if ( ! $product->is_in_stock() ) {
echo "<button>Hello</button>";
}
}
First check if the hook is firing properly.
add_action( 'woocommerce_before_add_to_cart_button', 'show_wishlist' );
function show_wishlist(){
echo '<p>Some custom text before</p>';
}
If you are able to confirm that the hook is working then use the below code.
function show_wishlist() {
global $product;
// Change In Stock Text
if ( $product->managing_stock() && $product->is_in_stock() ) {
//add your code here for Products in Stock
}
else{
//add your code here for Products out of Stock
}
}

Hide billing_postcode field based on shipping methods in woocommerce

I've two shipping methods
Pickup
Delivery
If Pickup is selected - billing_postcode field in checkout must be hidden.
If Delivery selected - billing_postcode should be displayed.
I've tried this snippet
add_filter('woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields');
function xa_remove_billing_checkout_fields($fields) {
$shipping_method ='local_pickup:6'; // Value of the applicable shipping method.
global $woocommerce;
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ($chosen_shipping == $shipping_method) {
unset($fields['billing']['billing_postcode']);
}
return $fields;
}
It works only when I force reload the page. I've tried to clear the site cache but it didn't helped me.
Kindly help me to resolve this. Waiting for response with hope.

Change checkout form based on shipping option woo-commerce

How do you change the woo-commerce checkout form for different shipping options? If they choose free shipping the checkout page displays shipping form. If they choose e-voucher the check out page displays a simpler form.
You can use the below code snippet to hide whichever fields you chose when the "e-voucher" shipping method is chosen. Simply put the following inside your functions file.
<?php
add_filter('woocommerce_checkout_fields', 'evoucher_remove_fields');
function evoucher_remove_fields($fields) {
$shipping_method ='evoucher:1'; // Change this to the value name of your shipping method
global $woocommerce;
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ($chosen_shipping == $shipping_method) {
unset($fields['billing']['billing_address_1']); // Hides billing address line 1
unset($fields['billing']['billing_address_2']); // Hides billing address line 2
}
return $fields;
}
?>
Source
There is a good plugin for such purposes. Its free version includes conditional logic, e.g. change the form with the base on shipping option.

Set billing_address_2 label in Woocommerce checkout

I'm trying to set / show a label for the billing_address_2 field on the Woocommerce checkout page, but can't find a way to do this. Does anyone know a solution?
The code below (which works fine on other fields) does not do the job.
add_filter( 'woocommerce_checkout_fields' , 'custom_rename_wc_checkout_fields' );
function custom_rename_wc_checkout_fields( $fields ) {
$fields['billing']['billing_address_2']['label'] = 'Building number';
return $fields;
}
Updating this question with an answer for the sake of future readers.
As of WooCommerce 3.5.1, commit 87054ece9a4c05db72e139730ed1764c63fab635 adds the 'screen-reader-text' label_class to both Billing & Shipping Address 2 fields for accessibility purposes as per Issue #21182. This class will keep the label hidden unless it is also changed (e.g. make it blank, like the Address 1 field).
Here's how I updated my functions.php to get the billing/shipping_address_2 labels back (I'm using the separate billing/shipping filters as I have made changes to other fields I didn't include in the code for brevity).
// Billing Fields.
add_filter( 'woocommerce_billing_fields', 'custom_woocommerce_billing_fields' );
function custom_woocommerce_billing_fields( $fields ) {
$fields['billing_address_2']['label'] = 'Address 2';
$fields['billing_address_2']['label_class'] = '';
return $fields;
}
// Shipping Fields.
add_filter( 'woocommerce_shipping_fields', 'custom_woocommerce_shipping_fields' );
function custom_woocommerce_shipping_fields( $fields ) {
$fields['shipping_address_2']['label'] = 'Address 2';
$fields['shipping_address_2']['label_class'] = '';
return $fields;
}

woocommerce check zip code before placing order

As local delivery is the only option (due to product delivery restrictions) I do not want a customer to get to the checkout page and have to fill out all their details and only then discover we do not deliver to their postcode.
Therefore, I require the same functionality of the Local Delivery postcode check at the Checkout page, but to be added at an earlier stage in the checkout process, such as on the Cart page? Or any other page, for that matter. Best place can be in product page before add to cart option.
i.e.
Enter your postcode to see if we deliver to your area:
Result - a yes or no message appears with further instructions
You can add a new field to the cart by using the woocommerce_cart_coupon hook and then you can create a handler using the template_redirect hook.
Something like the below which we have used on our sites before:
add_action( 'woocommerce_cart_coupon', array(&$this, 'new_woocommerce_cart_coupon'), 10, 0 );
add_action( 'template_redirect', array(&$this, 'new_post_code_cart_button_handler') );
public function new_woocommerce_cart_coupon() {
?>
<br/><br/><p>Enter your postcode</p><label for="post_code">Post Code</label> <input type="text" name="post_code" class="input-text" id="post_code" value="" /> <input type="submit" class="button" name="apply_post_code" value="Check Post Code" />
<?php
}
public function new_post_code_cart_button_handler() {
if( is_cart() && isset( $_POST['post_code'] ) && $_SERVER['REQUEST_METHOD'] == "POST" && !empty( $_POST['post_code'] ) ) {
//validate post code here
}
}
Sounds like your best option is to put the PostCode field at the top of your Billing Details form.
This way, once the PostCode is filled up, the shipping methods will adjust accordingly. As soon as the user goes down the form, the Local Shipping method will no longer be available if their postcode don't allow it.
This solution will only work if you place all the postcodes that you actually deliver to in the PostCode section in the Local Delivery settings in the WooCommerce dashboard:
This will ensure that Local Delivery option will only appear to the postcodes in your list. If the postcode entered is not on the list, the Local Delivery option will disappear on the shipping methods options below the form.
Just add this to your functions.php file:
//Rearrange the Fields in the Checkout Billing Details Form
add_filter("woocommerce_checkout_fields", "new_order_fields");
function new_order_fields($fields) {
$order_list = array(
"billing_postcode",
"billing_first_name",
"billing_last_name",
"billing_email",
"billing_phone",
"billing_company",
"billing_address_1",
"billing_address_2",
"billing_country"
);
foreach($order_list as $field)
{
$ordered_fields[$field] = $fields["billing"][$field];
}
$fields["billing"] = $ordered_fields;
return $fields;
}
Just rearrange the $order_list array according to your preference.

Resources