Woocommerce in wordpress, I want to show number of customers who are ordering that product (has not yet been processed)
Demo: http://i.imgur.com/f7Iicu2.jpg
put the below code in your function.PHP file
add_action( 'woocommerce_single_product_summary', 'bbloomer_product_sold_count', 11 );
add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_product_sold_count', 11 );
function bbloomer_product_sold_count() {
global $product;
$units_sold = get_post_meta( $product->id, 'total_sales', true );
echo '<p>' . sprintf( __( 'Units Sold: %s', 'woocommerce' ), $units_sold ) . '</p>';
}
in above code you can see the units sold in product single page/ Product detail page and looping page.
Related
First of all, I have zero knowledge of PHP.
I would like to add a button to each of my blog posts that will have the same text as "Your_Promo". Later I will translate that "Your_Promo" with another text like "Hosting Deal", later could be "Cheap Domain", and so on.
I use the below code in my functions.pho, but it doesn't work.
function custom_wc_translations($translated){
$text = array(
'Your_Promo' => 'Hosting Deal',
);
$translated = str_ireplace( array_keys($text), $text, $translated );
return $translated;
}
add_filter( 'gettext', 'custom_wc_translations', 20 );
Could anybody help?
Add this code in your active theme functions.php file:
function vh_excerpt_read_more($more) {
global $post;
return '... <a class="excerpt-read-more" href="'. get_permalink( $post->ID ) . '" title="'. __( 'Read ', 'domain_name' ) . esc_attr( get_the_title( $post->ID ) ).'">'. __( 'Your_Promo ยป', 'domain_name' ) .'</a>';
}
add_filter( 'excerpt_more', 'vh_excerpt_read_more' );
Tested & working for me
See the result below:
Thanks in advance.
I'm trying to remove product thumbnails from the WooCommerce StoreFront theme. I have every instance of them removed from the storefront generally, including the cart, but not the minicart in the main navigation/menubar.
The hooks are the same they are just not applying.
my filter
add_filter( 'woocommerce_cart_item_thumbnail', '__return_false' );
from cart.php line 68:
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
from mini-cart.php line 38:
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
This works for the cart where I am trying to remove it, but doesn't remove it from the minicart.
As a second example of a minicart problem, I am also removing the product link from the title. This works and it removes the link from the cart item (as well as in the checkout when reviewing the order), but still not from the minicart.
filter:
add_filter( 'woocommerce_cart_item_name', 'mad_remove_cart_product_link', 1, 3 );
function mad_remove_cart_product_link( $product_link, $cart_item, $cart_item_key ) {
$product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
return "0" . $product->get_title() . "0";
}
on line 83 of cart.php
echo apply_filters( 'woocommerce_cart_item_name', sprintf( '%s', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key );
on line 37 of mini-cart.php:
$product_name = apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key );
The visible "0" at each end of the product title in the filter is just for debugging. They obviously appear in the cart, the review area of the checkout, but not in the minicart
Just add another product to the cart and you will see your code working. WooCommerce uses sessionStorage, so as long the cart is not updated, it doesn't send any new ajax request, rather uses the value from sessionStorage :)
Add this to your child theme functions.php
add_filter( 'woocommerce_mini_cart_item_name_permalink', '__return_null' );
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.
I'm sooooo close - but I can't get this last little bit.
I am adding the authors name to books we have for sale in woo. The style needs to be different from the book title so I'm using a custom meta field to put it under the title and style it the way I want. I have everything working and where I want it, now I just need for it only show on a particular category instead of on all products.
There seems to me to be two ways to do this.
1 - Only display it for products in a particular category
2 - Only display it if there is content.
I'm currently trying to only display if it's a particular category, but while writing this, its seems a more elegant solution to only display if content exists.
Here's what I have
function cn_add_add_subhead_to_title() {
global $woocommerce, $post;
if ( is_product() && has_term( 'books-media', 'product_cat' ) ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
This doesn't display anywhere, but when I take out the if statement it shows on all products.
Any idea where I went awry?
Here is the answer in context Helga - this snippet will add a custom meta field to the woocommerce product editing dashboard, save the content and display it under the product title on the single product view.
/* add product subhead custom field to woocommerce product dashboard */
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_text_field',
'label' => __( 'MY-LABEL', 'woocommerce' ),
'placeholder' => 'Author',
'desc_tip' => 'true',
'description' => __( 'This text will show below the product title.', 'woocommerce' )
)
);
echo '</div>';
}
/* Save woo custom field */
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_text_field = $_POST['_text_field'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, '_text_field', esc_attr( $woocommerce_text_field ) );
}
/* add author below title on single product */
function cn_add_subhead_to_title() {
global $woocommerce, $post;
$meta = get_post_meta( $post->ID, '_text_field', true );
if( $meta != '' ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_subhead_to_title', 6 );
What if you simply test for the presence of the meta? If you don't save the meta in the other categories it will never display:
function cn_add_add_subhead_to_title() {
global $post;
if ( $meta = get_post_meta( $post->ID, '_text_field', true ) ) {
?>
<div class="cn-subhead">
<?php printf( __( 'by %s', 'textdomain' ), $meta ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
Helga - Thanks for your interest and help. I had a little time so I decided to try the other route of only displaying the meta if there was content instead of based on category. This may be a more flexible solution anyway, leaving the field open to any sort of subhead they want instead of limiting it only to Authors.
This the code that worked:
/* add author below title on single product */
function cn_add_add_subhead_to_title() {
global $woocommerce, $post;
$meta = get_post_meta( $post->ID, '_text_field', true );
if( $meta != '' ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
I am developing a plugin for WooCommerce. I want to override the order details template of admin. i have read about on https://www.skyverge.com/blog/override-woocommerce-template-file-within-a-plugin/ , but still I don't understand how to override the order detail template of admin. following is my code:
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
if ( ! class_exists( 'Test' ) ) {
load_plugin_textdomain( 'test', false, dirname( plugin_basename( __FILE__ ) ) . '/' );
}
}
class Test {
public function __construct() {
add_action( 'init', array( $this, 'include_template_functions' ), 20 );
add_action( 'woocommerce_init', array( $this, 'woocommerce_loaded' ) );
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
do_action( 'woocommerce_admin_order_data_after_order_details', 'hello' );
}
public function hello() {
echo "order detail template has loaded";
}
public function include_template_functions() {
include( 'woocommerce-template.php' );
echo "template has loaded";
}
public function woocommerce_loaded() {
}
public function plugins_loaded() {
}
}
$GLOBALS['wc_acme'] = new Test();
It's not calling the hook associated with woocommerce_admin_order_data_after_order_details.
Can anyone please suggest or share some example of editing the order details template editing via plugin. Please note that I am referring to the order detail template inside the administrator, where administrator can view the detail of any order from the list.
From my tutorial on customizing WooCommerce checkout fields this is how you'd display some extra order meta data in the Order Details metabox:
// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){ ?>
<div class="order_data_column">
<h4><?php _e( 'Extra Details' ); ?></h4>
<?php
echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_some_field', true ) . '</p>';
echo '<p><strong>' . __( 'Another field' ) . ':</strong>' . get_post_meta( $order->id, '_another_field', true ) . '</p>'; ?>
</div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );
This assumes that you have collected the data on checkout and saved the data as post meta for the $order in question.
You could use woocommerce_admin_order_data_after_billing_address action:
function order_phone_backend($order){
echo "<p><strong>Billing phone 2:</strong> " . get_post_meta( $order->id, '_billing_phone_new', true ) . "</p><br>";
}
add_action( 'woocommerce_admin_order_data_after_billing_address','order_phone_backend', 10, 1 );