Woocommerce product counts in single product page - wordpress

I am currently struggling with woo-commerce and the availability status on product pages. I am trying to display various texts (translated) on the product pages, whether the product is available and how many we have, then whether it is unavailable, whether it is available to backorder, and what I can't do is display the message that the product is in stock with disabled managing stock. There are unlimited products. But it still shows me only "available places".
There is my function.
add_filter( 'woocommerce_get_availability', 'product_stock_quantity_single', 1, 2);
function product_stock_quantity_single( $availability, $_product ) {
global $product;
$stock = $product->get_stock_quantity();
if ( $_product->is_in_stock() ) $availability['availability'] = __('Text front ' . $stock . ' text behind', 'woocommerce');
if ( !$_product->is_in_stock() ) $availability['availability'] = __('Out of stock text', 'woocommerce');
if ( $_product->is_on_backorder() ) $availability['availability'] = __( 'On backorder text', 'woocommerce' );
return $availability;
}
I've tried many different combinations and frankly, it doesn't work for me. Thanks for all the advice and help. I tried what was in the topic which was merged with mine. It's a different topic about different things I want to achieve. I need a product with disabled managing stock but still available (something like infinite stock)

You're currently returning $availability outside of the function. Give this a try:
add_filter( 'woocommerce_get_availability', 'product_stock_quantity_single', 1, 2);
function product_stock_quantity_single( $availability, $_product ) {
global $product;
$stock = $product->get_stock_quantity();
if ( $_product->is_in_stock() ) $availability['availability'] = __('Text front ' . $stock . ' text behind', 'woocommerce');
if ( !$_product->is_in_stock() ) $availability['availability'] = __('Out of stock text', 'woocommerce');
if ( $_product->is_on_backorder() ) $availability['availability'] = __( 'On backorder text', 'woocommerce' );
return $availability;
}

Check out this answer and add the fourth condition I added. Hope this helps you want to achieve.
add_filter( 'woocommerce_get_availability', 'product_stock_quantity_single', 1, 2);
function product_stock_quantity_single( $availability, $_product ) {
global $product;
$stock = $product->get_stock_quantity();
if ( $_product->is_in_stock() ) $availability['availability'] = __('Text front ' . $stock . ' text behind', 'woocommerce');
if ( !$_product->is_in_stock() ) $availability['availability'] = __('Out of stock text', 'woocommerce');
if ( $_product->is_on_backorder() ) $availability['availability'] = __( 'On backorder text', 'woocommerce' );
if ( $_product->is_in_stock() && !$stock ) $availability['availability'] = __('Stock disabled text', 'woocommerce');
return $availability;
}

Related

Display free shipping threshold based on Woocommerce min amount order from shipping settings

I want to display shipping notice based on free shipping settings for min amount order. I have more then one shipping zone based on different country and currency. Tried to use the following code, it works when you add product to cart, the shipping notice is display but as if you update quantity the shipping notice is removed, maybe duo to Ajax load? Can I add something to this code to make it work with qty update on product?
add_action( 'woocommerce_before_mini_cart_contents', 'display_free_shipping_cart_notice_zones' );
function display_free_shipping_cart_notice_zones() {
// Get Shipping Methods for Current Zone
global $woocommerce;
$shipping_methods = $woocommerce->shipping->get_shipping_methods();
// Loop through the array to find min_amount value/s
foreach($shipping_methods as $key => $value) {
if ( $shipping_methods[$key]->min_amount > 0 ) {
$min_amounts[$key] = $shipping_methods[$key]->min_amount;
}
}
if ( is_array($min_amounts) ) {
// Find lowest min_amount
$min_amount = min($min_amounts);
// Get Cart Subtotal inc. Tax excl. Shipping
$current = WC()->cart->subtotal;
// If Subtotal < Min Amount Echo Notice
// and add "Continue Shopping" button
if ( $current < $min_amounts ) {
$added_text = 'Get free shipping if you order ' . wc_price( $min_amount - $current ) . ' more!';
$return_to = wc_get_page_permalink( 'shop' );
$notice = sprintf( '%s %s', esc_url( $return_to ), 'Continue Shopping', $added_text );
wc_print_notice( $notice, 'notice' );
}
else if ( $current = $min_amounts ) {
$added_text = 'Congratulations - Your shipping is now on us and absolutely free :)';
$return_to = wc_get_page_permalink( 'shop' );
//$notice = sprintf( '%s %s', esc_url( $return_to ), 'Continue Shopping', $added_text );
$notice = sprintf( '%s', $added_text );
wc_print_notice( $notice, 'notice' );
}
}
}

