I have an requirement that billing phone in woocommerce is validate like email. Whenever we want to create an account in woocommerce with same email id, it throws an error email already exists. I want same this feature in billing phone.
this working for me
change wp_usermeta with your table name
add_action( 'woocommerce_register_form', 'wooc_extra_register_fields' );
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
global $wpdb;
$billing_phone =$_POST['billing_phone'];
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$validation_errors->add( 'billing_first_name_error', __( ' First name is required!', 'woocommerce' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$validation_errors->add( 'billing_last_name_error', __( ' Last name is required!.', 'woocommerce' ) );
}
if ( isset($billing_phone ) && empty( $billing_phone ) ) {
$validation_errors->add( 'billing_phone_error', __( ' Prone is required!.', 'woocommerce' ) );
}
$results = $wpdb->get_results('select * from `wp_usermeta` where meta_key = "billing_phone" and meta_value = "'.$billing_phone.'"');
if ( $results ) {
$validation_errors->add( 'billing_phone_error', __( 'Phone number already exists..', 'woocommerce' ) );
}
return $validation_errors;
}
This will be your solution :
add_filter('woocommerce_new_customer_data', 'risbl_custom_customer_data', 10 );
function risbl_custom_customer_data() {
global $wpdb;
$billing_phone = $_POST['billing_phone'];
$results = $wpdb->get_results('select * from `wp_usermeta` where meta_key = "billing_phone" and meta_value = "'.$billing_phone.'"');
if ( $results ) {
wc_add_notice( __( 'Phone number already exists.' ), 'error' );
}
}
Try this:
function wc_validate_phone_number() {
$phone = (isset( $_POST['billing_phone'] ) ? trim(
$_POST['billing_phone'] ) : '');
if ( ! preg_match( '/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/',
$phone ) ) {
wc_add_notice( __( 'Invalid Phone Number. Please enter with a valid
phone number. Eg: (123) 456 7890' ), 'error' );
}
}
Code Works Perfect:
add_filter('woocommerce_billing_fields',
'phone', 10 );
function phone() {
global $wpdb;
$billing_phone = $_POST['billing_phone'];
$results = $wpdb->get_results('select * from `wp_usermeta` where meta_key = "billing_phone" and meta_value = "'.$billing_phone.'"');
if ( $results ) {
wc_add_notice( __( 'Phone number already exists.' ), 'error' );
}
}
Related
I'd like to add a field on Check Payment that allows the order processor to insert a check number...please help! Check payment orders will only be entered by staff, not the public.
Add Field Here
I've searched all over and I'm stumped.
As per my understanding of your question, I have created this snippet which will help you with your requirement.
// add a field on the check payment type
add_filter( 'woocommerce_gateway_description', 'custom_check_payment_field', 10, 2 );
function custom_check_payment_field( $description, $payment_id ) {
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$current_user_role = (array) $user->roles;
if ( 'cheque' === $payment_id && $current_user_role[0] == 'administrator') {
ob_start();
echo '<div class="cheque-field">';
woocommerce_form_field(
'customer_cheque_number',
array(
'type' => 'text',
'label' => __( 'Enter cheque number', 'woocommerce' ),
'class' => array( 'form-row-wide' ),
'required' => true,
),
''
);
echo '<div>';
$description .= ob_get_clean();
}
}
return $description;
}
// save check number in meta/database
add_action( 'woocommerce_checkout_create_order', 'save_customer_cheque_number_order_meta', 10, 2 );
function save_customer_cheque_number_order_meta( $order, $data ) {
if ( isset( $_POST['customer_cheque_number'] ) && ! empty( $_POST['customer_cheque_number'] ) ) {
$order->update_meta_data( '_customer_cheque_number', esc_attr( $_POST['customer_cheque_number'] ) );
}
}
// show check number on orders page (OPTONAL)
add_action( 'woocommerce_get_order_item_totals', 'display_customer_cheque_number_on_order_totals', 10, 3 );
function display_customer_cheque_number_on_order_totals( $total_rows, $order, $tax_display ) {
if ( $order->get_payment_method() === 'cheque' && $customer_cheque_number = $order->get_meta( '_customer_cheque_number' ) ) {
$sorted_total_rows = array();
foreach ( $total_rows as $key_row => $total_row ) {
$sorted_total_rows[ $key_row ] = $total_row;
if ( $key_row === 'payment_method' ) {
$sorted_total_rows['customer_cheque_number'] = array(
'label' => __( 'Cheque number', 'woocommerce' ),
'value' => esc_html( $customer_cheque_number ),
);
}
}
$total_rows = $sorted_total_rows;
}
return $total_rows;
}
// add meta box on orders on woocommerce backend
add_action( 'add_meta_boxes', 'add_meta_boxes_callback' );
if ( ! function_exists( 'add_meta_boxes_callback' ) ) {
function add_meta_boxes_callback() {
add_meta_box( 'shop_order_fields', __( 'Cheque number', 'woocommerce' ), 'orders_cheque_number_custom_meta', 'shop_order', 'side', 'core' );
}
}
// show meta field with entered check number
if ( ! function_exists( 'orders_cheque_number_custom_meta' ) ) {
function orders_cheque_number_custom_meta() {
global $post;
$check_number = get_post_meta( $post->ID, '_customer_cheque_number', true ) ? get_post_meta( $post->ID, '_customer_cheque_number', true ) : '';
?>
<input type="text" style="width:250px;margin-top:15px" readonly name="readonly_check_number" value="<?php echo $check_number; ?>"></p>
<?php
}
}
I am trying to change the in stock text next to the quantity available in woocommerce. I am using the stock management in product variations.
I tried this code below:
// change stock text
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $variation ) {
// Change In Stock Text
if ( $variation->is_in_stock() ) {
$availability['availability'] = __('Available!', 'woocommerce');
}
// Change Out of Stock Text
if ( ! $variation->is_in_stock() ) {
echo '-------------------------';
echo __('Sold Out', 'woocommerce');
$availability['availability'] = __('Sold Out', 'woocommerce');
}
return $availability;
}
The code above changes the text but it does not pull in the stock quantity number from the variation stock manager.
The following code will handle all cases including the stock amount display with your custom texts:
add_filter( 'woocommerce_get_availability_text', 'customizing_stock_availability_text', 1, 2);
function customizing_stock_availability_text( $availability, $product ) {
if ( ! $product->is_in_stock() ) {
$availability = __( 'Sold Out', 'woocommerce' );
}
elseif ( $product->managing_stock() && $product->is_on_backorder( 1 ) )
{
$availability = $product->backorders_require_notification() ? __( 'Available on backorder', 'woocommerce' ) : '';
}
elseif ( $product->managing_stock() )
{
$availability = __( 'Available!', 'woocommerce' );
$stock_amount = $product->get_stock_quantity();
switch ( get_option( 'woocommerce_stock_format' ) ) {
case 'low_amount' :
if ( $stock_amount <= get_option( 'woocommerce_notify_low_stock_amount' ) ) {
/* translators: %s: stock amount */
$availability = sprintf( __( 'Only %s Available!', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_amount, $product ) );
}
break;
case '' :
/* translators: %s: stock amount */
$availability = sprintf( __( '%s Available!', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_amount, $product ) );
break;
}
if ( $product->backorders_allowed() && $product->backorders_require_notification() ) {
$availability .= ' ' . __( '(can be backordered)', 'woocommerce' );
}
}
else
{
$availability = '';
}
return $availability;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
I have the following custom form with code added to the functions.php file:
* ----------------------------------Checkout Field Stor data Start----------------------------------*/
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['account_per_title'] ) ) {
update_post_meta( $order_id, ' Account Title', sanitize_text_field( $_POST['account_per_title'] ) );
}
if ( ! empty( $_POST['account_fname'] ) ) {
update_post_meta( $order_id, 'Account First Name', sanitize_text_field( $_POST['account_fname'] ) );
}
if ( ! empty( $_POST['account_mname'] ) ) {
update_post_meta( $order_id, 'Account Middle Name', sanitize_text_field( $_POST['account_mname'] ) );
}
if ( ! empty( $_POST['account_lname'] ) ) {
update_post_meta( $order_id, 'Account Last Name', sanitize_text_field( $_POST['account_lname'] ) );
}
And here in the template I want to display the name of the person filling in the order:
<?php /* translators: %s: Customer first name */ ?>
<p><?php printf( esc_html__( 'Hi %s,', 'woocommerce' ), esc_html ($order->get_billing_first_name() ) ); ?></p>
<?php /* translators: %s: Order number */ ?>
<p><?php printf( esc_html__( 'Just to let you know — we\'ve received your order #%s, and it is now being processed:', 'woocommerce' ), esc_html( $order->get_order_number() ) ); ?></p>
Can anyone help with a solution?
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'processing_order_replacement_email_recipient', 10, 2 );
function processing_order_replacement_email_recipient( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
// Set HERE your replacement recipient email(s)… (If multiple, separate them by a coma)
$recipient = get_post_meta( $order->id, 'Account Email', true ) ;
return $recipient;
}
I am starting to study action and filter in wordpress. Lets say i wanna add DIV wrap to the output of printif at the bottom of code. Can i just copy all this code into my functions.php and edit some of its codes?
defined( 'YITH_WOOCOMPARE' ) || exit; // Exit if accessed directly.
if ( ! class_exists( 'YITH_Woocompare_Frontend' ) ) {
/**
* YITH Custom Login Frontend
*
* #since 1.0.0
*/
class YITH_Woocompare_Frontend {
public function __construct() {
// Add link or button in the products list.
if ( 'yes' === get_option( 'yith_woocompare_compare_button_in_product_page', 'yes' ) ) {
add_action( 'woocommerce_single_product_summary', array( $this, 'add_compare_link' ), 35 );
}
if ( 'yes' === get_option( 'yith_woocompare_compare_button_in_products_list', 'no' ) ) {
add_action( 'woocommerce_after_shop_loop_item', array( $this, 'add_compare_link' ), 20 );
}
return $this;
}
public function add_compare_link( $product_id = false, $args = array() ) {
extract( $args ); // phpcs:ignore
if ( ! $product_id ) {
global $product;
$product_id = ! is_null( $product ) ? $product->get_id() : 0;
}
// Return if product doesn't exist.
if ( empty( $product_id ) || apply_filters( 'yith_woocompare_remove_compare_link_by_cat', false, $product_id ) ) {
return;
}
$is_button = ! isset( $button_or_link ) || ! $button_or_link ? get_option( 'yith_woocompare_is_button', 'button' ) : $button_or_link;
if ( ! isset( $button_text ) || 'default' === $button_text ) {
$button_text = get_option( 'yith_woocompare_button_text', __( 'Compare', 'yith-woocommerce-compare' ) );
do_action( 'wpml_register_single_string', 'Plugins', 'plugin_yit_compare_button_text', $button_text );
$button_text = apply_filters( 'wpml_translate_single_string', $button_text, 'Plugins', 'plugin_yit_compare_button_text' );
}
printf( '%s', $this->add_product_url( $product_id ), 'compare' . ( 'button' === $is_button ? ' button' : '' ), $product_id, $button_text ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
}
I am facing the issue that I want to search for every product IDs including product variants in woocommerce admin. How to search product by variation id in woocommerce admin ?
can you please help me?
Thanks in advance.
Finally I have implement the above functionality by modifying the filter "posts_search".
By using bellow code you can search variable products by their variations id in woocommerce admin product search area.
add_filter( 'posts_search', 'product_search' );
function product_search( $where ) {
global $pagenow, $wpdb, $wp;
if ( 'edit.php' != $pagenow || ! is_search() || ! isset( $wp->query_vars['s'] ) || 'product' != $wp->query_vars['post_type'] ) {
return $where;
}
$search_ids = array();
$terms = explode( ',', $wp->query_vars['s'] );
foreach ($terms as $term){
if (is_numeric($term)){
$post_type = get_post_type( $term );
if($post_type == 'product_variation'){
$search_ids[] = wp_get_post_parent_id($term);
}else{
$search_ids[] = $term;
}
}
// Attempt to get a SKU
$sku_to_id = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_parent FROM {$wpdb->posts} LEFT JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id WHERE meta_key='_sku' AND meta_value LIKE %s;", '%' . $wpdb->esc_like( wc_clean( $term ) ) . '%' ) );
$sku_to_id = array_merge( wp_list_pluck( $sku_to_id, 'ID' ), wp_list_pluck( $sku_to_id, 'post_parent' ) );
if (sizeof($sku_to_id) > 0) {
$search_ids = array_merge($search_ids, $sku_to_id);
}
}
$search_ids = array_filter( array_unique( array_map( 'absint', $search_ids ) ) );
if ( sizeof( $search_ids ) > 0 ) {
$where = str_replace( 'AND (((', "AND ( ({$wpdb->posts}.ID IN (" . implode( ',', $search_ids ) . ")) OR ((", $where );
}
return $where;
}