Hide billing_postcode field based on shipping methods in woocommerce - wordpress

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.

Related

Change Shipping Address title based on selected shipping method in Woocommerce

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

Hide & refresh WooCommerce checkout after add coupon

We want to hide "Have a coupon? Add one..." on WooCommerce checkout, if a coupon is already added OR when a customer add a coupon on the checkout page.
Currently we habe this code below and it works when a customer enters a coupon on the cart page and then navigate to the checkout page. In this case, the "Have a coupon? Add one..." message is not visible. If no coupon in added on the cart page the message is visible.
This is working fine! But it does not work when a customer adds a coupon on the checkout page.
1.) We get the message "Coupon added" But the coupon message to add one is still visible AND also the coupon is not calculated in the order table. => After a page refresh is everything correctly.
2.) When a coupon is removed by the customer on checkout, then we get the message that the coupon is removed, but the discount is still visible in the order table. => After a page refresh it displays everything right again.
So now I'm trying to refresh the page after a coupon was added or removed. But I have problems to get the right event. I guess we must do this via js? Or is there a PHP approach?
add_filter( 'woocommerce_coupons_enabled', 'woocommerce_coupons_enabled_checkout' );
function woocommerce_coupons_enabled_checkout( $coupons_enabled ) {
global $woocommerce;
if ( ! empty( $woocommerce->cart->applied_coupons ) ) {
return false;
}
return $coupons_enabled;
}
Your code should be like this
add_filter( 'woocommerce_coupons_enabled', 'woocommerce_coupons_enabled_checkout' );
function woocommerce_coupons_enabled_checkout( $coupons_enabled ) {
if(is_checkout()){
global $woocommerce;
if ( ! empty( $woocommerce->cart->get_applied_coupons() ) ) {
$coupons_enabled = false;
}
}
return $coupons_enabled;
}
Edit: okay you need to check if page is checkout or cart then run the script. I have added condition in code.

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

handling response and placing order in woocommerce after redirecting back from payment website

I am developing a payment gateway plugin (For Cybersource Payment) for WooCommerce. I developed it to almost at the end, but one thing is intercepting me to finish and that is the part after redirecting from payment gateway website after successful payment back to my wordpress page (Purchase Confirmation Page - New Wordpress Page created using template in wp-content/themes/my-theme/order-confirm-template.php). But i dont know how to handle the response coming back from payment website. I looked for some woocommerce hooks but nothing worked. I found there is a hook woocommerce_thankyou but that is also not working for me.
Can somebody help me out here in these two points below
1) How to handle response and placed the order properly and remove the items from the cart when coming back after successful payment from gateway website.
2) which page should i redirect back from gateway website? back to the same checkout page or some custom page just like i did.
Any Help with code will be very appreciated. Thanks.
I have used the following code in my plugin, I hope this works for you too :)
First add this code,
function receipt_page($order){
echo $this -> ResponceHandler($order);
}
Now the code for ResponceHandler($order) function,
public function ResponceHandler($order_id){
if(!isset($_POST['ResponseCode'])){
global $woocommerce;
echo '<p>'.__('Thank you for your order, please click the button below to pay with XYZ', 'woocommerce').'</p>';
$order = new WC_Order($order_id);
$order_id = $order_id.'_'.date("ymds");
$post_data = get_post_meta($order_id,'_post_data',true);
update_post_meta($order_id,'_post_data',array());
###Your Form Code HERE###
echo '<form><input value="Proceed To Payment" type="submit" /> </form>';
}
###Haandle the response###
if(isset($_POST['ResponseCode']))
{
if($_POST['ResponseCode']==0){
global $woocommerce;
session_start();
$_SESSION['post']=$_POST;
$order = new WC_Order($order_id);
$order_id = $order_id.'_'.date("ymds");
$post_data = get_post_meta($order_id,'_post_data',true);
update_post_meta($order_id,'_post_data',array());
if($order->status != 'processing'){
$order ->status ='Processing';
$order->payment_complete();
$order -> add_order_note('XYZ Payment Gateway <br>Response message :'.$_POST['ResponseMessage'].'<br>Payment ID :'.$_POST['PaymentID'].'<br>Merchant Reference Number :'.$_POST['MerchantRefNo'].'<br>Transaction ID :'.$_POST['TransactionID'].'');
add_post_meta( $order->id, '_paymentid', sanitize_text_field( $_POST['PaymentID'] ) );
add_post_meta( $order->id, '_trno', sanitize_text_field( $_POST['TransactionID'] ) );
$woocommerce -> cart -> empty_cart();
wp_redirect( $order->get_checkout_order_received_url());
}
}
else {
if($order->status != 'failed'){
$order ->status ='failed';
echo "Payment failed!<br><br><br>Possible Error : ".$_POST['ResponseMessage']."<br>PaymentID: ".$_POST['PaymentID']."<br><br><br>We request you to save these details for further reference. <br>You can always pay for this order by clicking on your name in the top right corner and visiting your orders section."; }
}
}
}

Resources