WooCommerce custom order itemmeta display my account view order page

I created a custom field for the ecommerce admin order item meta. Everything is fine.
I would like to display the Custom Fields MetaValue on My Account's Order Details page. But nothing is being displayed. Based on Save Order item custom field in Woocommerce Admin order pages answer code, this is my attempt
function add_order_item_custom_field( $item_id, $item ) {
woocommerce_wp_text_input( array(
'id' => 'v_number'.$item_id,
'label' => __( 'V Number : ', 'ctxt' ),
'description' => __( 'Enter the title of your custom text field.', 'ctxt' ),
'desc_tip' => true,
'class' => 'v_number_class',
'value' => wc_get_order_item_meta( $item_id, '_v_number' ),
) );
}
add_action( 'woocommerce_before_order_itemmeta', 'add_order_item_custom_field', 10, 2 );
// Save the custom field value
function save_order_item_custom_field_value( $post_id, $post ){
$order = wc_get_order( $post_id );
foreach ( $order->get_items() as $item_id => $item ) {
if( isset( $_POST['v_number'.$item_id] ) ) {
$item->update_meta_data( '_v_number', sanitize_text_field( $_POST['v_number'.$item_id] ) );
$item->save();
}
}
$order->save();
}
add_action('save_post', 'save_order_item_custom_field_value' );
// Display meta my account view order page
printf (
'<p><a>V Number : <strong>' . $order->get_meta('_v_number') . '</strong></a></p>'
);
it should print your custom order meta value.
add_action( 'woocommerce_view_order', 'print_custom_order_meta' );
function print_custom_order_meta( $order_id ){
$order = wc_get_order($order_id);
foreach( $order->get_items() as $item ) {
echo 'V Number for '. $item->get_name() .' - ' . $item->get_meta( '_v_number', true ) . '<br>';
}
}
edit
for need to show metavalue after each product item you need to hook with a different action like this.
add_action('woocommerce_order_item_meta_end', 'show_order_meta', 11, 3);
function show_order_meta( $item_id, $item, $order ) {
echo '<br>V Number for '. $item->get_name() .' - ' . $item->get_meta( '_v_number', true ) . '<br>';
}

How to show Product Price + Shipping VAT in "Total" Merged

