I am recently working with WooCommerce WordPress project for my client and I am editing a plugin for WooCommerce WordPress. I am trying to display the product price to include the currency code use this function
* #param mixed $item
* #param bool $inc_tax (default: false).
* #param bool $round (default: true).
* #return float
*/
public function get_item_total( $item, $inc_tax = false, $round = true ) {
$qty = ( ! empty( $item['qty'] ) ) ? $item['qty'] : 1;
if ( $inc_tax ) {
$price = ( $item['line_total'] + $item['line_tax'] ) / max( 1, $qty );
} else {
$price = $item['line_total'] / max( 1, $qty );
}
$price = '<span class="price">' . $round ? round( $price, wc_get_price_decimals() ) . '</span>' : '<span class="price">' . $price . '</span>' ;
return apply_filters( 'woocommerce_order_amount_item_total', $price, $this, $item, $inc_tax, $round );
}
But it spits out a number like 55 with out the currency symbol. is there anyway get the number with currency symbol like $55 ?
Thank you for all your help
ty rgdesign
This was the fix it works now
public function get_item_total( $item, $inc_tax = false, $round = true ) {
$qty = ( ! empty( $item['qty'] ) ) ? $item['qty'] : 1;
if ( $inc_tax ) {
$price = ( $item['line_total'] + $item['line_tax'] ) / max( 1, $qty );
} else {
$price = $item['line_total'] / max( 1, $qty );
}
$currency_symbol = get_woocommerce_currency_symbol();
$price = $round ? round($price, wc_get_price_decimals() ): $price;
$price = '<span class="price">' .$currency_symbol . $price .'</span>';
return apply_filters( 'woocommerce_order_amount_item_total',$price, $this, $item, $inc_tax, $round );
}
I found it to be very easy. Just insert the following code block to functions.php file under theme folder.
//Change the symbol of an existing currency
add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
switch( $currency ) {
case 'USD': $currency_symbol = 'USD$'; break;
}
return $currency_symbol;
}
Related
I have this code but it seems outdated
add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
global $product;
function custom_price_html( $price, $product ) {
if ( is_single() && $product->is_on_sale() && $sales_price_to != "" ) {
$sales_price_from = $product->get_date_on_sale_from();
$sales_price_to = $product->get_date_on_sale_to();
if( ! empty($sales_price_from) || ! empty($sales_price_to) ){
$sales_price_date_to = $sales_price_from->date( "j.m.Y");
$sales_price_date_from = $sales_price_to->date( "j.m.Y");
$sales_date = '<p class="offer_date">Angebot vom '.$sales_price_date_from.' bis '.$sales_price_date_to.'</p>';
} else {
$sales_date = $sales_price_from = $sales_price_to = '';
}
$price = str_replace( '</ins>', ' </ins> <b>(Offer from ' . $sales_price_date_from . ' till ' . $sales_price_date_to . ')</b>', $price );
}
return $price;
}
I want to display sale end date for simple and variable products.
You're using the $sales_price_to variable when it's not defined. Please use the below code, tested and works perfectly.
add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
function custom_price_html( $price, $product ) {
if ( is_single() && $product->is_on_sale() ) {
$sales_price_from = $product->get_date_on_sale_from();
$sales_price_to = $product->get_date_on_sale_to();
if( ! empty($sales_price_from) || ! empty($sales_price_to) ){
$sales_price_date_from = $sales_price_from->date( "j.m.Y");
$sales_price_date_to = $sales_price_to->date( "j.m.Y");
$sales_date = '<p class="offer_date">Angebot vom '.$sales_price_date_from.' bis '.$sales_price_date_to.'</p>';
} else {
$sales_date = $sales_price_from = $sales_price_to = '';
}
$price = str_replace( '</ins>', ' </ins> <b>(Offer from ' . $sales_price_date_from . ' till ' . $sales_price_date_to . ')</b>', $price );
}
return $price;
}
Currently I'm using a this code/plugin to show prefix and lowest price the shop:
function show_only_lowest_prices_in_woocommerce_variable_products_load_plugin_textdomain() {
load_plugin_textdomain( 'show-only-lowest-prices-in-woocommerce-variable-products', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'show_only_lowest_prices_in_woocommerce_variable_products_load_plugin_textdomain' );
//Simple products
function wc_wc20_variation_price_format( $price, $product ) {
// Main prices
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'От %1$s', 'show-only-lowest-prices-in-woocommerce-variable-products' ), 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( __( 'От %1$s', 'show-only-lowest-prices-in-woocommerce-variable-products' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
if ( $price !== $saleprice ) {
$price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
}
return $price;
}
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 );
//Grouped products
// Show product prices in WooCommerce 2.0 format
add_filter( 'woocommerce_grouped_price_html', 'wc_wc20_grouped_price_format', 10, 2 );
function wc_wc20_grouped_price_format( $price, $product ) {
$tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
$child_prices = array();
foreach ( $product->get_children() as $child_id ) {
$child_prices[] = get_post_meta( $child_id, '_price', true );
}
$child_prices = array_unique( $child_prices );
$get_price_method = 'get_price_' . $tax_display_mode . 'uding_tax';
if ( ! empty( $child_prices ) ) {
$min_price = min( $child_prices );
$max_price = max( $child_prices );
} else {
$min_price = '';
$max_price = '';
}
if ( $min_price == $max_price ) {
$display_price = wc_price( $product->$get_price_method( 1, $min_price ) );
} else {
$from = wc_price( $product->$get_price_method( 1, $min_price ) );
$display_price = sprintf( __( 'От %1$s', 'show-only-lowest-prices-in-woocommerce-variable-products' ), $from );
}
return $display_price;
}
But I need to change it ot show the both prices (min and max) with prefix like this
From 10 to 25 lv
I tried to edited but in this code i cant get the max price to show to put prefix.
Try the following shorter function that use the dedicated hook for that:
add_filter( 'woocommerce_format_price_range', 'format_price_range_prefix', 20, 3 );
function format_price_range_prefix( $price, $from, $to ) {
$price = sprintf( _x( 'From %1$s to %2$s %3$s', 'Price range: from-to', 'woocommerce' ), is_numeric( $from ) ? wc_price( $from ) : $from, is_numeric( $to ) ? wc_price( $to ) : $to, __('lv', 'woocommerce') );
return $price;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
I am using the below code to display price next to the variations in WooCommerce. It works great if more than 2 variations but if only 2 variations it displays only the first variations price for both variations.
Any help with this would be great.
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
if ( is_product() ) {
if ( $product instanceof WC_Product && $product->is_type( 'variable' ) ) {
foreach ( $product->get_available_variations() as $variation ) {
//$product_variation_id = $variation['variation_id'];
foreach ( $variation['attributes'] as $variation_attribute_name => $variation_attribute_value ) {
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE slug = '$variation_attribute_value'" );
if ( $term != $variation_attribute_value && $variation_attribute_value != $result[0] ) continue;
$product_price = wc_price( $variation['display_price'] );
$product_price = strip_tags( $product_price );
return $term . ' - (' . $product_price . ')';
}
}
}
}
return $term;
}
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
This code is tested and works with multiple dropdowns and multiple attributes and product specific attributes like, attribute1 | attribute2 | attribute3.
// Add the prices next to the variations in WooCommerce
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_names' );
function display_price_in_variation_option_names( $term_name ) {
global $wpdb, $product;
if ( is_product() && $product instanceof WC_Product && $product->is_type( 'variable' ) ) {
foreach ( $product->get_available_variations() as $variation ) {
foreach ( $variation['attributes'] as $variation_attribute_name => $variation_attribute_value ) {
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE slug = '$variation_attribute_value'" );
$taxonomy = str_replace( 'attribute_', '', $variation_attribute_name );
$term = get_term_by( 'slug', $variation_attribute_value, $taxonomy );
if ( $term_name == $variation_attribute_value ) {
$product_price = wc_price( $variation['display_price'] );
$product_price = strip_tags( $product_price );
return $term_name . ' - (' . $product_price . ')';
} else if( is_object($term) && $term->name == $term_name ) {
$product_price = wc_price( $variation['display_price'] );
$product_price = strip_tags( $product_price );
return $term_name . ' - (' . $product_price . ')';
}
}
}
}
return $term_name;
}
How Can I change the Total price of the cart using hook.
I have tried many hooks without result.
i'm probably arrived to find the result .
This is what I have tried.
add_action('woocommerce_checkout_process', 'wh_getCartItemBeforePayment', 10);
function wh_getCartItemBeforePayment(){
if($_POST['percent']){
if($_POST['percent']==1){
/*WC()->cart->total*0.25;
WC()->cart->calculate_totals();
woocommerce_cart_totals();*/
/*
add_action('woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.01;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
}
throw new Exception( __( WC()->cart->total ) );*/
//$items = WC()->cart->get_cart();
$cupom_value=-1;
foreach ($items as $item => $values)
{
$_product = $values['data']->post;
$product_title = $_product->post_title;
$qty = $values['quantity'];
$price = get_post_meta($values['product_id'], '_price', true);
$post_id = $value['product_id'];
$regular_price = get_post_meta( $post_id, '_regular_price', true);
$sale_price = get_post_meta( $post_id, '_sale_price', true);
$cupom_value = ($regular_price - $sale_price) *0.25;
$price = $cupom_value;
$value['data']->price = $price;
}
//$price=3*0.25;
wc_add_notice($cupom_value, 'error' );
}}}
add_action( 'woocommerce_calculate_totals', 'action_cart_calculate_totals', 10, 1 );
function action_cart_calculate_totals( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( !WC()->cart->is_empty() ):
$cart_object->cart_contents_total += 10; // add 10 to cart total
endif;
}
i want to ask how to overwrite this woocommerce function (wp_schedule_single_event and post_status) using filter/action
Plugin file location : /woocommerce/includes/wc-order-functions.php
Original File :
/**
* Cancel all unpaid orders after held duration to prevent stock lock for those products.
*
* #access public
*/
function wc_cancel_unpaid_orders() {
global $wpdb;
$held_duration = get_option( 'woocommerce_hold_stock_minutes' );
if ( $held_duration < 1 || get_option( 'woocommerce_manage_stock' ) != 'yes' )
return;
$date = date( "Y-m-d H:i:s", strtotime( '-' . absint( $held_duration ) . ' MINUTES', current_time( 'timestamp' ) ) );
$unpaid_orders = $wpdb->get_col( $wpdb->prepare( "
SELECT posts.ID
FROM {$wpdb->posts} AS posts
WHERE posts.post_type IN ('" . implode( "','", wc_get_order_types() ) . "')
AND posts.post_status = 'wc-pending'
AND posts.post_modified < %s
", $date ) );
if ( $unpaid_orders ) {
foreach ( $unpaid_orders as $unpaid_order ) {
$order = wc_get_order( $unpaid_order );
if ( apply_filters( 'woocommerce_cancel_unpaid_order', 'checkout' === get_post_meta( $unpaid_order, '_created_via', true ), $order ) ) {
$order->update_status( 'cancelled', __( 'Unpaid order cancelled - time limit reached.', 'woocommerce' ) );
}
}
}
wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' );
wp_schedule_single_event( time() + ( absint( $held_duration ) * 60 ), 'woocommerce_cancel_unpaid_orders' );
}
add_action( 'woocommerce_cancel_unpaid_orders', 'wc_cancel_unpaid_orders' );
I want to change this variable :
$unpaid_orders = $wpdb->get_col( $wpdb->prepare( "
SELECT posts.ID
FROM {$wpdb->posts} AS posts
WHERE posts.post_type IN ('" . implode( "','", wc_get_order_types() ) . "')
AND posts.post_status = '**wc-on-hold**'
AND posts.post_modified < %s
", $date ) );
and this
**wp_schedule_single_event( time() + 3600 ),** 'woocommerce_cancel_unpaid_orders' );
please help..
Thanks
Azreal
This is absolutely possible - wc_cancel_unpaid_orders is not called directly, rather it's hooked on to the woocommerce_cancel_unpaid_orders action, at line 488 of that file:
add_action( 'woocommerce_cancel_unpaid_orders', 'wc_cancel_unpaid_orders' );
this means that you can unhook THEIR wc_cancel_unpaid_orders, and assign your own. For example, you could add this to your functions.php file:
<?php
remove_action( 'woocommerce_cancel_unpaid_orders', 'wc_cancel_unpaid_orders' );
add_action( 'woocommerce_cancel_unpaid_orders', 'my_custom_wc_cancel_unpaid_orders' );
function my_custom_wc_cancel_unpaid_orders() {
global $wpdb;
$held_duration = get_option( 'woocommerce_hold_stock_minutes' );
if ( $held_duration < 1 || get_option( 'woocommerce_manage_stock' ) != 'yes' )
return;
$date = date( "Y-m-d H:i:s", strtotime( '-' . absint( $held_duration ) . ' MINUTES', current_time( 'timestamp' ) ) );
// write your own code here....
$unpaid_orders = $wpdb->get_col( $wpdb->prepare( "
SELECT posts.ID
FROM {$wpdb->posts} AS posts
WHERE posts.post_type IN ('" . implode( "','", wc_get_order_types() ) . "')
AND posts.post_status = 'wc-pending'
AND posts.post_modified < %s
", $date ) );
if ( $unpaid_orders ) {
foreach ( $unpaid_orders as $unpaid_order ) {
$order = wc_get_order( $unpaid_order );
if ( apply_filters( 'woocommerce_cancel_unpaid_order', 'checkout' === get_post_meta( $unpaid_order, '_created_via', true ), $order ) ) {
$order->update_status( 'cancelled', __( 'Unpaid order cancelled - time limit reached.', 'woocommerce' ) );
}
}
}
wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' );
// do whatever you want here as well...
wp_schedule_single_event( time() + ( absint( $held_duration ) * 60 ), 'woocommerce_cancel_unpaid_orders' );
}