WooCommerce get variable product attribute label - wordpress

I'm trying to display the details from my custom product attributes on a table on my single product page. I can get the product variation SKU, but I can't seem to get the "label" (the best I can do is get the slug).
This is the code that I'm currently working with:
<!-- ******* START PRODUCT VARIABLE TABLE SECTION ******* -->
<?php
global $woocommerce, $product, $post;
if( $product->is_type( 'variable' ) ) :
$available_variations = $product->get_available_variations();
// START CHECK IF VARIATIONS ARE PRESENT
if ( $available_variations ) :
$attributes = $product->get_attributes();
?>
<div class="row">
<div class="small-12 column">
<table class="varations-table">
<thead>
<tr>
<th class="small-4">ORDER #</th>
<th class="small-5">DESCRIPTION</th>
<th class="small-3">UOM</th>
</tr>
</thead>
<tbody>
<?php foreach ($available_variations as $prod_variation) : ?>
<?php
// get some vars to work with
$post_id = $prod_variation['variation_id'];
$post_object = get_post($post_id);
?>
<tr>
<td><?php echo $prod_variation['sku']; ?></td>
<?php foreach ($prod_variation['attributes'] as $attr_name => $attr_value) : ?>
<td><?php echo $attr_value; ?></td>
<?php endforeach;?>
<td class="uom"><?php echo the_field( 'uom' ); ?></td>
</tr>
<?php endforeach;?>
</tbody>
</table>
</div>
</div>
<?php
// END CHECK IF VARIATIONS ARE PRESENT
endif;
endif;
?>
<!-- ******* END PRODUCT VARIABLE TABLE SECTION ******* -->
Does anyone know how I can pull the Variable label (not just the slug, replacing dashes with spaces using str_replace() )?

If you have the slug, you can get a WordPress taxonomy object with the get_taxonomy() function. So to get the label of a WooCommerce custom attribute you just have to do get_taxonomy($attribute_slug)->labels->name.

<?php if ( $product->is_type( 'variable' )) : ?>
<div class="size">
<span class="name-size">SIZE</span>
<span class="value-size">
<?php
$attr = get_post_meta($post->ID, '_product_attributes', true);
echo $attr['size']['value'];
?>
</span>
</div>
<?php endif; ?>

Related

Customize WooCommerce My Account>Downloads table to display downloadable file links in a single row

I'm attempting to override WooCommerce>Templates>downloads.php to display a custom table that shows the links for multiple downloadable file types in a single row.
My current code is as follows:
'''
<?php
$downloads = WC()->customer->get_downloadable_products();
$has_downloads = (bool) $downloads;
<?php do_action( 'woocommerce_before_account_downloads', $has_downloads ); ?>
<table>
<thead>
<tr>
<th>Title</th>
<th>Version</th>
</tr>
</thead>
<tbody>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<tr>
<td>
<a class="productLink" href="<?php echo get_permalink( $loop->post->ID ) ?>">
<?php the_title(); ?>
</a>
</td>
<td>
/*This is the bit that doesn't work*/
<?php
$files = $product->get_downloads();
foreach( $files as $key => $each_download ) {
echo 'File Name | ';
}
?>
</td>
</tr>
<?php endwhile; wp_reset_query(); // Remember to reset ?>
</tbody>
</table>
'''
I'm struggling with how to show the downloadable file links with the filenames visible and separated by a vertical line | .

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>

wp post metadata showing in backend but not frontend / same code

Situation:
using a php file (table_template.php) with include_once to show meta data. Using this template in the backend (inside a metabox) shows everything as it should - using the same template on the frontend (inside a post) the table metadata do not show (but other meta data from custom fields do).
var dump in the backend:
string(65) "[["test value 1","test value 2","test value 3"],["test value a","test value b","test value c"]]"
var dump on the frontend:
string(6) "[[""]]"
code from table_template.php:
<?php
/* TEMPLATE TO RENDER THE TABLE (AS PREVIEW) IN THE BACKEND AND FRONTEND */
global $post;
$table_meta = get_post_meta( $post->ID, 'psg_table_meta', true ) ?
get_post_meta( $post->ID, 'psg_table_meta', true ) : '[[""]]';
$t = json_decode( $table_meta );
$c_id = get_post_meta( $post->ID );
$c = get_post( $c_id );
?>
<div class="psg_box_table">
<table class="psg_table ">
<thead>
<tr>
<?php foreach ( $t[ 0 ] as $col ): ?>
<th>
<?php echo $col; ?>
</th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach ( $t as $idx => $row ): ?>
<?php if ( $idx == 0 )
continue; ?>
<tr>
<?php foreach ( $row as $col ): ?>
<td>
<div class="psg_table_content">
<?php echo str_replace( '"', '"', $col ) ?>
</div>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<!--END SIZING CHART TABLE -->
How i load the Template:
include_once( "table_template.php" );
If you are using the template inside loop of WP_Query, then replace
$post->ID
with
get_the_ID();
Also check the psg_table_meta key is correct one.
The issue was, that i should not check for the postID (the table meta data is not directly in the post, but in a selected/linked post within it. So i had to replace
$table_meta = get_post_meta( $post->ID, 'psg_table_meta', true ) ?
with
$table_meta = get_post_meta( $psg_selected, 'psg_table_meta', true ) ?

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.

How to display the_meta() of a Wordpress custom page in a table

I have some meta values on my wordpress custom page that I need to be displayed in a table. Is there any way I can do that.
Here's the code I'm using now: <?php the_meta(); ?>
And this is what it shows:
I want to do something like:
I found out that:
the_meta() is located in wp-includes/post-template.php
This makes the output:
<ul class='post-meta'>
<li><span class='post-meta-key'>your_key:</span> your_value</li>
</ul>
So it's not recommended to edit files in that folder because of the wordpress update.
<table>
<tr><td colspan="2">Game Summary</td></tr>
<?php
$meta = get_post_meta( get_the_ID() );
$exclude = array('_edit_last', '_wp_page_template', '_edit_lock');
foreach( $meta as $key => $value ) {
if( in_array( $key, $exclude) )
continue;
?>
<tr>
<td><?php echo $key; ?></td>
<td><?php echo $value[0]; ?></td>
</tr>
<?php
}
?>
</table>

Resources