I am working with Woocommerce and Advanced Custom Fields.
I would like to divide the Woocommerce product price by a custom field and then output this as another custom field so it can be shown on the product page.
Example would be a pack of 10 pens, where I also want to show the cost-per-pen as a custom field, allowing customers to compare the price-per-unit across multiple skus.
I believe you could do something simple like:
<?php
$price = $product->get_price_html();
$customfield = get_field('myfield');
// simple maths
$price_piece = $price / $customfield;
?>
<div>
<?php echo $price_piece ;?>
</div>
There is also a helpful page for woocommerce product information
https://www.businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/. I didnt checked the code, but you get the idea, hope that helps!
Related
I need help. I created an additional field in ACF - data_dostawy which I am displaying the text. But I would like it to show me the estimated delivery date. I import the products through WP all import. (there are 5k) from several suppliers with different lead times. That's why I can't set one date for everyone. I would like this function to get the value from the delivery_time field and display the delivery date. For example, today is 09/18/2022, the value in the delivery_time field is 2 so it should say "expected delivery on 09/20/2022. Moreover, I would like to exclude Saturdays and Sundays. Please tell me if such a thing is possible?
My code
add_action( 'etheme_product_single_custom_html_01', 'shipping_date', 20);
function shipping_date() { ?>
<?php if(get_field('czas_dostawy')) { ?>
<div class="sp_czas_dostawy"><?php the_field('czas_dostawy'); ?></div>
<?php }
}
when i create a product, i need to add cross sell and up sell products, but when i start to digit i want that system shows to me only product available.
How do this? I tried something like this How to hide out of stock products from Cross-sells and Upsells products in WooCommerce?
<?php if ( ! $cross_sell->is_in_stock() && ! $cross_sell->backorders_allowed() ) : continue; endif; ?>
but is referred to product frontend page
How to do this?
To the admin: i cannot add some type of code because i did not find anything about this!
I'm trying to display prices of certain WooCommerce products/variations on a regular page. The reason I can't type it manually is these prices are changed in real-time by a plugin with a currency rate.
I found this page:
How to get the price of variable product using variation ID?
In my page builder I inserted a code block with this piece of code:
<?PHP
$product_id = ###;
// ### is the product ID
$_product = wc_get_product( $product_id );
echo $_product->get_sale_price();
?>
or this one
<?PHP
$variation_id = ###;
// ### is the variation ID
$_variation = wc_get_product( $variation_id );
echo $_variation->get_sale_price();
?>
They both worked but the displayed numbers had so many decimal places like 2360.8353159978.
(see example screenshot: https://i.imgur.com/7D4Xydu.png)
I would like to round them up to zero like 2360.
The other issue is they are with no tax. I think that's because I use the price excl. tax on the backend of product pages (Woo shows the price incl. tax on the front end though, which is what I want to achieve here too).
The third issue is numbers don't the comma for thousands.
The perfect result should be 2,596 because 2,360 + 10% tax = 2,596
I'm very new to PHP and have sought the answer for a long time.
Can anybody kindly give me some suggestions? Any input is appreciated.
I'm not sure about the tax, but here is the solution for trimming the decimals:
/** Trim zeros in price decimals **/
add_filter( 'woocommerce_price_trim_zeros', '__return_true' );
add_filter( 'woocommerce_adjust_non_base_location_prices', '__return_false' );
I've found the answer to my own question. See the codes below:
<?PHP
$variation_id = ###;
$_variation = wc_get_product( $variation_id );
echo wc_price($_variation->get_price_including_tax(), array('decimals' => 0));
?>
You can replace the variation_id with product_id too as long as you can find them.
So I needed to use the wc_price to get the formatted price numbers, which is the way you set the thousand separator and no. of decimal, etc in WooCommerce' General tab. For rounding, since I've created a way to round the numbers in WooCommerce (similar to function.php in the child theme) so wc_price will call and show the rounded result as well.
The format still needs to be tweaked on my WordPress page e.g. I like to remove all decimals so I used array('decimals' => 0) to truncate them.
Text style also needs to be fixed with CSS but I'm not to show that here since it's not relevant to the topic.
Anyway the problem is solved. Hope this code is useful to other people. Again I know very very little about PHP it's just from my searching on Google. Anyone who likes to improve the code please share.
Best,
Acon
Is there a way to customise a wordpress blog post so that it changes slightly based on the User?
eg. when showing a post around Salary and Bonuses on the company blog, I'd like to customise some of the text based on the employee level (director, Executive etc) - for example:
a paragraph of text about pension contribution should only appear to
directors reading the page
the bonus amount should change based on employee level.
I don't want to create several pages per category (eg. one page for the directors, one for the execs etc) - I would like to have just one blog post with some variable fields?
The codex has a great function outlined, current_user_can
http://codex.wordpress.org/Function_Reference/current_user_can
As well as WP_User_Query
http://codex.wordpress.org/Class_Reference/WP_User_Query
You can use both of these to accomplish your goal.
I needed to do something similar, I came up with custom roles based on the plugin "Capability Manager Enhanced".
In a file you can check the roles like:
<? if ( is_user_logged_in() ) : ?>
<?
global $current_user;
get_currentuserinfo();
$roles = $current_user->roles; //$roles is an array
if ( is_user_logged_in() && in_array("custom_role", $roles) ) {
echo('WORK HARDER!');
} else {
echo('NO CAKE FOR YOU!');
}
?>
<?php endif; ?>
I need to display the item price in emails sent to admin and to customers using WooCommerce.
The default templates display Item, Quantity, and Price (item price X quantity). I can add the columns to the tables, but don't know how to get the data from the product.
(It is interesting that this has not been included in the default. I dont think i've ever placed an order online that did not include the single item price.)
This post was helpful, but didn't give me quite all needed:
display tax in woocommerce invoice
It has been a while but this may come handy to someone else.
As far as I am aware, there is no method that returns the unit price, but you can just calculate it by dividing the total price of the item by its quantity like so:
$item->get_total() / $item->get_quantity()
Since this will return just a number, you would probably want to add a currency symbol, so, you would end up with something like this:
<?php echo wc_price($item->get_total() / $item->get_quantity()); ?>
I edited plugins/woocommerce/templates/emails/email-order-items.php
After product name I inserted the price:
<br/>
<strong><?php _e( 'Price', 'woocommerce' ); ?>:</strong>
<?php echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key ); ?>
NB! I put my custom files in directory mytheme/woocommerce/emails/ - without "templates" folder (!). Strange but it's the only way to force them working.
Looks like #Anunja's code up above may no longer work - instead just add the following line underneath Product Name in a copied version of templates/emails/email-order-items.php
echo '<br/>Individual Price $' . $_product->get_price();