How to specify Woocommerce product in a script? - wordpress

This code works for me but it makes adjustments to every subscription on the website. I want to target a particular product or array of products so others are unaffected.
that's issue I have with that code on other subscriptions
add_filter( 'woocommerce_subscriptions_product_price_string', 'my_subs_price_string1', 10, 3 );
function my_subs_price_string1( $subscription_string, $product, $include ) {
/****
var_dump($product); Various variables are available to us
****/
return 'Today ' . wc_price( $product->subscription_sign_up_fee ) .
', a ' . $product->subscription_trial_length . ' ' . $product->subscription_trial_period .
' trial of the product, then an outright purchase of ' . wc_price( $product->subscription_price );
}
EDIT:
I tried using a snippet from the post in the answer below. But for some reason, it just removes the string and doesn't replace chosen string.
Here's the snippet.
function wc_subscriptions_custom_price_string( $pricestring ) {
global $product;
$products_to_change = array( 2212 );
if ( in_array( $product->id, $products_to_change ) ) {
$newprice = str_replace( 'on the 20th day of every 6th month', 'on the 20th November and 20th May', $pricestring );
}
return $newprice;
}
add_filter( 'woocommerce_subscriptions_product_price_string', 'wc_subscriptions_custom_price_string' );
add_filter( 'woocommerce_subscription_price_string', 'wc_subscriptions_custom_price_string' );

Try this clean code:
/**
* Modify subscription product price string.
*
* #param string $subscription_string Subscription price text.
* #param WC_Product $product WC Product object.
* #return string
*/
function custom_wc_subscriptions_product_price_string( $subscription_string, $product ) {
// If not product then return original string.
if ( ! $product ) {
return $subscription_string;
}
// Get the product id.
$product_id = $product->get_id();
// Define allowed products ids in array comma seperated.
$allowed_products = array( 1001, 1002, 1003 );
// If allowed products is not empty and product is is within allowed products then only run the inner code.
if ( ! empty( $allowed_products ) && in_array( $product_id, $allowed_products, true ) ) {
$subscription_string = sprintf(
/* translators: 1: Sign-up Fee 2: Trail Length 3: Trail Period 4: Subscription Price. */
__( 'Today %1$s, a %2$s %3$s trial of the product, then an outright purchase of %4$s', 'default' ),
wc_price( $product->subscription_sign_up_fee ),
$product->subscription_trial_length,
$product->subscription_trial_period,
wc_price( $product->subscription_price )
);
}
return $subscription_string;
}
add_filter( 'woocommerce_subscriptions_product_price_string', 'custom_wc_subscriptions_product_price_string', 10, 2 );

Related

Show units sold as word on WooCommerce Archive and Product Page

I am trying to understand how to show single and multiple values for my count.
Basically like this:
"Bought by one customer so far" opposite "Bought by 1 customer so far"
"Bought by five customers so far" opposite "Bought by 5 customers so far"
How can I convert the total sales number to a word/string?
This is the code I am working with:
add_action( 'woocommerce_single_product_summary', 'product_sold_count', 11 );
add_action( 'woocommerce_after_shop_loop_item', 'product_sold_count', 11 );
function product_sold_count() {
global $product;
$units_sold = get_post_meta( $product->get_id(), 'total_sales', true );
if ($units_sold) echo '<span class="bought-by-customers">' . sprintf( __( 'Bought by %s customers so far', 'woocommerce' ), $units_sold ) . '</span>';
}
Which works, but not the way I prefer it. Any advice?
For your question you can use the PHP NumberFormatter class,
You can also use WC_Product::get_total_sales() versus get_post_meta()
So you get:
function product_sold_count() {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get total sales
$units_sold = $product->get_total_sales();
if ( $units_sold >= 1 ) {
// https://www.php.net/manual/en/class.numberformatter.php
$fmt = new NumberFormatter( 'en', NumberFormatter::SPELLOUT );
// Singular are plural
$units_sold == 1 ? $s_p = '' : $s_p = 's';
// Output
echo '<span class="bought-by-customers">' . sprintf( __( 'Bought by %s customer%s so far', 'woocommerce' ), $fmt->format( $units_sold ), $s_p ) . '</span>';
}
}
}
add_action( 'woocommerce_single_product_summary', 'product_sold_count', 11 );
add_action( 'woocommerce_after_shop_loop_item', 'product_sold_count', 11 );
Result:
Bought by thirty-one customers so far
Bought by one customer so far

How can I modify get_price_html to create custom price display for on sale items