I have one little isusue that dont know how to resolve myself. I have set in my shop to show separated Price and shipping costs but in total showed me bad price.
For example my products cost 24.99€ + SHIPPING FEE : 3,95€ = 28.94€ but in calculation in cart page is calculating: 24.99€ + 3.95€ - 0.26€ what is wrong.
i found that Total price is calculated via this function:
<td data-title="<?php esc_attr_e( 'Total', 'woocommerce' ); ?>"><?php wc_cart_totals_order_total_html(); ?></td>
and this is function that control that part:
from cart-totals.php in templates, and bellow is function from wc-cart-functions.php
function wc_cart_totals_order_total_html() {
$value = '<strong>' . WC()->cart->get_total() . '</strong> ';
// If prices are tax inclusive, show taxes here.
if ( wc_tax_enabled() && WC()->cart->display_prices_including_tax() ) {
$tax_string_array = array();
$cart_tax_totals = WC()->cart->get_tax_totals();
if ( get_option( 'woocommerce_tax_total_display' ) == 'itemized' ) {
foreach ( $cart_tax_totals as $code => $tax ) {
$tax_string_array[] = sprintf( '%s %s', $tax->formatted_amount, $tax->label );
}
} elseif ( ! empty( $cart_tax_totals ) ) {
$tax_string_array[] = sprintf( '%s %s', wc_price( WC()->cart->get_taxes_total( true, true ) ), WC()->countries->tax_or_vat() );
}
if ( ! empty( $tax_string_array ) ) {
$taxable_address = WC()->customer->get_taxable_address();
$estimated_text = WC()->customer->is_customer_outside_base() && ! WC()->customer->has_calculated_shipping()
? sprintf( ' ' . __( 'estimated for %s', 'woocommerce' ), WC()->countries->estimated_for_prefix( $taxable_address[0] ) . WC()->countries->countries[ $taxable_address[0] ] )
: '';
$value .= '<small class="includes_tax">' . sprintf( __( '(includes %s)', 'woocommerce' ), implode( ', ', $tax_string_array ) . $estimated_text ) . '</small>';
}
}
echo apply_filters( 'woocommerce_cart_totals_order_total_html', $value );
}
So my question is how to add that 1.63E at Total Price, so will get correct price. Thanks
EDIT: Found the same problem like mine here but answers dont seems to make changes.
First, thanks for your Post, I was almost thinking I'm the only one with this need.
So far, this worked for my shop. I'm shure my code is not very versatile for different shop settings. Maybe someone could make a more general usable version.
Edit: I've added a picture showing the two rates. Image of the result and I've found a minor mistake calculating the shipping tax, corrected now.
/**
* Change Tax Amount including Shipping Taxes
* Referencing to wc-cart-functions.php starting from Line 296
*
*/
add_filter( 'woocommerce_cart_totals_order_total_html', 'woo_rename_tax_inc_cart', 10, 1 );
function woo_rename_tax_inc_cart( $value ) {
/* Get all infos needed */
$shipping_total = WC()->cart->shipping_total;
$taxes = WC()->cart->get_taxes_total( true, true );
$taxrate = 7.7;
$newtaxes = ($shipping_total/(100+$taxrate)*$taxrate) + $taxes; // Shipping is 100% + taxrate %, so we deduct both percentages.
/* Check if Shipment total is active */
if ( ! empty($shipping_total) && $shipping_total != 0 ) {
if ( ! empty( $value ) ) {
// Show Price /wc-cart-functions.php Line 297
$value = '<strong>' . WC()->cart->get_total() . '</strong> ';
$value .= '<small class="includes_tax">' . '(inkl. ' . wc_price( $newtaxes ) . ' MWST)' . '</small>';
}
}
// Attach Tax Info to Price (single line)
$value = str_ireplace( 'Tax', 'GST', $value );
return $value;
}

Woocommerce recently viewed Products

I have created a recently viewed script which generated a shortcode which I then inserted into my home page.
The script is designed so that people who may have visited my website and left, once they come back can see instantly what products they had been viewing on their last visit.
I have placed the shortcode [woocommerce_recently_viewed_products]
and have generated the shortcode using the following script:
function rc_woocommerce_recently_viewed_products( $atts, $content = null ) {
// Get shortcode parameters
extract(shortcode_atts(array(
"per_page" => '5'
), $atts));
// Get WooCommerce Global
global $woocommerce;
// Get recently viewed product cookies data
$viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] ) : array();
$viewed_products = array_filter( array_map( 'absint', $viewed_products ) );
// If no data, quit
if ( empty( $viewed_products ) )
return __( 'You have not viewed any product yet!', 'rc_wc_rvp' );
// Create the object
ob_start();
wc_setcookie( 'woocommerce_recently_viewed', implode( '|', $viewed_products ) );
}
// Get products per page
if( !isset( $per_page ) ? $number = 4 : $number = $per_page )
// Create query arguments array
$query_args = array(
'posts_per_page' => $number,
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'post__in' => $viewed_products,
'orderby' => 'rand'
);
// Add meta_query to query args
$query_args['meta_query'] = array();
// Check products stock status
$query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query();
// Create a new query
$r = new WP_Query($query_args);
// If query return results
if ( $r->have_posts() ) {
$content = '<ul class="rc_wc_rvp_product_list_widget">';
// Start the loop
while ( $r->have_posts()) {
$r->the_post();
global $product;
$content .= '<li>
<a href="' . get_permalink() . '">
' . ( has_post_thumbnail() ? get_the_post_thumbnail( $r->post->ID, 'shop_thumbnail' ) : woocommerce_placeholder_img( 'shop_thumbnail' ) ) . ' ' . get_the_title() . '
</a> ' . $product->get_price_html() . '
</li>';
}
$content .= '</ul>';
}
// Get clean object
$content .= ob_get_clean();
// Return whole content
return $content;
}
// Register the shortcode
add_shortcode("woocommerce_recently_viewed_products",
"rc_woocommerce_recently_viewed_products");
Everything seems to have registered. However,when I test this myself. I view a few products, go back to the homepage where the shortcode is registered and I see the text
You have not viewed any product yet!
I can not figure out what might be missing in order to register and show the products which I or a potential customer may have viewed.
Woocommerce only save the recently viewed cookie IF woocommerce_recently_viewed_products WIDGET is ACTIVE! See code in wc-product-functions.php wc_track_product_view() function.
Code to save the cookie always in functions.php:
/**
* Track product views. Always.
*/
function wc_track_product_view_always() {
if ( ! is_singular( 'product' ) /* xnagyg: remove this condition to run: || ! is_active_widget( false, false, 'woocommerce_recently_viewed_products', true )*/ ) {
return;
}
global $post;
if ( empty( $_COOKIE['woocommerce_recently_viewed'] ) ) { // #codingStandardsIgnoreLine.
$viewed_products = array();
} else {
$viewed_products = wp_parse_id_list( (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) ); // #codingStandardsIgnoreLine.
}
// Unset if already in viewed products list.
$keys = array_flip( $viewed_products );
if ( isset( $keys[ $post->ID ] ) ) {
unset( $viewed_products[ $keys[ $post->ID ] ] );
}
$viewed_products[] = $post->ID;
if ( count( $viewed_products ) > 15 ) {
array_shift( $viewed_products );
}
// Store for session only.
wc_setcookie( 'woocommerce_recently_viewed', implode( '|', $viewed_products ) );
}
remove_action('template_redirect', 'wc_track_product_view', 20);
add_action( 'template_redirect', 'wc_track_product_view_always', 20 );
You need to set the cookie when you are viewing a single product page so use something like this where I set the cookie to equal the product ID I just viewed. In your case you'll need to get the cookie value if it exists then append the new product to the list of products.
function set_user_visited_product_cookie() {
global $post;
if ( is_product() ){
// manipulate your cookie string here, explode, implode functions
wc_setcookie( 'woocommerce_recently_viewed', $post->ID );
}
}
add_action( 'wp', 'set_user_visited_product_cookie' );
Below code to set cookie 'woocommerce_recently_viewed' worked for me. Hope it helps other
$Existing_product_id = $_COOKIE['woocommerce_recently_viewed'];
if ( is_product() )
{
$updated_product_id = $Existing_product_id.'|'.$post->ID;
wc_setcookie( 'woocommerce_recently_viewed', $updated_product_id );
}

