In woocommerce the checkout form (Town/City) not validating with this code.
// Check if Ship to a different address is set, if it's set then validate shipping fields.
if (!empty($ship_to_different_address)) {
if (empty(trim($billing_city)) || !ctype_alpha($billing_city)) {
wc_add_notice(__('Only alphabets are alowed in <strong>Shipping First Name</strong>.'), 'error');
}
if (empty(trim($shipping_city)) || !ctype_alpha($shipping_city)) {
wc_add_notice(__('Only alphabets are alowed in <strong>Shipping Last Name</strong>.'), 'error');
}
}
Something like this may do the trick...
function so_61559494_validate_checkout() {
if( ! empty( $_POST['ship_to_different_address'] ) ) {
$billing_city = ! empty( $_POST['billing_city'] ) ? trim( $_POST['billing_city'] ) : '';
if ( empty( $billing_city ) || ! ctype_alpha( $billing_city ) ) {
$notice = __('Only alphabets are alowed in <strong>Billing City</strong>.', 'your-text-domain' );
throw new Exception( $notice );
}
$shiping_city = ! empty( $_POST['shiping_city'] ) ? trim( $_POST['shiping_city'] ) : '';
if ( empty( $shipping_city ) || ! ctype_alpha( $shipping_city ) ) {
$notice = __('Only alphabets are alowed in <strong>Shipping City</strong>.', 'your-text-domain' );
throw new Exception( $notice );
}
}
}
add_action( 'woocommerce_checkout_process', 'so_61559494_validate_checkout' );
It's entirely untested though, so use with caution.
Related
What I am trying to do here is hide payments methods, based on the customer country.
Hide bacs and cheque, when the country is different than US.
When the country is US, hide cod payment method (this is working)
I been trying to do this, but it didn't work
Here is the code:
add_filter( 'woocommerce_available_payment_gateways', 'custom_payment_gateway_disable_country' );
function custom_payment_gateway_disable_country( $available_gateways ) {
if ( is_admin() ) return $available_gateways;
if ( isset( $available_gateways['bacs' && 'cheque'] ) && WC()->customer->get_billing_country() <> 'US' ) {
unset( $available_gateways['bacs' && 'cheque'] );
} else {
if ( isset( $available_gateways['cod'] ) && WC()->customer->get_billing_country() == 'US' ) {
unset( $available_gateways['cod'] );
}
}
return $available_gateways;
}
Can someone push me in the right direction? Any help will be appreciated!
Your if/else statements are wrong. As isset( $available_gateways['bacs' && 'cheque'] will never work.
See: PHP If Statement with Multiple Conditions
So you get:
function filter_woocommerce_available_payment_gateways( $payment_gateways ) {
// Not on admin
if ( is_admin() ) return $payment_gateways;
// Get country
$customer_country = WC()->customer->get_shipping_country() ? WC()->customer->get_shipping_country() : WC()->customer->get_billing_country();
// Country = US
if ( in_array( $customer_country, array( 'US' ) ) ) {
// Cod
if ( isset( $payment_gateways['cod'] ) ) {
unset( $payment_gateways['cod'] );
}
} else {
// Bacs & Cheque
if ( isset( $payment_gateways['bacs'] ) && isset( $payment_gateways['cheque'] ) ) {
unset( $payment_gateways['bacs'] );
unset( $payment_gateways['cheque'] );
}
}
return $payment_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );
How could I customise the order thank you page based on the order's shipping method? So for example if a customer used 'Delivery on request' option, the thank you page would display a different title.
add_filter( 'the_title', 'woo_personalize_order_received_title', 10, 2 );
function woo_personalize_order_received_title( $title, $id ) {
if ( is_order_received_page() && get_the_ID() === $id ) {
global $wp;
// Get the order. Line 9 to 17 are present in order_received() in includes/shortcodes/class-wc-shortcode-checkout.php file
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-received'] ) );
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key ) {
$order = false;
}
}
if ( isset ( $order ) ) {
$chosen_titles = array();
$available_methods = $wp->shipping->get_packages();
$chosen_rates = ( isset( $wp->session ) ) ? $wp->session->get( 'chosen_shipping_methods' ) : array();
foreach ($available_methods as $method)
foreach ($chosen_rates as $chosen) {
if( isset( $method['rates'][$chosen] ) ) $chosen_titles[] = $method['rates'][ $chosen ]->label;
}
if( in_array( 'Delivery price on request', $chosen_titles ) ) {
//$title = sprintf( "You are awesome, %s!", esc_html( $order->billing_first_name ) ); // use this for WooCommerce versions older then v2.7
$title = sprintf( "You are awesome, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
}
return $title;
}
is_order_received_page() doesn't exist. Instead use is_wc_endpoint_url( 'order-received' )…
Also $wp->session or $wp->shipping will not work. Instead you can find the chosen shipping method data in the order item "shipping".
Try this instead:
add_filter( 'the_title', 'customizing_order_received_title', 10, 2 );
function customizing_order_received_title( $title, $post_id ) {
if ( is_wc_endpoint_url( 'order-received' ) && get_the_ID() === $post_id ) {
global $wp;
$order_id = absint( $wp->query_vars['order-received'] );
$order_key = isset( $_GET['key'] ) ? wc_clean( $_GET['key'] ) : '';
if ( empty($order_id) || $order_id == 0 )
return $title; // Exit
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key )
return $title; // Exit
$method_title_names = array();
// Loop through Order shipping items data and get the method title
foreach ($order->get_items('shipping') as $shipping_method )
$method_title_names[] = $shipping_method->get_name();
if( in_array( 'Delivery price on request', $method_title_names ) ) {
$title = sprintf( "You are awesome, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
return $title;
}
Code goes on function.php file of your active child theme (or active theme). Tested and works.
Similar: Adding custom message on Thank You page by shipping method in Woocommerce
I have a wordpress site with woocommerce store, and I want to force users to register when purchasing specific products only.
How can I do it?
Thanks
Here is the code for your query.
add_action( 'woocommerce_after_checkout_validation' , 'restict_registration_for_some_products', 10, 2 );
function restict_registration_for_some_products( $data, $errors ) {
if( isset( $data['createaccount'] ) && !$data['createaccount'] ) {
$retricted_ids = get_resticted_product_ids();
if( isset( $retricted_ids ) && $retricted_ids != null ) {
$cart_content = WC()->cart->get_cart_contents();
$cart_ids = wp_list_pluck( $cart_content, 'product_id' );
$cart_ids = array_values( $cart_ids );
$common_ids = array_intersect( $retricted_ids, $cart_ids );
if( isset( $common_ids ) && $common_ids != null ) {
$errors->add( 'account_registration', __( 'You are not allowed to purchase these products without creating an account.', 'text-domain' ) );
}
}
}
}
function get_resticted_product_ids() {
//specific product ids
return array(110,96,70);
}
I am trying to enable and disable certain payment functions for certain countries in Woocommerce. I had placed the following code in my functions:
function payment_gateway_disable_by_country( $available_gateways ) {
// Abort if in admin area
if ( is_admin() ) {
return $available_gateways;
}
$billing_country = WC()->customer->get_country();
$shipping_country = ! empty( WC()->customer->get_shipping_country() ) ? WC()->customer->get_shipping_country() : $billing_country;
if ( isset( $available_gateways['invoice'] ) && $billing_country != 'DE' ) {
unset( $available_gateways['invoice'] );
}
if ( isset( $available_gateways['cod'] ) && $shipping_country != 'DE' ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways','payment_gateway_disable_by_country');
but then I am getting a white screen with the following error message:
Fatal error: Can't use method return value in write context in /home/www/mysite/html/wordpress/wp-content/themes/mytheme/functions.php on line 10.
Is there anything wrong with my code?
My registration form on WP only had the options Username, e-mail and password.
//1. firstname
add_action( 'register_form', 'myplugin_register_form' );
function myplugin_register_form() {
$first_name = ( ! empty( $_POST['first_name'] ) ) ? trim( $_POST['first_name'] ) : '';
?>
<?php
}
//2. Add validation. In this case, we make sure first_name is required.
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['first_name'] ) || ! empty( $_POST['first_name'] ) && trim( $_POST['first_name'] ) == '' ) {
$errors->add( 'first_name_error', __( '<strong>ERROR</strong>: You must include a first name.', 'mydomain' ) );
}
return $errors;
}
//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'myplugin_user_register' );
function myplugin_user_register( $user_id ) {
if ( ! empty( $_POST['first_name'] ) ) {
update_user_meta( $user_id, 'first_name', trim( $_POST['first_name'] ) );
}
}
In my registration form (template) I've placed the following:
<label><?php _e('First Name', APP_TD) ?></label>
<input tabindex="3" type="text" name="first_name" class="text regular-text" id="display_name" value="<?php echo esc_attr( wp_unslash( $first_name ) ); ?>" maxlength="100" />
And it works as it should. It grabs the first name and places it in the user profile. However I'm not sure on how to add the last name to it aswell, I'm quite a beginner, and for some reason I can't get the last name to work. Help would be much appreciated.
I've never worked with something like that on my WP. Where have you put this code?
However I would try to execute something like this:
//1. Fistname
add_action( 'register_form', 'myplugin_register_form' ); function myplugin_register_form() {
$first_name = ( ! empty( $_POST['first_name'] ) ) ? trim( $_POST['first_name'] ) : '';
$last_name = ( ! empty( $_POST['last_name'] ) ) ? trim( $_POST['last_name'] ) : '';
?>
<?php
}
//2. Add validation. In this case, we make sure first_name is required.
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['first_name'] ) || ! empty( $_POST['first_name'] ) && trim( $_POST['first_name'] ) == '' ) {
$errors->add( 'first_name_error', __( '<strong>ERROR</strong>: You must include a first name.', 'mydomain' ) );
}
if ( empty( $_POST['last_name'] ) || ! empty( $_POST['last_name'] ) && trim( $_POST['last_name'] ) == '' ) {
$errors->add( 'last_name_error', __( '<strong>ERROR</strong>: You must include a last name.', 'mydomain' ) );
}
return $errors;
}
//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'myplugin_user_register' );
function myplugin_user_register( $user_id ) {
if ( ! empty( $_POST['first_name'] ) ) {
update_user_meta( $user_id, 'first_name', trim( $_POST['first_name'] ));
}
if ( ! empty( $_POST['last_name'] ) ) {
update_user_meta( $user_id, 'last_name', trim( $_POST['last_name'] ) );
}
}