get_the_terms not working in custom plugin - wordpress

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>

Related

How do I show custom WooCommerce Subscriptions data on the frontend for customers subscription details?

I am using the WooCommerce Subscriptions plugin to manage recurring orders.
But I want my customers to see custom data, that I add to all new subscriptions, on their subscriptions details page.
I add a variable for the customers baby name, which I add as _baby_name in the subscriptions data post_meta data like this:
/**
* Triggered after a new subscription is created.
*
* #since 2.2.22
* #param WC_Subscription $subscription
*/
function action_wcs_create_subscription( $subscription ) {
// Get ID
$subscription_id = $subscription->get_id();
update_post_meta( $subscription_id, '_baby_name', get_current_user_id() );
}
add_action( 'wcs_create_subscription', 'action_wcs_create_subscription', 10, 1 );
For testing purposes, I just set the value to be get_current_user_id().
To present this custom data on the customers frontend, I have tried to modify the subscription-details.php file:
wp-content/plugins/woocommerce-subscriptions/vendor/woocommerce/subscriptions-core/templates/myaccount/subscription-details.php
I added a row in the top of my subscription_details table, above the status row like this:
<tbody>
<tr>
<td><?php esc_html_e( 'Baby Name', 'woocommerce-subscriptions' ); ?></td>
<td><?php echo esc_html( $subscription.'baby_name' ); ?></td>
</tr>
<tr>
<td><?php esc_html_e( 'Status', 'woocommerce-subscriptions' ); ?></td>
<td><?php echo esc_html( wcs_get_subscription_status_name( $subscription->get_status() ) ); ?></td>
</tr>
But in my new row for Baby Name, I just get all data associated with $subscriptions:
What should I use instead of $subscription.'_baby_name' to pull the value of _baby_name, and show it in the table?
Since the data is saved as post meta, you can use $subscription->get_meta( '_baby_name', true );
So you get:
<tbody>
<tr>
<td><?php esc_html_e( 'Baby Name', 'woocommerce-subscriptions' ); ?></td>
<td><?php echo $subscription->get_meta( '_baby_name', true ); ?></td>
</tr>
<tr>
<td><?php esc_html_e( 'Status', 'woocommerce-subscriptions' ); ?></td>
<td><?php echo esc_html( wcs_get_subscription_status_name( $subscription->get_status() ) ); ?></td>
</tr>
OR use get_post_meta( $subscription->get_id(), '_baby_name', true );
<tbody>
<tr>
<td><?php esc_html_e( 'Baby Name', 'woocommerce-subscriptions' ); ?></td>
<td><?php echo get_post_meta( $subscription->get_id(), '_baby_name', true ); ?></td>
</tr>
<tr>
<td><?php esc_html_e( 'Status', 'woocommerce-subscriptions' ); ?></td>
<td><?php echo esc_html( wcs_get_subscription_status_name( $subscription->get_status() ) ); ?></td>
</tr>
However, if you don't want to edit template files, you can use the wcs_subscription_details_table_before_dates action hook:
function action_wcs_subscription_details_table_before_dates( $subscription ) {
echo '<tr><td>';
echo esc_html_e( 'Baby Name', 'woocommerce-subscriptions' );
echo '</td><td>';
echo get_post_meta( $subscription->get_id(), '_baby_name', true );
echo '</td></tr>';
}
add_action( 'wcs_subscription_details_table_before_dates', 'action_wcs_subscription_details_table_before_dates', 10, 1 );
The only downside to this is that the new row will be displayed below the 'status' row, opposite above

Change WooCommerce attribute table to two columns

I'm very new to Wordpress/WooCommerce and I'm trying to do something I thought would have been simple.
The very basic WooCommerce Single Product page has the Product Attributes, they all appear under one big column.
Where can I actually modify the style and css for Product Page, so that I can have them in 2 columns?
I see the table is itself is generated under class "woocommerce-product-attributes" and "shop_attributes" ?
Any guidance is very appreciated.
Had the same issue, solved it by editing woocommerce/templates/single-product/product-attributes.php. To make the change permanent between woocommerce updates, copy the file contents to yourtheme/woocommerce/single-product/product-attributes.php.
Change:
<table class="woocommerce-product-attributes shop_attributes">
<?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
<th class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $product_attribute['label'] ); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $product_attribute['value'] ); ?></td>
</tr>
<?php endforeach; ?>
</table>
to:
<table>
<?php foreach (array_chunk($product_attributes, 2) as $product_attribute_key => $product_attribute) :{ ?>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
<?php foreach ($product_attribute as $value) :{ ?>
<th class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $value['label'] ); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $value['value'] ); ?></td>
<?php } endforeach; ?>
</tr>
<?php } endforeach; ?>
</table>

