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 }
}
Related
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!
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
I get this code (see below) to create a Product Variation Custom Field. It works just fine. Now I need to use the WP All Import Pro plugin to import Products from a csv file. This plugin allow to create the importation task by mapping the information in the csv to the fields of Product and Product Variation.
Problem: thing is that the new created Product Variation Custom Field is not visible for the WP All Import plugin. I mean, this new field is not listed when I do the mapping of the data be imported.
My assumption is that this code fails to create some data in the database to make this field available for other module.
How can I get this to work properly
I also attached a capture of the WP All Import page where the field should be visible.
/*******************************
add custom fields to product variations
*********************************/
// regular variable products
add_action( 'woocommerce_product_after_variable_attributes', 'add_to_variations_metabox', 10, 3 );
add_action( 'woocommerce_save_product_variation', 'save_product_variation', 20, 2 );
/*
* Add new inputs to each variation
*
* #param string $loop
* #param array $variation_data
* #return print HTML
*/
function add_to_variations_metabox( $loop, $variation_data, $variation ){
$custom = get_post_meta( $variation->ID, '_custom', true ); ?>
<div class="variable_custom_field">
<p class="form-row form-row-first">
<label><?php echo __( 'MY CUSTOM FIELD:', 'plugin_textdomain' ); ?></label>
<input type="text" size="5" name="variation_custom_data[<?php echo $loop; ?>]" value="<?php echo esc_attr( $custom ); ?>" />
</p>
</div>
<?php
}
/*
* Save extra meta info for variable products
*
* #param int $variation_id
* #param int $i
* return void
*/
function save_product_variation( $variation_id, $i ){
// save custom data
if ( isset( $_POST['variation_custom_data'][$i] ) ) {
// sanitize data in way that makes sense for your data type
$custom_data = ( trim( $_POST['variation_custom_data'][$i] ) === '' ) ? '' : sanitize_title( $_POST['variation_custom_data'][$i] );
update_post_meta( $variation_id, '_custom', $custom_data );
}
}
WP ALL IMPORT has this section coded into their files. They have not provide any hook or filter to add fields to this section.
The most important thing here is that WP ALL IMPORT has thought is like this :
As all the Meta for a particular variation is added finally to the post meta table. Which will be retrieved as by get_post_meta. Just like the custom fields. So in order to attach meta to the variation, you can just another Custom Fields with the Name as the name which the actual meta is stored with and with the value you want the variation to be. Just check the image attached. Refer - http://www.wpallimport.com/documentation/custom-fields/theme-plugin-fields/
I know this question was asked almost a year ago but I have been trying to figure out this exact issue out for quite some time now. But in the end I managed to find a workaround that at least will allow you to get that data into the variation instead of the main parent product:
1) In the WpAllImport Wordpress Addon you will be able to see a "variations" tab as shown in your screenshot. Click on this.
2) On this page you will once again not be able to see any custom fields you have made for variations HOWEVER there is a product attributes section.
3) If you add your variation as an attribute and uncheck "Show in variations" and "Taxonomy" and "Is Visible" then you can store your data in an attribute.
4) After the import you should be able to see you data in the attribute, if you know some programming you can even write a script to then loop through all variations and copy this data over to a custom field.
I know it's not much, but for my purposes I was at least able to save the data in the variation which I then used late in a product export plugin.
Hope that helps!
Though this question was initially asked 2 years ago. I found a solution in 2020. I hope this will help anyone who's still looking for a solution.
Please go to All Products and at the top where it says Add New Product you should see two more buttons "Import" and "Export". Click on Export and you will see an option of checkbox "Yes, export all custom meta" Please check this potion and all your custom fields even in variations will be exported.
Thanks
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();
I am trying to get the index-page of a Wordpress-blog show some very specific posts. As far as I understand i need to use a standard loop in order to make sticky posts work, so custom queries is out of the question. (Correct me if this is wrong.)
All posts are put in a main category (Eg. "Make-Up") In addition, posts that should show on the front page gets an additional category "Frontpage".
The current loop outputs all posts, regardless of category. And styles certain categories differently. An example would be the video-category which is only shown by getting an embed code from a custom field in the post.
<?php elseif (in_category('20')) : ?>
<div class="post element grid_4">
<?php echo get_post_meta($post->ID, 'Embed', true) ?>
</div>
I need to remove all posts not in the category "Frontpage" while still being able to control how posts are being shown.
Earlier i used a filter to control the main loop:
function exclude_category($query) {
if ( $query->is_home ) {
$query->set('cat', '20 27');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');
However this would cause my geomashup-plugin to break as it probably uses the same loop?
My current proposal for a solution would be to do something like this, plus functioning code:
<?php elseif (the post is in BOTH category 20 and 27)) : ?>
<div class="post element grid_4">
<?php echo get_post_meta($post->ID, 'Embed', true) ?>
</div>
<?php else : ?>
<div style: display: none;></div>
However i am unsure about how make a condition demanding the post to be in two categories, and i realise this is a terribly dirty fix.
Any tips or pointers as to how i could solve this would be greatly appreciated :)
Front page can be seen here: http://parfymelle.brandbase.no
For anyone wondering i solved it by including the geotagged posts-category (the shop locations) in the filter for the main loop, and then using a php if in the index.php to hide posts from that category. Dirty, but works, i guess.