WooCommerce order comments - disabling use of special characters - woocommerce

I'm using the following code to restrict the WooCommerce checkout inputs to alphabetical characters only (thanks to this post: Alphabet characters only for billing and shipping names in WooCommerce). However, this also marks each of the checkout fields as 'required' so for example even if the customer doesn't want to add any order comments, they will have to add something to complete the order. How can I edit this so that all of the fields are not required? Help much appreciated!
/* remove special charaters from checkout ? */
add_action('woocommerce_checkout_process', 'wh_alphaCheckCheckoutFields');
function wh_alphaCheckCheckoutFields() {
$billing_first_name = filter_input(INPUT_POST, 'billing_first_name');
$billing_last_name = filter_input(INPUT_POST, 'billing_last_name');
$order_comments = filter_input(INPUT_POST, 'order_comments');
$shipping_first_name = filter_input(INPUT_POST, 'shipping_first_name');
$shipping_last_name = filter_input(INPUT_POST, 'shipping_last_name');
$ship_to_different_address = filter_input(INPUT_POST, 'ship_to_different_address');
if (empty(trim($billing_first_name)) || !ctype_alpha($billing_first_name)) {
wc_add_notice(__('Only alphabets are alowed in <strong>Billing First
Name</strong>.'), 'error');
}
if (empty(trim($billing_last_name)) || !ctype_alpha($billing_last_name)) {
wc_add_notice(__('Only alphabets are alowed in <strong>Billing Last
Name</strong>.'), 'error');
}
if (empty(trim($order_comments)) || !ctype_alpha($order_comments)) {
wc_add_notice(__('Only alphabets are alowed in <strong>Delivery
Instructions</strong>.'), 'error');
}
// 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($shipping_first_name)) || !ctype_alpha($shipping_first_name)) {
wc_add_notice(__('Only alphabets are alowed in <strong>Shipping First
Name</strong>.'), 'error');
}
if (empty(trim($shipping_last_name)) || !ctype_alpha($shipping_last_name)) {
wc_add_notice(__('Only alphabets are alowed in <strong>Shipping Last
Name</strong>.'), 'error');
}
}
}

Change this condition
if (empty(trim($order_comments)) || !ctype_alpha($order_comments)) {
To this
if ( !empty( trim( $order_comments ) ) && !ctype_alpha( $order_comments ) ) {
Complete code
/* remove special charaters from checkout ? */
add_action('woocommerce_checkout_process', 'wh_alphaCheckCheckoutFields');
function wh_alphaCheckCheckoutFields() {
$billing_first_name = filter_input(INPUT_POST, 'billing_first_name');
$billing_last_name = filter_input(INPUT_POST, 'billing_last_name');
$order_comments = filter_input(INPUT_POST, 'order_comments');
$shipping_first_name = filter_input(INPUT_POST, 'shipping_first_name');
$shipping_last_name = filter_input(INPUT_POST, 'shipping_last_name');
$ship_to_different_address = filter_input(INPUT_POST, 'ship_to_different_address');
if ( empty( trim( $billing_first_name ) ) || !ctype_alpha( $billing_first_name ) ) {
wc_add_notice(__('Only alphabets are alowed in <strong>Billing First Name</strong>.'), 'error');
}
if ( empty( trim( $billing_last_name ) ) || !ctype_alpha( $billing_last_name )) {
wc_add_notice(__('Only alphabets are alowed in <strong>Billing Last Name</strong>.'), 'error');
}
if ( !empty( trim( $order_comments ) ) && !ctype_alpha( $order_comments ) ) {
wc_add_notice(__('Only alphabets are alowed in <strong>Delivery Instructions</strong>.'), 'error');
}
// 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( $shipping_first_name ) ) || !ctype_alpha( $shipping_first_name ) ) {
wc_add_notice(__('Only alphabets are alowed in <strong>Shipping First Name</strong>.'), 'error');
}
if ( empty( trim( $shipping_last_name ) ) || !ctype_alpha( $shipping_last_name ) ) {
wc_add_notice(__('Only alphabets are alowed in <strong>Shipping Last Name</strong>.'), 'error');
}
}
}

Related

Hide "ship to a different address" in Woocommerce based on user roles and specific products in cart

I'm trying to hide the "ship to a different address" on WooCommerce checkout page for 2 user roles, if their cart contains any of the specified product ID's.
My code is working for a single product ID:
/** Shipping Address */
function filter_woocommerce_cart_needs_shipping_address( $needs_shipping ) {
// The targeted products & roles
$targeted_variation_id = 414;
$targeted_user_role = 'team';
$targeted_user_role2 = 'team2';
// Flag
$found = false;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( in_array( $targeted_variation_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$found = true;
break;
}
}
// True & user Role is
if ( $found && current_user_can( $targeted_user_role )) {
$needs_shipping = false;
}
// True & only 1 item in cart
if ( $found && current_user_can( $targeted_user_role2 )) {
$needs_shipping = false;
}
return $needs_shipping;
}
add_filter( 'woocommerce_cart_needs_shipping_address', 'filter_woocommerce_cart_needs_shipping_address', 10, 1 );
But then when I try to create an array of multiple IDs, I can't achieve the desired result, this is my attempt:
**Multiple Variants**
/** Shipping Address */
function filter_woocommerce_cart_needs_shipping_address( $needs_shipping ) {
// The targeted products & roles
$targeted_variation_id = array(414,617);
$targeted_user_role = 'team';
$targeted_user_role2 = 'team2';
// Flag
$found = false;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( in_array( $targeted_variation_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$found = true;
break;
}
}
// True & user Role is
if ( $found && current_user_can( $targeted_user_role )) {
$needs_shipping = false;
}
// True & only 1 item in cart
if ( $found && current_user_can( $targeted_user_role2 )) {
$needs_shipping = false;
}
return $needs_shipping;
}
add_filter( 'woocommerce_cart_needs_shipping_address', 'filter_woocommerce_cart_needs_shipping_address', 10, 1 );
Anyone able to help figure out how to get it to work with multiple ID's and not just one?
The mistakes in your code is in the following line:
if ( in_array( $targeted_variation_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) )
$targeted_variation_id has been changed to an array,
while the searched value must be a string when using in_array()
It also seems more logical to check the user role first, before iterate all cart items
So you get:
function filter_woocommerce_cart_needs_shipping_address( $needs_shipping ) {
// Retrieve the current user object
$user = wp_get_current_user();
// Add your user roles, several can be entered, separated by a comma
$user_roles = array( 'team1', 'team2', 'administrator' );
// Targeted product IDs, several can be entered, separated by a comma
$targeted_product_ids = array( 30, 813, 414, 617 );
// User role is found
if ( array_intersect( $user_roles, (array) $user->roles ) ) {
// Loop through cart contents
foreach ( WC()->cart->get_cart_contents() as $cart_item ) {
// Get product ID
$product_id = $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : $cart_item['product_id'];
// Checks if a value exists in an array
if ( in_array( $product_id, $targeted_product_ids ) ) {
$needs_shipping = false;
break;
}
}
}
return $needs_shipping;
}
add_filter( 'woocommerce_cart_needs_shipping_address', 'filter_woocommerce_cart_needs_shipping_address', 10, 1 );

State restriction

I have some items that we are not allowed to send to California, how can I restrict people who have "CA" in their address, Woocomerce cart? I have this code but for some reason, it doesn't work
// REMOVE ITEMS THAT CAN'T SHIP TO CALIFORNIA/INTERNATIONAL
if( !empty($shipping_state) && $shipping_state == 'CA'){
$removed_item = false;
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( has_term('not-carb-compliant-cant-ship-to-california', 'product_tag', $cart_item['product_id'] ) ) {
WC()->cart->remove_cart_item($cart_item_key);
$removed_an_item = true;
}
}
if ( $removed_an_item ) {
$notice = 'This tem is not currently available in your region and has been removed from your cart.';
wc_print_notice( $notice , 'error' );
}
}
}

WooCommerce Hide Payment Gateway for Custom Attribute

Just starting in my WordPress/WooCommerce journey.
I found a question on here from a while back asking about restricting payment gateways for a specific attribute selection. I've tried to integrate this into my site and I cannot get it working! I've added the Attribute and Terms, and the slugs are as follows - pay_now_deposit (attr) and pay_now / deposit (terms).
I'm looking to restrict Klarna payments for those paying by deposit, and the code I have is below -
function conditional_payment_gateways( $available_gateways ) {
$in_cart = false;
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
// See if there is an attribute called 'pa_size' in the cart
// Replace with whatever attribute you want
if (array_key_exists('pa_pay_now_deposit', (array) $values['data']->get_attributes() ) ) {
foreach ($values['data']->get_attributes() as $attribute => $variation);
// Replace 'small' with your value.
if ($variation == 'deposit') $in_cart = true; //edited
}
}
if ( $in_cart ) {
unset($available_gateways['klarna_payments']);
}
else {
unset($available_gateways['cod']);
}
return $available_gateways;
}
Any advice on where I'm going wrong would be greatly appreciated!
Try to add a break statement.
add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);
function conditional_payment_gateways( $available_gateways ) {
$in_cart = false;
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
if ( array_key_exists( 'pa_pay_now_deposit', (array) $values['data']->get_attributes() ) ) {
foreach ( $values['data']->get_attributes() as $attribute => $variation ){
if ( $variation == 'deposit' ){
$in_cart = true; //edited
break;
}
}
}
if( $in_cart ){
break;
}
}
if ( $in_cart ) {
unset( $available_gateways['klarna_payments'] );
}else {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}

Wordpress increase number of previously approved comment

In the Wordpress -> Settings -> Discussion there a parameter called "Comment author must have a previously approved comment"
The users must be have a comment approved before to auto approving the after comments. How can I increase this parameter, this mean the users must be have 5 comment approved before?
Thanks
Use below code snippet in your functions.php
based on the value of Comment author must have a previously approved comment, it will check whether the user has 5 commented approved or not. If not then it will return false otherwise it will return true.
function pre_comment_approved_callback($approved, $commentdata){
global $wpdb;
$author = $commentdata['comment_author'];
$email = $commentdata['comment_author_email'];
$comment_type = $commentdata['comment_type'];
$mod_keys = trim( get_option( 'moderation_keys' ) );
if ( 1 == get_option( 'comment_previously_approved' ) ) {
if ( 'trackback' !== $comment_type && 'pingback' !== $comment_type && '' !== $author && '' !== $email ) {
$comment_user = get_user_by( 'email', wp_unslash( $email ) );
if ( ! empty( $comment_user->ID ) ) {
$ok_to_comment = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_approved) FROM $wpdb->comments WHERE user_id = %d AND comment_approved = '1' LIMIT 5", $comment_user->ID ) );
} else {
// expected_slashed ($author, $email)
$ok_to_comment = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_approved) FROM $wpdb->comments WHERE comment_author = %s AND comment_author_email = %s and comment_approved = '1' LIMIT 5", $author, $email ) );
}
if ( ( 5 == $ok_to_comment ) &&
( empty( $mod_keys ) || false === strpos( $email, $mod_keys ) ) ) {
return true;
} else {
return false;
}
} else {
return false;
}
}
return $approved;
}
add_filter('pre_comment_approved', 'pre_comment_approved_callback', 10, 2);

Yoast normal title and meta description no longer works when using wpseo_title and wpseo_metadesc

The codes below works in changing title and meta description by query strings. There was only one page with 3 query strings. The other pages do not have query strings. With the codes in functions.php, the other pages Yoast title and metadescription does not work properly. Without the codes, Yoast title and meta description works properly. Is there a way to make the codes work while still using Yoast title and meta description?
/* Changing title and meta description by query strings */
function yoast_add_title (){
if ( ! empty( $_GET[ 'q' ] ) && $_GET['q'] == 'medical' ) {
$title = 'Health';
}
if ( ! empty( $_GET[ 'q' ] ) && $_GET['q'] == 'wealth' ) {
$title = 'Financial';
}
if ( ! empty( $_GET[ 'q' ] ) && $_GET['q'] == "retirement" ) {
$title = 'Wealth';
}
return $title;
}
function yoast_add_metadesc (){
if ( ! empty( $_GET[ 'q' ] ) && $_GET['q'] == 'medical' ) {
$metadesc = 'Health';
}
if ( ! empty( $_GET[ 'q' ] ) && $_GET['q'] == 'wealth' ) {
$metadesc = 'Financial';
}
if ( ! empty( $_GET[ 'q' ] ) && $_GET['q'] == "retirement" ) {
$metadesc = 'Wealth';
}
return $metadesc;
}
add_filter( 'wpseo_title', 'yoast_add_title', 10, 1);
add_filter( 'wpseo_metadesc', 'yoast_add_metadesc', 10, 1);
The code below works in functions.php instead. Somehow I need to target the exact page id and use template_redirect.
function yoast_add_title (){
if ( $_GET['q'] == 'medical' ) {
$title = 'Health';
}
if ( $_GET['q'] == 'wealth' ) {
$title = 'Financial';
}
if ( $_GET['q'] == 'retirement' ) {
$title = 'Wealth';
}
return $title;
}
function yoast_add_metadesc (){
if ( $_GET['q'] == 'medical' ) {
$metadesc = 'Health';
}
if ( $_GET['q'] == 'wealth' ) {
$metadesc = 'Financial';
}
if ( $_GET['q'] == 'retirement' ) {
$metadesc = 'Wealth';
}
return $metadesc;
}
function yoast_add_all (){
if ( is_page(50) && ( ! empty( $_GET[ 'q' ] ) ) ) {
add_filter( 'wpseo_title', 'yoast_add_title', 10, 1 );
add_filter( 'wpseo_metadesc', 'yoast_add_metadesc', 10, 1 );
}
}
add_action( 'template_redirect', "yoast_add_all" );

Resources