Add custom cart field to 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?

Custom Wordpress Query Posts based on meta values

I have a simple wordpress query that I need to write as part of a theme I am creating.
I have most of it but need a little push in the right direction to filter the returned results pragmatically based upon a custom meta field of the custom post type.
If the post meta field 'completion date' or 'published date' is equal to or before the current date, put posts into Published Research table.
If the posts meta field 'completion date' or 'published date' is a date ahead of the current date, put the posts into Ongoing Research.
* UPDATED *
Currently I have this:
<div class="container main-content">
<div class="row">
<h1>ONGOING RESEARCH</h1>
<table class="table table-bordered">
<tr>
<th>Type</th>
<th>Title</th>
<th>Summary / Abstract</th>
<th>Keywords</th>
<th>Type of Research</th>
<th>Principle Investigator</th>
<th>Completion Date</th>
</tr>
<?php $wp_query_ongoing = new WP_Query( array( 'post_type' => 'research', 'posts_per_page' => 5, 'order' => 'asc' ) ); ?>
<?php while ( $wp_query_ongoing->have_posts() ) : $wp_query_ongoing->the_post(); //start of the loop ?>
<?php
$post_id = get_the_ID();
$completion_date = get_post_meta( $post_id, "duration_end", true );
$publication_date = get_post_meta( $post_id, "publication_date", true );
?>
<?php if (strtotime($publication_date) >= strtotime("now") || strtotime($completion_date) >= strtotime("now")) { ?>
<tr>
<td><?php echo do_shortcode('[wpuf-meta name="_hidden_type"]' ); ?></td>
<td><?php the_title(); ?></td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>
<?php echo do_shortcode('[wpuf-meta name="duration_end"]'); ?>
<?php echo do_shortcode('[wpuf-meta name="publication_date"]'); ?>
</td>
</tr>
<?php } ?>
<?php endwhile; //end of the loop ?>
</table>
<h1>PUBLISHED RESEARCH</h1>
<table class="table table-bordered">
<tr>
<th>Type</th>
<th>Title</th>
<th>Summary / Abstract</th>
<th>Keywords</th>
<th>Author</th>
<th>Type of Research</th>
<th>Open Access</th>
<th>Date Published</th>
</tr>
<?php $wp_query_published = new WP_Query( array( 'post_type' => 'research', 'posts_per_page' => 5, 'order' => 'asc' ) ); ?>
<?php while ( $wp_query_published->have_posts() ) : $wp_query_published->the_post(); //start of the loop ?>
<?php
$post_id = get_the_ID();
$completion_date = get_post_meta( $post_id, "duration_end", true );
$publication_date = get_post_meta( $post_id, "publication_date", true );
?>
<?php if ( strtotime($publication_date) <= strtotime("now") || strtotime($completion_date) <= strtotime("now")){ ?>
<tr>
<td><?php echo do_shortcode('[wpuf-meta name="_hidden_type"]' ); ?></td>
<td><?php the_title(); ?></td>
<td>
<?php echo do_shortcode('[wpuf-meta name="synopsis"]'); ?>
<?php echo do_shortcode('[wpuf-meta name="abstract"]'); ?>
</td>
<td>
<?php echo do_shortcode('[wpuf-meta name="keywords"]'); ?>
<?php
$terms = get_the_terms($post->ID, 'keywords');
foreach ($terms as $keyword) {
$myKeywords[] = $keyword->name;
}
echo implode( ', ', $myKeywords );
$myKeywords = null;
?>
</td>
<td><?php echo do_shortcode('[wpuf-meta name="author"]' ); ?></td>
<td><?php echo do_shortcode('[wpuf-meta name="type_of_research"]'); ?></td>
<td><?php echo do_shortcode('[wpuf-meta name="open_access"]'); ?></td>
<td>
<?php echo do_shortcode('[wpuf-meta name="duration_end"]'); ?>
<?php echo do_shortcode('[wpuf-meta name="publication_date"]'); ?>
</td>
</tr>
<?php } ?>
<?php endwhile; //end of the loop ?>
</table>
* New related question *
Currently the ongoing research table works correctly, only showing posts that have a meta date set somewhere in the future. However, the published table is the correct posts BUT also the posts that should only be in ongoing. ????
I have never worked with dates in php. How can I modify the above code to return posts according to the the date provided in a meta field.
N/B we are not concerned with the posts actual published date, only the date set in a custom meta field.
https://www.dropbox.com/s/0hddjypw9adj9qj/29_alcoholresearchdirectory_browse-research_1-1.png?dl=0
Thanks for any pointers, I have not worked with php and dates.

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