this is my first question on Stack Overflow...
I am modifying a WooCommerce website for a client. What they want is for all items which have a sale prices to display a red dot next to the sale price.
I have located the public function 'get_price_html' in abstract-wc-product.php:
/**
* Returns the price in html format.
*
* #param string $deprecated Deprecated param.
*
* #return string
*/
public function get_price_html( $deprecated = '' ) {
if ( '' === $this->get_price() ) {
$price = apply_filters( 'woocommerce_empty_price_html', '', $this );
} elseif ( $this->is_on_sale() ) {
$price = wc_format_sale_price( wc_get_price_to_display( $this, array( 'price' => $this->get_regular_price() ) ), wc_get_price_to_display( $this ) ) . $this->get_price_suffix();
} else {
$price = wc_price( wc_get_price_to_display( $this ) ) . $this->get_price_suffix();
}
return apply_filters( 'woocommerce_get_price_html', $price, $this );
}
I have figured out that if I modify the 'elseif ( $this->is_on_sale()' section, I can do what I want, and this works, but only when I modify and upload the core version of abstract-wc-product.php, which I don't want to do.
I have child theme going, and in my child theme directory, I have copied abstract-wc-product.php into the following folder structure:
woocommerce/includes/abstracts
This isn't working, so I have also tried to copy the function above into my functions.php file, to see if this overrides the core version.
Neither of these is working - can anybody help with how to do this, please?
Thank you!
Stuart
If you can't use CSS, here's how you do it:
/* Here's the woocommerce filter. Then we use a static function (anonymous function) to pass the two arguments that the original filter passes.*/
add_filter( 'woocommerce_get_price_html', static function( $price, $product ) {
// Now we check to see if the product is onsale.
if ( $product->is_on_sale() ) :
// Change this to whatever worked for you when you modified core files.
$price = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), wc_get_price_to_display( $product ) ) . $product->get_price_suffix();
endif;
// Return the $price. This is here in the event the product that is passed isn't on sale.
return $price;
}, 10, 2 );
This goes into your child theme functions.php.
Your key is in return apply_filters( 'woocommerce_get_price_html', $price, $this );
Check the doc here about how to use filters in WordPress: https://developer.wordpress.org/reference/functions/apply_filters/
In your case, I believe you can achieve your goal with this function below - not tested!!
function custom_sale_price( $price, $product ){
if( $product->is_on_sale() ){
return '<span class="red-dot"></span>' . $price;
}
return $price;
}
add_filter('woocommerce_get_price_html', 'custom_sale_price', 10, 2 );

Woocommerce show price incl and excl tax in product catalog

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 );
}

Multiple currencies in woocommerce not showing

I am newbie in Wordpress Plugin Development. I'm facing some issues. One currency payment displayed so how multiple currency payment will display.
Below code is showing one currency.
// Show product prices in fiat + crypto
function mcc_woocommerce_get_price_html( $text, $product )
{
// Is MCC installed?
if ( ! function_exists( 'mycryptocheckout' ) )
return $text;
// Retrieve all of our currencies.
$currencies = MyCryptoCheckout()->currencies();
// Change this to your preferred currency symbol
$show_currency = 'BCH';
$currency = $currencies->get( $show_currency );
// Is this currency known?
if ( ! $currency )
return $text;
$new_price = $currency->convert( get_woocommerce_currency(), $product->get_price() );
return $text . ' / ' . $new_price . ' ' . $show_currency;
}
add_filter( 'woocommerce_get_price_html', 'mcc_woocommerce_get_price_html', 100, 2 );

Add "sub-total" to my-account/orders table

Trying to add the order subtotal in woocommerce my-account/orders table but I can't seem to get it to display. Currently it adds the column and the label but its not displaying the orders sub total. I am currently using the code below :
add_filter( 'woocommerce_account_orders_columns',
'add_account_orders_column', 10, 1 );
function add_account_orders_column( $columns ){
$columns['item_subtotal_tax_excl'] = __( 'Sub-total', 'woocommerce' );
return $columns;
}
add_action( 'woocommerce_my_account_my_orders_column_custom-column',
'add_account_orders_column_rows' );
function add_account_orders_column_rows( $order ) {
// Example with a custom field
if ( $value = $order->get_meta( 'item_subtotal_tax_excl' ) ) {
echo esc_html( $value );
}
}
Subtotal like in cart doesn't exist in WooCommerce Orders meta data, so you need to get it and calculate it from order items:
add_filter( 'woocommerce_account_orders_columns', 'add_account_orders_column', 10, 1 );
function add_account_orders_column( $columns ){
$columns['item_subtotal_tax_excl'] = __( 'Sub-total', 'woocommerce' );
return $columns;
}
add_action( 'woocommerce_my_account_my_orders_column_custom-column', 'display_account_orders_column_rows_value' );
function display_account_orders_column_rows_value( $order ) {
$subtotal = 0; // Initializing
// Loop through order items (line items)
foreach ( $order->get_items() as $item ) {
// Sum item subtotal excluding taxes and not discounted
$subtotal += $item->get_subtotal();
}
echo $subtotal;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Related:
Get Order items and WC_Order_Item_Product in WooCommerce 3
How to get WooCommerce order details
Get the metadata of an order item in woocommerce 3
Edit: the following code worked for me: (answer provided by helgatheviking on woocommerce slack)
//show sub total in order page
add_filter( 'woocommerce_account_orders_columns',
'add_account_orders_column', 10, 1 );
function add_account_orders_column( $columns ){
$columns['item_subtotal_tax_excl'] = __( 'Sub-Total',
'woocommerce' );
return $columns;
}
add_action(
'woocommerce_my_account_my_orders_column_item_subtotal_tax_excl',
'add_account_orders_column_rows' );
function add_account_orders_column_rows( $order ) {
// Example with a custom field
if ( $value = $order->get_subtotal() ) {
echo esc_html( $value );
}
}

Resources