Add custom cart field to woocommerce - woocommerce

This may be a noob question, but I've googled a lot and haven't found the answer. I've already replaced the woocommerce cart to my child theme subfolder.
I'm trying to add a field to the woocommerce cart, so it will just display the cart subtotal multiplied by a number defined by me. I.e., the cart subtotal is 3,000.00, I want to define the multiplier number 3, so the field I want to show must display 9.000,00.
I've already added the field header to the cart:
<thead>
<tr>
<th class="product-remove"> </th>
<th class="product-quantity"><?php _e( 'Quantity', 'thefoxwp' ); ?></th>
<th class="product-subtotal"><?php _e( 'Total', 'thefoxwp' ); ?></th>
<th class="product-result"><?php _e( 'Total to be Received', 'thefoxwp' ); ?></th>
</tr>
</thead>
The field I want to configure is the class="product-result".
It's not a input field, it will just show the subtotal multiplied by a number defined by me.
I need this information to be sent with the woocommerce email to the client and saved in my order details.
Does anybody know how to do it?
Thanks

Could figure it out.
I added two classes to the header
<thead>
<tr>
<th class="product-remove"> </th>
<th class="product-quantity"><?php _e( 'Quantity', 'thefoxwp' ); ?></th>
<th class="product-subtotal"><?php _e( 'Total', 'thefoxwp' ); ?></th>
<th class="product-tax"><?php _e( 'Exchange Tax', 'thefoxwp' ); ?></th>
<th class="product-result"><?php _e( 'Total to be Received', 'thefoxwp' ); ?></th>
</tr>
</thead>
And then added this code
<!-- Exchange Tax -->
<td class="product-tax">
<?php
$tax = 4.05;
echo "R$ $tax";
?>
</td>
<!-- Product Result -->
<td class="product-result">
<?php
$resultado = WC()->cart->subtotal;
$mostrar = $resultado * $tax;
echo "R$ "; echo number_format("$mostrar",2);
?>
</td>
So it's now echo the result of the cart subtotal * the exchange tax.
But now, how can I get this variable to echo on the email sent by woocommerce and on the checkout page?

Related

Wordpress add user number field in contact information part

I am wondering how to achieve the following.
I try to create a number field input in the wordpress user profile under contact information.
So far if i use show_user_profile & edit_user_profile hooks, the field will appear on the bottom of the profile page.
Is it possible to create a number field under the contact information part (standard in wordpress?)
I tried to add different priorities to the actions, but that didnt solve it.
So far i have the following:
function addurenuser(){
$userid = get_current_user_id();
if ( current_user_can('update_plugins',$userid)){ ?>
<table class="form-table">
<tr>
<th>
<label>Uren</label>
</th>
<td>
<input type="number" name="uren" id="uren" value="<?php echo esc_attr( get_the_author_meta( 'uren', $user->ID ) ); ?>">
</td>
</tr>
</table> <?php
}
}
add_action( 'show_user_profile', 'addurenuser' );
add_action( 'edit_user_profile', 'addurenuser' );
Use this code
function addurenuser()
{
echo '<script>
jQuery(document).ready(function($){
$(".your-custom-field").insertAfter($(".user-email-wrap").closest("table"));
});
</script>';
$userid = get_current_user_id();
if (current_user_can('update_plugins', $userid)) { ?>
<table class="form-table your-custom-field">
<tr>
<th>
<label>Uren</label>
</th>
<td>
<input type="number" name="uren" id="uren" value="<?php echo esc_attr(get_the_author_meta('uren', $userid->ID)); ?>">
</td>
</tr>
</table> <?php
}
}
add_action('show_user_profile', 'addurenuser');
add_action('edit_user_profile', 'addurenuser');

Editing the invoice template in Woocommerce

I need to edit the Woocommerce invoice template with coding. I Copied all the files from wp-content/plugins/woocommerce-pdf-invoices-packing-slips/templates/Simple to to my child them in wp-content/themes/church-event-child/woocommerce/pdf/yourtemplate and customized them there. I need the template like this:
Subtotal: € 0
IVA: € 0
Order total: € 0
But woocommerce simple template only shows it like this
Subtotal: € 0
Order total: € 0
So it is about adding the IVA with the amount inside. Here you can see the code I am trying to change, but I dont know how to find where the label of the tax_amount is kept and how to display it right.
<table class="totals">
<tfoot>
<?php foreach( $this->get_woocommerce_totals() as $key => $total ) : ?>
<tr class="<?php echo $key; ?>">
<td class="no-borders"></td>
<th class="description"><?php echo $total['label']; ?></th>
<td class="price"><span class="totals-price"><?php echo $total['value']; ?></span></td>
</tr>
<?php endforeach; ?>
</tfoot>
</table>
Thanks in Advance

get_the_terms not working in custom plugin

