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 );
Related
I'm using the below code to show customers' phone numbers in the order list. I want also to show the variation name of the purchased product below the phone number
I'm using this snippet. Can anyone help me add the variation name in the same order column?
add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 50, 2 );
function custom_orders_list_column_content( $column, $post_id ) {
if ( $column == 'order_number' )
{
global $the_order;
if( $phone = $the_order->get_billing_phone() ){
$phone_wp_dashicon = '<span class="dashicons dashicons-phone"></span> ';
echo '<br>' . $phone_wp_dashicon . $phone.'</strong>';
}
if( $email = $the_order->get_billing_email() ){
echo '<br><strong>' . $email . '</strong>';
}
}
}
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 );
I'm trying to add an extra shipping cost for all the additional shipping fees that aren't calculated from the third party shipping
company:
//add additional extra cost to shipping except for local pickup
add_filter( 'woocommerce_package_rates', 'shipping_extra_cost' );
function shipping_extra_cost( $rates ) {
foreach($rates as $key => $rate ) {
$rates[$key]->cost = $rates[$key]->cost + get_field('extra_cost', "51");
}
return $rates;
}
But then the additional fee is also added on local shipping
which is wrong.
I can't work with WC shipping classes because that messes with the third party shipping calculation program.
Is there a way I can check if "local pickup" exists and then exclude the extra fee from it?
You can exclude 1 or more shipping methods by using $rate->method_id
Note: because get_field() only applies when using the ACF plugin I replaced it with a fixed number, adjust to your needs
So you get:
function filter_woocommerce_package_rates( $rates, $package ) {
//$field = get_field( 'extra_cost', '51' );
$field = 50;
// Multiple can be added, separated by a comma
$exclude = array( 'local_pickup' );
// Loop through
foreach ( $rates as $rate_key => $rate ) {
// Targeting
if ( ! in_array( $rate->method_id, $exclude ) ) {
// Set the new cost
$rates[$rate_key]->cost += $field;
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates','filter_woocommerce_package_rates', 10, 2 );
For debugging purposes you can temporarily use:
function filter_woocommerce_cart_shipping_method_full_label( $label, $method ) {
// Getters
$id = $method->id;
$method_id = $method->method_id;
// Output
$label .= '<span style="color:red; font-size:20px; display:block;">Id = ' . $id . '</span>';
$label .= '<span style="color:red; font-size:20px; display:block;">Method id = ' . $method_id . '</span>';
return $label;
}
add_filter( 'woocommerce_cart_shipping_method_full_label', 'filter_woocommerce_cart_shipping_method_full_label', 10, 2 );
Result:
By default WooCommerce shows the attribute of a variable product in the title and I'm using this code to show the attribute below the title in the cart and checkout pages:
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );
But that doesn't work in My Account page, users see the full product name with no attribute.
To fix it I'm using the code below to show the attribute in the product title:
function show_attributes_outside_title_1( $enabled ) {
if ( !is_account_page() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_product_variation_title_include_attributes', 'show_attributes_outside_title_1' );
function show_attributes_outside_title_2( $enabled ) {
if ( !is_account_page() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_is_attribute_in_product_name', 'show_attributes_outside_title_2' );
But I'd like to show the attribute below the title (or a new column), it's easier to read and goes with the same desing you see in the cart and checkout pages.
There is some confusion in the initial part of the question.
You say you want to show the attribute under the product title on the cart and checkout page but then return __return_false, do you intend to do the opposite?
SOLUTION #1
You may want to reverse the check to make sure that your chosen product variation attribute is shown under the product name on your account page under Downloads (as evidenced by your comment above):
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );
add_filter( 'woocommerce_product_variation_title_include_attributes', 'show_attributes_outside_title_1' );
function show_attributes_outside_title_1( $enabled ) {
if ( is_account_page() ) {
$enabled = true;
}
return $enabled;
}
add_filter( 'woocommerce_is_attribute_in_product_name', 'show_attributes_outside_title_2' );
function show_attributes_outside_title_2( $enabled ) {
if ( ! is_account_page() ) {
$enabled = false;
}
return $enabled;
}
SOLUTION #2
If you want to leave the code in your question unchanged you can use the woocommerce_account_downloads_column_download-product hook where download-product is the id of the product name column (in the /my-account/downloads/ page). Here the documentation.
Finally, with the wc_get_formatted_variation function you can get the name of the chosen variation. For more information on parameters read the documentation.
// shows the variation chosen in the product name in the download table of the my-account page
add_action( 'woocommerce_account_downloads_column_download-product', 'change_product_download_name', 10, 1 );
function change_product_download_name( $download ) {
// gets the product object
$product = wc_get_product( $download['product_id'] );
// gets the name of the produc
$product_name = $download['product_name'];
// if the product is a variation
if ( $product->is_type( 'variation' ) ) {
// gets the name of the product with the chosen variation
$product_name = $product->get_name() . " - " . wc_get_formatted_variation( $product, true, false, false );
}
// print the product name (with or without product url)
if ( $download['product_url'] ) {
echo '' . esc_html( $product_name ) . '';
} else {
echo esc_html( $product_name );
}
}
The code has been tested and works. Add it to your active theme's functions.php.
I changed Vincenzo's answer a bit to make it look the same way I see the attributes in my Cart and Checkout pages. Here's the code in case anybody else needs it:
// Shows the variation chosen in the product name in the download table of the my-account page
add_action( 'woocommerce_account_downloads_column_download-product', 'change_product_download_name', 10, 1 );
function change_product_download_name( $download ) {
// gets the product object
$product = wc_get_product( $download['product_id'] );
// gets the name of the product
$product_name = $download['product_name'];
// define variable
$product_attributes = '';
// if the product is a variation
if ( $product->is_type( 'variation' ) ) {
// gets the name of the product with the chosen variation
$product_name = $product->get_name();
$product_attributes = wc_get_formatted_variation( $product, true, true, false );
}
// print the product name (with or without product url)
if ( $download['product_url'] ) {
echo '' . esc_html( $product_name ) . '<p>' . esc_html( $product_attributes ) . '</p>';
} else {
echo esc_html( $product_name ) . '<p>' . esc_html( $product_attributes ) . '</p>';
}
}
// Shows variation outside title in cart and checkout pages
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );
The last two filters replace my code and the first part solves the issue in the Downloads page.
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 );
}