I want to remove dollar sign "$" from the product price in a certain section of single-product page. Here's my code which is not working.Please help.
global $product;
$price = $product->get_price_html();
$filter_price = preg_replace('/.00/', '', $price);
$new_price = str_replace( '$', '', $filter_price );
echo $new_price;
You could do this:
$price = $product->get_price();
$new_price = str_replace( '$', '', $price );
echo $new_price;
If you want to get the price in a product template then you dont have to declare global $product again. Also, this will get you the actual price of the product before the apply of VAT. You can calculate the VAT manually afterwards.
Related
I would like to show the regular price before the discounted price in the add to cart button on the single product pages only.
Here's the code snippet I added to the functions.php file of my child theme:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text' );
function woocommerce_custom_single_add_to_cart_text() {
global $product;
$regular_price = woocommerce_price( $_product->get_regular_price() );
$sale_price = woocommerce_price( $_product->get_sale_price() );
return __( 'Add to cart' . $regular_price . $sale_price, 'woocommerce' );
}
This code is incorrect and crashes the website but I don't understand why. Can anyone help me fix it?
The code crashes because you use a variable $_product which is not defined inside your hook. Please try this code instead:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'filter_woocommerce_product_single_add_to_cart_text' );
function filter_woocommerce_product_single_add_to_cart_text(): string {
global $product;
$regular_price = wc_price( $product->get_regular_price() );
$sale_price = wc_price( $product->get_sale_price() );
return __( 'Add to cart', 'woocommerce' ) . $regular_price . $sale_price;
}
I've fixed your variable name $_product to $product (as defined inside a global) and moved your price to text assignment outside the translation function. Also, I've preplaced the deprecated function woocommerce_price with wc_price.
This way, your code works but don't display the price correctly since this filter filters only the text as a string.
I think you need to check for a different hook to achieve your goal.
I want to show the product price including and exclunding tax under each product in the catalog page of my Woocommerce shop.
Its already working, but it is not showing anything for variable products where I have only one variation. On single products it is also working.
Also I do get the notification:
Notice: WC_Product::get_price_including_tax ist seit Version 3.0 veraltet! Benutze stattdessen wc_get_price_including_tax.
Notice: WC_Product::get_price_excluding_tax ist seit Version 3.0 veraltet! Benutze stattdessen wc_get_price_excluding_tax.
But if I do so, it is not working anymore at all.
add_action( 'woocommerce_after_shop_loop_item_title', 'preise_notice', 10 );
function preise_notice() {
global $product;
if ( $price_html_incl_tax = $product->get_price_including_tax() )
if ( $price_html_excl_tax = $product->get_price_excluding_tax() ) {
echo '<div class="product-prices-excl-vat"><a>ab ' . wc_price($price_html_excl_tax) . ' netto</a></div>';
echo '<div class="product-prices-incl-vat"><a>(' . wc_price($price_html_incl_tax) . ' inkl. 19% MwSt.)</a></div>';
}
}
The wc_get_price_including_tax and wc_get_price_excluding_tax functions expect $product as an argument. So you will have to pass it like this:
wc_get_price_including_tax( $product )
Also it seems like a good idea to get the product's tax rate instead of hard coding it in. Maybe in the future you will have products that do not have a 19% tax rate. I also included the currency argument to the wc_price function so the price will be shown in the shop's currency.
You can use the following snippet that will get the product's tax rate and prints the prices including and excluding tax:
add_action( 'woocommerce_after_shop_loop_item_title', 'add_product_price_incl_and_excl_tax', 10 );
function add_product_price_incl_and_excl_tax() {
global $product;
$tax_rate = '';
$tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
//Check the product tax rate
if ( !empty( $tax_rates ) ) {
$tax_rate = reset($tax_rates);
$tax_rate = sprintf( ' inkl. %.0f%% MwSt.', $tax_rate['rate'] );
}
//Print product prices including tax and tax percentage, and excluding tax
printf( '<div class="product-prices-excl-vat">ab %s netto</div>', wc_price( wc_get_price_excluding_tax( $product ), array( 'currency' => get_woocommerce_currency() ) ) );
printf( '<div class="product-prices-incl-vat">%s%s</div>', wc_price( wc_get_price_including_tax( $product ), array( 'currency' => get_woocommerce_currency() ) ), $tax_rate );
}
Using woocommerce, I have several categories but would like to add "per person" to the category "cookery classes".
I have written this function, which works on all products.
I can't seem to get it to work only on the category.
'/*---------Adds per person to price -------*/
add_filter( 'woocommerce_get_price_html', 'custom_price_html', 500, 2 );
function custom_price_html( $price, $product ) {
if (( int ) is_category( 'Cookery Classes','product_cat', $product->id ) )
$price .='per person';
$price .= '<span class="from"><BR>' . _x('per person', 'price_per_person','woocommerce') . ' </span>';
return $price;
}'
found and installed this plugin:
https://wordpress.org/plugins/woocommerce-jetpack/
Allows you to add custom labels to prices per product catagory
I have a WordPress site that uses WooCommerce plug-in. When a discount is applied to a product, the cart displays the price as crossed out with the discounted price at the bottom.
I would like to have a message with the last date of the discount, something like "$100-early bird rate applies up to 2015-09-30". How would I tweak the discount display on the cart or is there a plug-in for this?
Thanks in advance
Sale end date is stored as product meta, you can pull it using get_post_meta , the filter to use is get_price_html
add_filter( 'woocommerce_get_price_html', 'so32504214_price_html', 20, 2 );
function so32504214_price_html( $price, $product ){
$date = get_post_meta( $product->id, '_sale_price_dates_to', true );
if( strstr( $price, "<ins>" ) && "" != $date ) {
$date = date("Y-m-d", $date);
return str_replace( '</ins>', ' </ins> - early bird rate applies up to '. $date , $price );
}
return $price;
}
As the title says, how do i echo the stock quantity of a particular product id? Let's say the product id is "1100" and i want the stock quantity to appear in a page that is not part of the product archive. Is that possible?
For example, in an empty page, it should appear as
"Product A leftover stock: 10"
I am just learning to code and have come up with the following, but no result appear:
function nntest(){
global $woocommerce;
global $product;
$product_id = 1100;
echo $product->get_stock_quantity();
}
Thanks!
You can use get_post_meta() function to get values from database.
this values are stored in wp_postmeta table.
$stock = get_post_meta( $post->ID, '_stock', true );
Another option if you want to work with the actual WC_Product class:
$product_id = 1100;
$product = wc_get_product($product_id);
echo $product->get_stock_quantity();
If you just want to check whether the product is in stock or out of stock use:
$stock = get_post_meta( $post->ID, '_stock_status', true );
It would either return 'instock' or 'outofstock' depending on the stock status.
If you have enabled the option "Enable stock management at product level" and you want stock quantity, use:
$stock = get_post_meta( $post->ID, '_stock', true );