I am using a css to change the price view of the product with the code below. I found this to be the only advantageous working code. However, this code also appears on the product archive page and other components. I just want it to affect the price on the product page. And in any case, prices should appear in this view.
add_filter( 'woocommerce_get_price_html', 'bbloomer_simple_product_price_format', 10, 2 );
function bbloomer_simple_product_price_format( $price, $product ) {
if ( $product->is_on_sale() && $product->is_type('simple') ) {
$price = sprintf( __( '<div class="featured-price-container clickable"><div class="featured-price-box"><div class="featured-price-info"><div class="featured-price-discount"></div>Son 30 Günün En Düşük Fiyatı!</div><div class="featured-prices"><span class="prc-org">%1$s</span><span class="prc-dsc">%2$s</span></div></div></div>', 'woocommerce' ), wc_price ( $product->get_regular_price() ), wc_price( $product->get_sale_price() ), wc_price( $product->get_regular_price() - $product->get_sale_price() ) );
}
return $price;
}```
add_filter( 'woocommerce_get_price_html', 'bbloomer_simple_product_price_format', 10, 2 );
function bbloomer_simple_product_price_format( $price, $product ) {
if ( $product->is_on_sale() && $product->is_type('simple') ) {
$price = sprintf( __( '<div class="featured-price-container clickable"><div class="featured-price-box"><div class="featured-price-info"><div class="featured-price-discount"></div>Son 30 Günün En Düşük Fiyatı!</div><div class="featured-prices"><span class="prc-org">%1$s</span><span class="prc-dsc">%2$s</span></div></div></div>', 'woocommerce' ), wc_price ( $product->get_regular_price() ), wc_price( $product->get_sale_price() ), wc_price( $product->get_regular_price() - $product->get_sale_price() ) );
}
return $price;
}
I have already inserted this snippet to show an additional column with an icon if there are customer notes in the order. it works.
add_action( 'wp_enqueue_scripts', 'mini_enqueue_scripts' );
add_filter( 'manage_shop_order_posts_columns', 'woocommerce_add_order_notes_column', 99 );
function woocommerce_add_order_notes_column( $columns ) {
$columns['order_notes'] = __('Customer note', 'woocommerce');
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'woocommerce_show_order_notes_column', 10, 2 );
function woocommerce_show_order_notes_column( $column_name, $order_id ) {
switch ( $column_name ) {
case 'order_notes':
$order = wc_get_order( $order_id );
$note = $order->get_customer_note();
if ( !empty($note) ) {
echo '<span class="note-on tips" data-tip="' . wc_sanitize_tooltip( $note ) . '">' . __( 'Yes', 'woocommerce' ) . '</span>';
} else {
echo '<span class="na">–</span>';
}
break;
}
}
How can I create another column (if the VAT number has been entered in the order) on the side that does the same thing?
For that I have two additional fields on the checkout page: "vat number" (billing_piva) and "CID number" (billing_cid). I would like to have them together on the same column with two headers first, like:
"Vat Number: xxx" and "CID Number: xxx"
Any advice?
To add 2 columns versus 1 you can actually apply the same as you already did for adding 1 column
Adding the VAT number to the order can be done in different ways, from your question I understand that this is a custom checkout field with the meta key: billing_piva
Note: the use of add_action( 'wp_enqueue_scripts', 'mini_enqueue_scripts' ); is not necessary
So you get:
// Display on order admin list (header)
function filter_manage_edit_shop_order_columns( $columns ) {
// Add columns
$columns['order_notes'] = __( 'Customer note', 'woocommerce' );
$columns['order_vat'] = __( 'VAT number', 'woocommerce' );
return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );
// Display on order admin list (populate the column)
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {
// Get order
$order = wc_get_order( $post_id );
// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
// Compare
switch ( $column ) {
case 'order_notes':
// Get customer note
$note = $order->get_customer_note();
// NOT empty
if ( ! empty( $note ) ) {
echo '<span class="note-on tips" data-tip="' . wc_sanitize_tooltip( $note ) . '">' . __( 'Yes', 'woocommerce' ) . '</span>';
} else {
echo '<span class="na">–</span>';
}
break;
case 'order_vat':
// Get VAT (if necessary, adjust to the correct meta key)
$vat_number = $order->get_meta( 'billing_piva' );
// NOT empty
if ( ! empty( $vat_number ) ) {
// Output
$output = '<span>' . sprintf( __( 'VAT Number: %s', 'woocommerce' ), $vat_number ) . '</span>';
// Get CID number
$cid_number = $order->get_meta( 'billing_cid' );
// NOT empty
if ( ! empty ( $cid_number ) ) {
// Concatenation
$output .= '<br><span>' . sprintf( __( 'CID Number: %s', 'woocommerce' ), $cid_number ) . '</span>';
}
// Print
echo $output;
} else {
echo '<span class="na">–</span>';
}
}
}
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );
I'm trying to check with my ERP if a product is in stock. So when you clic add to cart, I run a code checks if is in stock and then remove it if is out of stock. I want to show a message that said the product was remove cause outstock.
function custom_validate_stock() {
if(1==1){
$stockERP = 0;
if($stockERP < 1){
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product->id ){
WC()->cart->remove_cart_item($cart_item_key);
$removed = true;
break;
}
}
}
}
if($removed){
remove_action( 'woocommerce_cart_is_empty', 'wc_empty_cart_message', 10 );
add_action( 'woocommerce_cart_is_empty', 'custom_empty_cart_message', 10 );
}
}
}
function custom_empty_cart_message() {
$html = '<div class="col-12 offset-md-1 col-md-10"><p class="cart-empty">';
$html .= wp_kses_post( apply_filters( 'wc_empty_cart_message', __( 'Your product is out of stock, please refresh the page.', 'woocommerce' ) ) );
echo $html . '</p></div>';
}
Right after
WC()->cart->remove_cart_item($cart_item_key);
I think you could add a notice:
wc_add_notice( __( 'Thing removed.', 'your-textdomain' ), 'error' );
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' );
}
}
When click on add to cart button, the Woocommerce shows the message, view cart, I want to edit this message, actually edit all the span, put some icon etc...
Add a filter to your theme/functions.php. The code below just overrides the existing $message. This overwrites $message with an nearly identical one that prepends a "checkout" link to the message.
Make sure you return the $message.
You can of course just modify the existing message, as the entire thing is passed as a string via the first param or $message var.
add_filter ( 'wc_add_to_cart_message', 'wc_add_to_cart_message_filter', 10, 2 );
function wc_add_to_cart_message_filter($message, $product_id = null) {
$titles[] = get_the_title( $product_id );
$titles = array_filter( $titles );
$added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );
$message = sprintf( '%s %s %s',
esc_html( $added_text ),
esc_url( wc_get_page_permalink( 'checkout' ) ),
esc_html__( 'Checkout', 'woocommerce' ),
esc_url( wc_get_page_permalink( 'cart' ) ),
esc_html__( 'View Cart', 'woocommerce' ));
return $message;
}
Have you tried a filter like the following
function your_add_to_cart_message() {
if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) :
$message = sprintf( '%s%s', __( 'Successfully added to cart.', 'woocommerce' ), esc_url( get_permalink( woocommerce_get_page_id( 'shop' ) ) ), __( 'Continue Shopping', 'woocommerce' ) );
else :
$message = sprintf( '%s%s', __( 'Successfully added to cart.' , 'woocommerce' ), esc_url( get_permalink( woocommerce_get_page_id( 'cart' ) ) ), __( 'View Cart', 'woocommerce' ) );
endif;
return $message;
}
add_filter( 'wc_add_to_cart_message', 'your_add_to_cart_message' );
In reply to the ajax message update, try a translation function like:
function your_woo_ajax_solution( $translation, $text, $domain ) {
if ( $domain == 'woocommerce' ) { // your domain name
if ( $text == 'View Cart' ) { // current text that shows
$translation = 'Basket updated.'; // The text that you would like to show
}
}
return $translation;
}
add_filter( 'gettext', 'your_woo_ajax_solution', 10, 3 );
2017 - 2019 - For Woocommerce 3+ (handling multiple products added to cart)
Replaced by wc_add_to_cart_message_html filter hook, the 2nd function argument has changed to $products (instead of $product_id)…
You can make changes on the code inside this hooked function, like in this thread:
add_filter( 'wc_add_to_cart_message_html', 'custom_add_to_cart_message_html', 10, 2 );
function custom_add_to_cart_message_html( $message, $products ) {
$titles = array();
$count = 0;
foreach ( $products as $product_id => $qty ) {
$titles[] = ( $qty > 1 ? absint( $qty ) . ' × ' : '' ) . sprintf( _x( '“%s”', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) );
$count += $qty;
}
$titles = array_filter( $titles );
$added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', $count, 'woocommerce' ), wc_format_list_of_items( $titles ) );
// The custom message is just below
$added_text = sprintf( _n("%s item has %s", "%s items have %s", $count, "woocommerce" ),
$count, __("been added to your basket.", "woocommerce") );
// Output success messages
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
$message = sprintf( '%s %s', esc_url( $return_to ), esc_html__( 'Continue shopping', 'woocommerce' ), esc_html( $added_text ) );
} else {
$message = sprintf( '%s %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View cart', 'woocommerce' ), esc_html( $added_text ) );
}
return $message;
}
Related threads (for Woocommerce 3+):
Hide Added to Cart message in Woocommerce
Customizing add-to-cart messages based on the product IDs in WooCommerce 3
Customize add to cart message in Woocommerce 3
If you look at add-to-cart.js it fires a trigger added_to_cart on adding a product to cart. I hooked into that and did this
jQuery(document.body).on("added_to_cart", function( data ) {
jQuery('button.added').nextAll().remove();
jQuery('button.added').after(' <span style="text-align:center;display:block;" class="cart_updated_ajax"><a href="' + wc_add_to_cart_params.cart_url + '" title="' +
wc_add_to_cart_params.i18n_view_cart + '">Cart Updated</a></span>');
});
Here you can add anything after product is added to cart.
Hope that helps!
In Woocommerce 3.0 "wc_add_to_cart_message" is obsolete and no longer works. So while the answer by #zmonteca was ok, is no longer working on the Woocommerce 3.0
Just replace "wc_add_to_cart_message" with "wc_add_to_cart_message_html" and voile... works.
add_filter ( 'wc_add_to_cart_message', 'wc_add_to_cart_message_filter', 10, 2 );
function wc_add_to_cart_message_filter($message, $product_id = null) {
$titles[] = get_the_title( $product_id );
$titles = array_filter( $titles );
$added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );
$message = sprintf( '%s %s %s',
esc_html( $added_text ),
esc_url( wc_get_page_permalink( 'checkout' ) ),
esc_html__( 'Checkout', 'woocommerce' ),
esc_url( wc_get_page_permalink( 'cart' ) ),
esc_html__( 'View Cart', 'woocommerce' ));
return $message;}
#Dante is correct, the solutions provided by #BradleyD won't work for ajax_add_to_cart on shop page.
The solution provided by #Abstract is working as expected. I am also using his solution.
Another jQuery approach is to to listen for the the ajaxSuccess event on the document object and do the desired modifications for the clicked button.
Something like that should work:
$(document).ajaxSuccess(function(event, xhr, settings) {
if (settings.url.indexOf('?wc-ajax=add_to_cart') !== -1) {
// You can find the clicked button element under the event.target.activeElement
// Than you can do whatever you want here. Add new html element and text, etc.
}
});