I add one product for e.g “ABC” – price : $10 (including VAT 20%)
So product net price = £8 and tax = £2
In frontend on product detail page i show product price including TAX.($10)
When Customer purchase this product then in woocommerce order generate like this
product price = $8
tax = $2
-------------------
Total = $10
But i want price in admin order detail page like
product price : $10(including 20% VAT)
Not want to show VAT separate.
Thanks
Try this. Put it in your theme function.php file
//Add price inc VAT column on admin order page
function action_woocommerce_admin_order_item_values( $null, $item, $absint ) {
$val = ($item['type'] == 'line_item' || $item['type'] == 'shipping') ? $item['total'] + $item['total_tax'] : ' ';
$valdecimal = wc_format_decimal( $val, $dp='', $trim_zeros );
?>
<td class="item_fcost" data-sort-value="<?php echo $val; ?>">
<div class="view" style="font-weight: bold; text-align: right; padding-right: 10px;">
<?php if ($val>0) echo '$'; echo $valdecimal;?>
</div>
</td>
<?php
};
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );
function action_woocommerce_admin_order_item_headers( $order ) {
echo '<th class="item_fcost sortable" data-sort="float" style="text-align: right;">Price inc VAT</th>';
};
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 3 );
I updated the code above slightly, so it also works with fees and negative amounts:
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );
function action_woocommerce_admin_order_item_values( $null, $item, $item_id ) {
$validOrderItemTypes = ['line_item', 'shipping', 'fee'];
$val = ( in_array( $item['type'], $validOrderItemTypes )) ? $item['total'] + $item['total_tax'] : '−';
$price = wc_price( $val );
?>
<td class="item_fcost" data-sort-value="<?php echo $val; ?>">
<div class="view" style="font-weight: bold; text-align: right; padding-right: 10px;">
<?php echo $price; ?>
</div>
</td>
<?php
};
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 3 );
function action_woocommerce_admin_order_item_headers( $order ) {
echo '<th class="item_fcost sortable" data-sort="float" style="text-align: right;">Price incl. VAT</th>';
};
Related
I am trying to show the price with VAT included in woocommerce orders backoffice but i am getting this error when i try to divide the total by the quantity.
The total amount it shows without any problem, but if i add the division gives me this error.
Any help with this?
function action_woocommerce_admin_order_item_values( $null, $item, $absint ) {
$val = ($item['type'] == 'line_item' || $item['type'] == 'shipping' ) ? ($item['total'] + $item['total_tax']) / $item['quantity'] : ' ';
$valdecimal = wc_format_decimal( $val, $dp='', $trim_zeros );
?>
<td class="item_fcost" data-sort-value="<?php echo $val; ?>">
<div class="view" style="font-weight: bold; text-align: right; padding-right: 10px;">
<?php if ($val>0) echo '€'; echo $valdecimal;?>
</div>
</td>
<?php
};
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );
function action_woocommerce_admin_order_item_headers( $order ) {
echo '<th class="item_fcost sortable" data-sort="float" style="text-align: right;">Preço com IVA</th>';
};
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 3 );
SOLVED
$quantity = $item->get_quantity();
$val = ($item['type'] == 'line_item' || $item['type'] == 'shipping' ) ? ($item['total'] + $item['total_tax']) / $quantity : ' ';
Hi I am trying to add an upload image field to the woocommerce register form and then get this to display in the admin area in the backend. I am using the code below which give the image upload on register form but having trouble trying to get it to display within the admin area.
//// ADD IMAGE UPLOAD TOO REGSITER
// Add enctype to form to allow file upload
function AddEnctypeCustomRegistrationForms() {
echo 'enctype="multipart/form-data"';
}
add_action('woocommerce_register_form_tag','AddEnctypeCustomRegistrationForms');
// Add file input html to register form
add_action('woocommerce_register_form','AddImageField');
function AddImageField() {
?>
<p class="form-row validate-required" id="pro_image" data-priority="">
<label for="pro_image" class="">Upload Picture ID<abbr class="required" title="required">*</abbr></label>
<span class="woocommerce-input-wrapper">
<input type='file' name='pro_image' accept='image/*,.pdf' required>
</span>
</p>
<?php
}
// Validate new fields
function ValidateImageField( $errors, $username, $email ) {
if ( isset( $_POST['pro_image'] ) && empty( $_POST['pro_image'] ) ) {
$errors->add('pro_image_error', __( 'Please provide a valid image', 'woocommerce' ) );
}
return $errors;
}
add_filter('woocommerce_registration_errors','ValidateImageField',10,3 );
// Save new field
function SaveImageField( $customer_id ) {
if ( isset( $_FILES['pro_image'] ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$attach_id= media_handle_upload('pro_image', 0 );
if ( is_wp_error( $attach_id) ) {
update_user_meta( $customer_id,'pro_image', $_FILES['pro_image'] . ": " . $attach_id->get_error_message() );
} else {
update_user_meta( $customer_id,'pro_image', $attach_id);
}
}
}
add_action('user_register','SaveImageField',1);
I have tried a few different things but not having any luck, here is one of them.
function add_SaveImageField( $user ) {
?>
<h3><?php _e('ID','woocommerce' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="SaveImageField_field"><?php _e( 'ID', 'woocommerce' ); ?></label></th>
<td><input type="image" name="SaveImageField_field" value="<?php echo esc_attr( get_the_author_meta( 'pro_image', $user->ID )); ?>" class="pro_image" /></td>
</tr>
</table>
<br />
<?php
}
add_action( 'show_user_profile', 'add_SaveImageField', 10, 1 );
add_action( 'edit_user_profile', 'add_SaveImageField', 10, 1 );
Any help would be much apricated
Thanks
You have to do some changes to your code
// Save new field
function SaveImageField( $customer_id ) {
if ( isset( $_FILES['pro_image'] ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
//Code changes
foreach ($_FILES as $file => $array){
if ($file == 'pro_image') {
if (!empty($array['name'])) {
$attach_id = media_handle_upload($file,$new_post);
update_user_meta( $customer_id, $file, $attach_id );
}
}
}
//End Code changes
}
}
add_action('user_register','SaveImageField',1);
function add_SaveImageField( $user ) {
//Code changes
$thumbnail_id = get_the_author_meta( 'pro_image', $user->ID );
$image_url = '';
if (!empty($thumbnail_id) && $thumbnail_id) {
$image_size = 'full'; // (thumbnail, medium, large, full or custom size)
$image_attachment_data_arr = wp_get_attachment_image_src( $thumbnail_id, $image_size );
$image_url = $image_attachment_data_arr[0];
}
//EndCode changes
?>
<h3><?php _e('ID','woocommerce' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="SaveImageField_field"><?php _e( 'ID', 'woocommerce' ); ?></label></th>
<td>
<!--Code changes-->
<?php if (!empty($image_url)) { ?>
<img src="<?php echo $image_url;?>" style="width: 200px;">
<?php }else{ ?>
<h3>Image Not uploaded</h3>
<?php } ?>
<!--End Code changes-->
</td>
</tr>
</table>
<br />
<?php
}
add_action( 'show_user_profile', 'add_SaveImageField', 10, 1 );
add_action( 'edit_user_profile', 'add_SaveImageField', 10, 1 );
I added this code below to add prices including VAT in the admin order page.
It work well but I tried to refund an order today and I got this error:
Fatal error: Uncaught Error: Cannot use object of type WC_Order_Refund as array in
How can I exclude this new price from the refund process ?
This custom price should not be submitted once I click on refund
//Add price inc VAT column on admin order page
function action_woocommerce_admin_order_item_values( $null, $item, $absint ) {
$val = ($item['type'] == 'line_item' || $item['type'] == 'shipping') ? $item['total'] + $item['total_tax'] : ' ';
$valdecimal = wc_format_decimal( $val, $dp='', $trim_zeros );
?>
<td class="item_fcost" data-sort-value="<?php echo $val; ?>">
<div class="view" style="font-weight: bold; text-align: right; padding-right: 10px;">
<?php if ($val>0) echo '$'; echo $valdecimal;?>
</div>
</td>
<?php
};
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );
function action_woocommerce_admin_order_item_headers( $order ) {
echo '<th class="item_fcost sortable" data-sort="float" style="text-align: right;">Price inc VAT</th>';
};
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 3 );
You can quit early if the incoming $item variable is a WC_Order_Refund object.
if ( 'WC_Order_Refund' == gettype( $item ) ) {
return;
}
Here's the updated code that does not generate any errors:
//Add price inc VAT column on admin order page
function action_woocommerce_admin_order_item_values( $null, $item, $absint ) {
$val = ('line_item' == $item->get_type() || 'shipping' == $item->get_type() ) ? $item->get_total() + $item->get_total_tax() : ' ';
$valdecimal = wc_format_decimal( $val );
?>
<td class="item_fcost" data-sort-value="<?php echo $val; ?>">
<div class="view" style="font-weight: bold; text-align: right; padding-right: 10px;">
<?php if ($val>0) echo '$'; echo $valdecimal;?>
</div>
</td>
<?php
};
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );
function action_woocommerce_admin_order_item_headers( $order ) {
echo '<th class="item_fcost sortable" data-sort="float" style="text-align: right;">Price inc VAT</th>';
};
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 3 );
I have 2 functions. One is to calculate fee (here is part from it that is responsible for calculating fee and saving it into global variable):
function pre_checkout()
{
global $woocommerce;
$section2 ='';
$section3 ='';
$section2 = sanitize_text_field($_POST["user_data"]["section2"]);
$section3 = sanitize_text_field($_POST["user_data"]["section3"]);
$how_many_months = preg_replace('/\D/', '', $_POST["user_data"]["section1"]);
$boxToFind = '';
if(($section2 == 'toy_yes') || ($section2 == 'toy_no'))
{
$boxToFind = sanitize_text_field($_POST["user_data"]["section1"]);
}
else{
$boxToFind = sanitize_text_field($_POST["user_data"]["section1"].$_POST["user_data"]["section2"]);
}
WC()->cart->empty_cart(true);
$boxesLookupTable = array(
//susbscriptions
'small1month'=>'112',
'medium1month'=>'113',
'large1month'=>'114',
'small6month'=>'115',
'medium6month'=>'116',
'large6month'=>'117',
'small12month'=>'118',
'medium12month'=>'119',
'large12month'=>'120',
//gifts
'gift1month'=>'133',
'gift3month'=>'134',
'gift6month' =>'135',
'gift12month' =>'136'
);
$productId = $boxesLookupTable[$boxToFind];
// echo $productId;
//check if product already in cart
// if no products in cart, add it
WC()->cart->add_to_cart($productId,1);
if($section2 === "toy_yes"){
$product = wc_get_product($productId);
$price = $product->get_price();
$toy_price = 4.99;
unset($_COOKIE['product_price_with_toy']);
setcookie('product_price_with_toy',$toy_price*$how_many_months,time() + (86400 * 30),"/");
// WC_Cart::add_fee( 'Gift Fee ', $product_price_with_toy,false, false );
/* "gift"; */
}
if($section3 === "toy_yes"){
$how_many_months = preg_replace('/\D/', '', $_POST["user_data"]["section2"]);
$product = wc_get_product($productId);
$price = $product->get_price();
$toy_price = 4.99;
unset($_SESSION['product_price_with_toy']);
$_SESSION['product_price_with_toy'] = $price + $toy_price;
unset($_COOKIE['product_price_with_toy']);
setcookie('product_price_with_toy',$how_many_months * $toy_price,time() + (86400 * 30),"/");
// WC_Cart::add_fee( 'Gift Fee ', $product_price_with_toy,false, false );
/*"subscription"; */
};
$items = WC()->cart->get_cart();
global $woocommerce;
$cart_url = wc_get_cart_url();
$checkout_url = wc_get_checkout_url();
foreach($items as $item => $values) {
$_product = $values['data']->post;
?>
<div class="dropdown-cart-wrap">
<div class="dropdown-cart-left">
<?php echo get_the_post_thumbnail( $values['product_id'], 'thumbnail' ); ?>
</div>
<div class="dropdown-cart-right">
<h5><?php echo $_product->post_title; ?></h5>
<p><strong>Quantity:</strong>
<?php $product = wc_get_product($productId);
woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ) );
?></p>
<?php global $woocommerce;
$currency = get_woocommerce_currency_symbol();
$price = get_post_meta( $values['product_id'], '_regular_price', true);
$sale = get_post_meta( $values['product_id'], '_sale_price', true);
?>
<?php if($sale) { ?>
<p class="price"><strong>Price:</strong> <del><?php echo $currency; echo $price; ?></del> <?php echo $currency; echo $sale; ?></p>
<?php } elseif($price) { ?>
<p class="price"><strong>Price:</strong> <?php echo $currency; echo $price; ?></p>
<?php }
?>
<div class="dropdown-cart-right dropdown-checkout-link">
<a class="checkout-submit checkout green-btn text-center" href="#">Order</a>
</div>
</div>
<div class="clear"></div>
</div>
<?php }
die();
}
add_action('wp_ajax_pre_checkout','pre_checkout');
add_action('wp_ajax_nopriv_pre_checkout', 'pre_checkout');
Next i have woocommerce action:
function add_cart_fee() {
$chuj = $GLOBALS['product_price_with_toy'];
WC()->cart->add_fee( __('Extra Gift Fee', 'woocommerce'),$chuj );
}
add_action( 'woocommerce_cart_calculate_fees', 'add_cart_fee' );
And basicaly toy fee is always 0 when im using session, global variables - only through cookies is passing values to action - i dont understand why woocommerce / wordpress is wiping global value that ive set inside my calculation function. Or maybe someone know how to update the total price with using hooks and actions, that would be even better than using fee field.
I am working with a custom theme and would like to add a custom upload field in the woocommerce admin section. Particularly, the categories section. I want to add another field just like the thumbnail upload that stores the information.
raunak-gupta in Adding custom field to product category in WooCommerce shares a way forward which I took on and produced this. HELP???
Added uploader.js
jQuery(document).ready( function($){
var mediaUploader_woo;
$('#upload-button-woo').on('click',function(e) {
e.preventDefault();
if( mediaUploader_woo ){
mediaUploader_woo.open();
return;
}
mediaUploader_woo = wp.media.frames.file_frame = wp.media({
title: 'Choose an Image',
button: { text: 'Choose Image'},
multiple: false
});
mediaUploader_woo.on('select', function(){
attachment = mediaUploader_woo.state().get('selection').first().toJSON();
$('#meta-woo').val(attachment.url);
});
mediaUploader_woo.open();
});
});
I called the media uploader in functions.php
function my_admin_scripts() {
wp_enqueue_media();
wp_register_script( 'wina_classic-uadmin-js', get_template_directory_uri() . '/js/uploader.js', array('jquery','media-upload','thickbox'), '20130115', true );
wp_enqueue_script( 'wina_classic-uadmin-js');
}
add_action('admin_print_scripts', 'my_admin_scripts');
then voila..
/*-------------------------------------------------------------------
Add Custom metabox for woocommerce Category page
---------------------------------------------------------------------*/
function product_cat_add_video_field_rj() {
?>
<div class="form-field">
<label for="term_meta[video_link]"><?php _e( 'Video Link', 'flatsome' ); ?></label>
<input type="text" name="term_meta[video_link]" id="term_meta[video_link]" value="">
<p class="description"><?php _e( 'Enter a Video Link','flatsome' ); ?></p>
</div>
<?php
}
function product_cat_edit_video_field_rj($term) {
$t_id = $term->term_id;
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[video_link]"><?php _e( 'Video Link', 'flatsome' ); ?></label></th>
<td>
<input type="text" name="term_meta[video_link]" id="meta-woo" value="<?php echo esc_attr( $term_meta['video_link'] ) ? esc_attr( $term_meta['video_link'] ) : ''; ?>" style="margin-left: 0px; margin-right: 0px; width: 50%;" />
<input type="button" class="button button-secondary" value="Upload Image" id="upload-button-woo" />
<p class="description"><?php _e( 'Enter a Video Link','flatsome' ); ?></p>
</td>
</tr>
<?php
}
// this action use for add field in add form of taxonomy
add_action( 'product_cat_add_form_fields', 'product_cat_add_video_field_rj', 10, 2 );
// this action use for add field in edit form of taxonomy
add_action( 'product_cat_edit_form_fields', 'product_cat_edit_video_field_rj', 10, 2 );
function product_cat_video_link_save( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
update_option( "taxonomy_$t_id", $term_meta );
}
}
// this action use for save field value of edit form of taxonomy
add_action( 'edited_product_cat', 'product_cat_video_link_save', 10, 2 );
// this action use for save field value of add form of taxonomy
add_action( 'create_product_cat', 'product_cat_video_link_save', 10, 2 );
PS: This code does not store or reproduce the saved data in the text field.
A few hours later, I came up with this code below
Add function to functions.php
//call for woocommerce custom admin image code
require get_template_directory() . '/inc/woo-meta-category.php';
/*--------------------------------------------------------------------------------------
Uploader JS
----------------------------------------------------------------------------------------*/
function my_admin_scripts() {
wp_enqueue_media();
wp_register_script( 'wina_classic-uadmin-js', get_template_directory_uri() . '/js/uploader.js',
array('jquery','media-upload','thickbox'), '20130115', true );
wp_enqueue_script( 'wina_classic-uadmin-js');
}
add_action('admin_print_scripts', 'my_admin_scripts');
Add Media uploader Javascript in js/uploader.js
jQuery(document).ready( function($){
var mediaUploader_woo;
$('#upload-button-woo').on('click',function(e) {
e.preventDefault();
if( mediaUploader_woo ){
mediaUploader_woo.open();
return;
}
mediaUploader_woo = wp.media.frames.file_frame = wp.media({
title: 'Choose an Image',
button: { text: 'Choose Image'},
multiple: false
});
mediaUploader_woo.on('select', function(){
attachment = mediaUploader_woo.state().get('selection').first().toJSON();
$('#category-meta-woo').val(attachment.url);
$('#category-header-preview').attr('src', ''+ attachment.url + '' );
});
mediaUploader_woo.open();
});
});
Finally add /inc/woo-meta-category.php file with code below
<?php
/*-------------------------------------------------------------------
Add Custom metabox for woocommerce Category page
---------------------------------------------------------------------*/
function product_cat_add_cat_head_field_rj() { ?>
<div class="form-field">
<label for="term_meta[cat_head_link]"><?php _e( 'Category Page Image', 'wina-classic' ); ?></label>
<input type="text" name="term_meta[cat_head_link]" id="term_meta[cat_head_link]" value="">
<p class="description"><?php _e( 'Upload Category Page Image','wina-classic' ); ?></p>
</div>
<?php }
function product_cat_edit_cat_head_field_rj($term) {
$t_id = $term->term_id; $term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[cat_head_link]"><?php _e( 'Category Page Image', 'wina-classic' ); ?></label></th>
<td>
<img src="<?php echo esc_attr( $term_meta['cat_head_link'] ) ? esc_attr( $term_meta['cat_head_link'] ) : ''; ?>" height="60" width="120" id="category-header-preview" />
<input type="hidden" name="term_meta[cat_head_link]" id="category-meta-woo" value="<?php echo esc_attr( $term_meta['cat_head_link'] ) ? esc_attr( $term_meta['cat_head_link'] ) : ''; ?>" style="margin-left: 0px; margin-right: 0px; width: 50%;" />
<input type="button" class="button button-secondary" value="Upload Image" id="upload-button-woo" />
<p class="description"><?php _e( 'Upload Category Page Image','wina-classic' ); ?></p>
</td>
</tr>
<?php
}
// this action use for add field in add form of taxonomy
add_action( 'product_cat_add_form_fields', 'product_cat_add_cat_head_field_rj', 10, 2 );
// this action use for add field in edit form of taxonomy
add_action( 'product_cat_edit_form_fields', 'product_cat_edit_cat_head_field_rj', 10, 2 );
function product_cat_cat_head_link_save( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
update_option( "taxonomy_$t_id", $term_meta );
}
}
// this action use for save field value of edit form of taxonomy
add_action( 'edited_product_cat', 'product_cat_cat_head_link_save', 10, 2 );
// this action use for save field value of add form of taxonomy
add_action( 'create_product_cat', 'product_cat_cat_head_link_save', 10, 2 );
In the front end
global $post;
//for product cat archive page only
$term = get_queried_object();
$cutomPageImageOption = get_option('taxonomy_' . $term->term_id);
$cutomPageImage = $cutomPageImageOption['cat_head_link'];
if ($cutomPageImage > 1) { echo "Please add a category head image in the admin panel"; }
?>
<section id="page-header" class="page-header" style="background-image: url('<?php echo $cutomPageImage; ?>">
<div class="container">
<div class="row">
<?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>
<h1><?php woocommerce_page_title(); ?></h1>
<?php endif; ?>
</div>
</div>
</section>