By default it will display total sales of product full time, now I want to it reset total sales of product when order status is Processing or Complete ( in WooCommerce => Orders ). it still does not work :'(
add_action( 'woocommerce_single_product_summary', 'order_is_status', 10, 1);
function order_is_status($order_id) {
global $product;
$order = new WC_Order($order_id);
$units_sold = get_post_meta( $product->id, 'total_sales', true );
if ( 'completed' == $order->status ) {
$order -> update_post_meta( $post_id, 'total_sales', '0' ); // reset total oder = 0
}else {
echo '<p>' . sprintf( __( 'Units sold: %s', 'woocommerce' ), $units_sold ) . '</p>';
}
}
The action 'woocommerce_single_product_summary' does not provide an order id as a parameter.
Related
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 made a custom field in the product card and displayed it in the catalogue, but the problem is that there is no link to go to the product card. I would appreciate any help.
Here is the code for adding a field in the product card
// Field Short name
add_action( 'woocommerce_product_options_general_product_data', 'shorttitle_custom_general_fields' );
add_action( 'woocommerce_process_product_meta', 'shorttitle_custom_general_fields_save' );
function shorttitle_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => 'shorttitle_field',
'label' => __( 'Краткое название', 'woocommerce' ),
//'desc_tip' => 'true',
//'description' => __( 'Краткое название', 'woocommerce' ),
'type' => 'html'
)
);
echo '</div>';
}
function shorttitle_custom_general_fields_save( $post_id ){
// Text Field
$woocommerce_text_field = $_POST['shorttitle_field'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, 'shorttitle_field', esc_attr( $woocommerce_text_field ) );
}
This is the code for displaying this field on the catalogue page
// Change the output of the product header (in the catalogue)
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title' );
add_action( 'woocommerce_shop_loop_item_title', 'custom_woocommerce_template_loop_product_title', 99 );
function custom_woocommerce_template_loop_product_title() {
global $post;
$product = wc_get_product( $post->ID );
$title = get_post_meta( $post->ID, 'shorttitle_field', true );
if( $title ) {
echo '<h2 class="woocommerce-loop-product__title">' . esc_html( $title ) . '</h2>';
} else {
echo '<h2 class="woocommerce-loop-product__title">' . esc_html( $product->get_title() ) . '</h2>';
}
}
Page https://www.krisstyle.com.ua/shop/
I'm using this snippet below to slightly modify Woocommerce's added to cart message. It redirects to the cart page from product pages. I'd like to add the product's thumbnail image to that Woocommerce message notification to show it clearer to the customer what has been added to the cart. Any solutions? I've tried a bunch of different ways, but no success.
function ace_add_to_cart_message_html( $message, $products ) {
$count = 0;
$titles = array();
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.', // Singular
'%s are added to your cart.', // Plural
$count, // Number of products added
'woocommerce' // Textdomain
), wc_format_list_of_items( $titles ) );
$message = sprintf( '%s %s', esc_url( wc_get_checkout_url() ), esc_html__( 'Proceed to checkout', 'woocommerce' ), esc_html( $added_text ) );
return $message;
}
You can use wc_add_to_cart_message action hook. try the below code.
function add_product_image_wc_add_to_cart_message_html( $message, $product_id ){
if ( has_post_thumbnail( $product_id ) ):
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $product_id ), 'single-post-thumbnail' );
endif;
$message = sprintf( '<img src="'.$image[0].'" style="height: 50px;width: 50px;float: left;margin-right: 10px;" />%s', $message );
return $message;
}
add_filter( 'wc_add_to_cart_message', 'add_product_image_wc_add_to_cart_message_html', 10, 2 );
Tested and Works
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 );
Code from class-wc-cart.php file of woocommerce plugin:
// Force quantity to 1 if sold individually and check for existing item in cart
if ( $product_data->is_sold_individually() ) {
$quantity = apply_filters( 'woocommerce_add_to_cart_sold_individually_quantity', 1, $quantity, $product_id, $variation_id, $cart_item_data );
$in_cart_quantity = $cart_item_key ? $this->cart_contents[ $cart_item_key ]['quantity'] : 0;
if ( $in_cart_quantity > 0 ) {
throw new Exception( sprintf( '%s %s', wc_get_cart_url(), __( 'View Cart', 'woocommerce' ), sprintf( __( 'You cannot add another "%s" to your cart.', 'woocommerce' ), $product_data->get_title() ) ) );
}
}
The Modification I wish to make is to change
wc_get_cart_url()
to
wc_get_checkout_url()
and add the modified code into functions.php so that the change becomes permanent. But how to achieve this??
If you run your own validation before WooCommerce, you can check for the sold_individually() limitation and show your own error notice, like so:
add_filter( 'woocommerce_add_to_cart_validation',
'so_41537378_individual_validation', 5, 6 );
function so_41537378_individual_validation( $passed_validation, $product_id, $quantity, $variation_id = '', $variation = array(), $cart_item_data = array() ) {
$product_data = wc_get_product( $product_id );
// Load cart item data - may be added by other plugins
$cart_item_data = (array) apply_filters( 'woocommerce_add_cart_item_data', $cart_item_data, $product_id, $variation_id );
// Generate a ID based on product ID, variation ID, variation data, and other cart item data
$cart_id = WC()->cart->generate_cart_id( $product_id, $variation_id, $variation, $cart_item_data );
// Find the cart item key in the existing cart
$cart_item_key = WC()->cart->find_product_in_cart( $cart_id );
// Force quantity to 1 if sold individually and check for existing item in cart
if ( $product_data->is_sold_individually() ) {
$quantity = apply_filters( 'woocommerce_add_to_cart_sold_individually_quantity', 1, $quantity, $product_id, $variation_id, $cart_item_data );
$in_cart_quantity = $cart_item_key ? WC()->cart->cart_contents[ $cart_item_key ]['quantity'] : 0;
if ( $in_cart_quantity > 0 ) {
/* translators: %s: product name */
$error_message = sprintf( '%s %s', wc_get_checkout_url(), __( 'Checkout', 'your-plugin-textdomain' ), sprintf( __( 'You cannot add another "%s" to your cart.', 'your-plugin-textdomain' ), $product_data->get_title() ) );
// add your notice
wc_add_notice( $error_message, 'error' );
$passed_validation = false;
}
}
return $passed_validation;
}
Edited for quite a few errors like $this out of context, undefined variables, etc. Tested and working.