Woocommerce Show default variation price

I'm using Woocommerce and product variations and all my variations have a default variation defined.
How I can find the default variation and display its price?
This is the code I got so far, but it displays the cheapest variation price, and I'm looking for my default variation product price instead.
// Use WC 2.0 variable price format, now include sale price strikeout
add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );
function wc_wc20_variation_price_format( $price, $product ) {
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'HERE YOUR LANGUAGE: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'HERE YOUR LANGUAGE: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
if ( $price !== $saleprice ) {
$price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
}
return $price;
I found the code example here Woocommerce variation product price to show default
I did not find answer for this question (how to display price from default product variation instead of price range?) so i created the code:
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
foreach($product->get_available_variations() as $pav){
$def=true;
foreach($product->get_variation_default_attributes() as $defkey=>$defval){
if($pav['attributes']['attribute_'.$defkey]!=$defval){
$def=false;
}
}
if($def){
$price = $pav['display_price'];
}
}
return woocommerce_price($price);
}
This plugin show price from default variation, of course if you set default values before. Tested on woocommerce version 2.6.2.
This is my own solution, think it could be better, but I'm not a php developer, so feel free to add improvements.
function wc_wc20_variation_price_format( $price, $product ) {
// Main Price
$available_variations = $product->get_available_variations();
$selectedPrice = '';
foreach ( $available_variations as $variation )
{
$tmp = implode($variation['attributes']);
if (strpos($tmp,'standard') !== false) {
$selectedproduct = wc_get_product($variation['variation_id']);
$price = $selectedproduct->get_price();
$selectedPrice = wc_price($price);
}
}
return $selectedPrice;
}
I modified your code a bit to show discounted price as well.
// show default variation price
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
$default_attributes = $product->get_variation_default_attributes();
foreach($product->get_available_variations() as $variation) {
$is_default=true;
foreach($default_attributes as $attribute_key => $attribute_value) {
if($variation['attributes']['attribute_' . $attribute_key] != $attribute_value){
$is_default=false;
break;
}
}
if($is_default){
return $variation['price_html'];
}
}
}

Resources