I have created custom plugin which stores the user data in db table. The form have dropdown which have taxonomy id as value.
The form is working and taxonomy id and other user data is inserting in the database.
In the wp admin panel i am showing the data that has been inserted in the database. All the data is coming with the taxonomy id. But when i use get_the_terms to get the name according to the taxonomy id it shows nothing.
Here is the code:
global $wpdb;
$table_name=$wpdb->prefix .'opu_userdata';
$data=$wpdb->get_results("SELECT * FROM $table_name order by id DESC");
<table class="table table-bordered" id="tow-table">
<thead>
<tr>
<th>Sno</th>
<th>Name</th>
<th>Email</th>
<th>Mobile</th>
<th>Product</th>
<th>Product Sub</th>
<th>Annual Salary</th>
<th>Employee Type</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<?php
$sn=1;
foreach($data as $data):?>
<tr>
<td><?php echo $sn;?></td>
<td><?php echo ucwords($data->name);?></td>
<td><?php echo $data->email;?></td>
<td><?php echo $data->mobile;?></td>
<td><?php echo $data->product;?></td>
<td><?php
if($data->product=="bank-account"){
$id=$data->product_sub;
$n=get_the_terms($id,'account_type');
echo $id; /////if i echo id it prints the id perfectly but when i try $n[0]->name it shows nothing (var_dump($n) shows bool(false) )
}?></td>
<td><?php echo $data->annual_salary;?></td>
<td><?php echo $data->emp_type;?></td>
<td><?php echo $data->age;?></td>
</tr>
<?php $sn++; endforeach;?>
</tbody>
Ok i got the solution. I have to use get_term_by instead of get_the_terms to get the term name according to the taxonomy id
Here is the solution:
<table class="table table-bordered" id="tow-table">
<thead>
<tr>
<th>Sno</th>
<th>Name</th>
<th>Email</th>
<th>Mobile</th>
<th>Product</th>
<th>Product Sub</th>
<th>Annual Salary</th>
<th>Employee Type</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<?php
$sn=1;
foreach($data as $d):?>
<tr>
<td><?php echo $sn;?></td>
<td><?php echo ucwords($d->name);?></td>
<td><?php echo $d->email;?></td>
<td><?php echo $d->mobile;?></td>
<td><?php echo $d->product;?></td>
<td><?php
if($d->product=="bank-account"){
$id=$d->product_sub;
$term=get_term_by('id',$id,'account_type');
echo $term->name;
}?></td>
<td><?php echo $d->annual_salary;?></td>
<td><?php echo $d->emp_type;?></td>
<td><?php echo $d->age;?></td>
</tr>
<?php $sn++; endforeach;?>
</tbody>

Woocommerce - Add to custom word in checkout and cart page

i want to add custom text in checkout and cart page.
what code do I need to write to the function.php file?
beside to the "total" word. Always static.
Thank you
https://i.hizliresim.com/nJ4dBR.jpg
image1
Overriding the woocommerce checkout/review-order.php template.
You need first (if not done) to copy the templates sub folder located in in woocommerce plugin folder to your active child theme (or theme) folde, and rename it woocommerce.
Once done in your active theme go to woocommerce > checkout, and open/edit review-order.php template file.
At the end of this template you have this:
<?php do_action( 'woocommerce_review_order_before_order_total' ); ?>
<tr class="order-total">
<th><?php _e( 'Total', 'woocommerce' ); ?></th>
<td><?php wc_cart_totals_order_total_html(); ?></td>
</tr>
<?php do_action( 'woocommerce_review_order_after_order_total' ); ?>
</tfoot>
So you will change:
To:
Now you can save, you are done…
Hope this will helps you.
For more information,
Customize the text “Total” in WooCommerce checkout page
You will need to copy cart/cart-totals.php and checkout/review-order.php template files from woocommerce/templates to your theme's folder at <your-theme's-folder>/woocommerce/.
Then modify following templates.
Template Path: <your-theme's-folder>/woocommerce/cart/cart-totals.php
<tr class="order-total">
<th><?php _e( 'Total', 'woocommerce' ); ?></th>
<td data-title="<?php esc_attr_e( 'Total', 'woocommerce' ); ?>"><?php wc_cart_totals_order_total_html(); ?></td>
</tr>
Template Path: <your-theme's-folder>/woocommerce/checkout/review-order.php
<tr class="order-total">
<th><?php _e( 'Total', 'woocommerce' ); ?></th>
<td><?php wc_cart_totals_order_total_html(); ?></td>
</tr>
Modification Example
Edit <th><?php _e( 'Total', 'woocommerce' ); ?></th> with, for example,
<th><?php _e( 'Total', 'woocommerce' ); ?> <span> <?php _e('some text here', 'mm-woocommerce'); ?> </span></th>
We added <span> <?php _e('some text here', 'mm-woocommerce'); ?> </span>

Plugin not displaying function on page where shortcode is present

This is a plugin I'm currently developing, it's function is to display a CSV file stored in the plugin directory, as an HTML table; The reason I'm writing this manually rather than using one of the many already existing plugins which perform a similar task is; I need to be able to use the html output for another script's functions that will be executed later, rather than just displaying this data.
The shortcode is functioning because it isn't visible on the page, however nothing else appears on the page.
As far as I know the csv file I'm using to test this plugin isn't corrupted, can be opened.
This is the content of Book1.csv:
test1,test2,test3
a,b,c
1,2,3
4,5,6
7,8,9
10,11,12
13,14,15
<?php
<table>
<thead>
<th>
Image
</th>
<th>
Name
</th>
<th>
Price
</th>
</thead>
<tbody>
<?php
add_shortcode( "csv", "open_csv_file");
function open_csv_file() {
$handle = fopen("Book1.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE):
}
?>
<?php
<tr>
<td><?= $data[1] ?></td>
<td><?= $data[2] ?></td>
<td>$<?= number_format($data[7], 2) ?></td>
</tr>
endwhile;
}
</tbody>
</table>
?>
I found some errors in your code, first you are not getting any output because
} for function is closed in wrong place
short tags <?= may not work if it is disabled in configuration
The following may work,
<?php
add_shortcode( "csv", "open_csv_file");
function open_csv_file() {
?>
<table>
<thead>
<th>
Image
</th>
<th>
Name
</th>
<th>
Price
</th>
</thead>
<tbody>
<?php
$handle = fopen("Book1.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE):
?>
<tr>
<td><?php echo $data[1]; ?></td>
<td><?php echo $data[2]; ?></td>
<td>$<?php echo number_format($data[7], 2); ?></td>
</tr>
<?php
endwhile;
?>
</tbody>
</table>
<?php } ?>

Resources