Show 4 first product attributes - woocommerce

I want to display the first 4 product attributes on each product card.
That code shows me only specific attributes. Any way to get these attributes? Every category has own attributes so I cant do it for every category on the site. Please help me
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
$has_row = false;
$attributes = $product->get_attributes();
ob_start();
?>
<div class="product_attributes">
<?php foreach ( $attributes as $attribute ) :
if ( empty( $attribute['is_visible'] ) || ( $attribute['is_taxonomy'] && ! taxonomy_exists( $attribute['name'] ) ) )
continue;
$values = wc_get_product_terms( $product->get_id(), $attribute['name'], array( 'fields' => 'names' ) );
$att_val = apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
if( empty( $att_val ) )
continue;
$has_row = true;
?>
<div class="col">
<div class="att_label"><?php echo wc_attribute_label( $attribute['name'] ); ?></div>
<div class="att_value"><?php echo $att_val; ?></div><!-- .att_value -->
</div><!-- .col -->
<?php endforeach; ?>
</div><!-- .product_attributes -->
<?php
if ( $has_row ) {
echo ob_get_clean();
} else {
ob_end_clean();
}

So I wrote other function:
if( $_product->has_attributes() ){
// Initializing
$attributes = array();
// Loop through product attributes
$i = 0;
foreach( $_product->get_attributes() as $taxonomy => $attribute ){
// The product attribute label name
$attribute_name = get_taxonomy( $taxonomy )->labels->singular_name;
// Set each product attribute with its values in an array
// $attributes[] = '<strong>'.$attribute_name.'</strong>: '.$_product->get_attribute($taxonomy);
?>
<div class="prodAttr">
<p class="prodAttr__title"><?php echo $attribute_name?></p>
<p class="prodAttr__value"><?php echo $_product->get_attribute($taxonomy) ?></p>
</div>
<?php
$i++;
if ($i == 4) break;
}
// Display (output)
// echo '<div class="product-attributes"><span>'. implode( '</span> <span>', $attributes ) . '</span></div>';
}
It works. Closed

Related

Wordpress pagination give duplicate content

So i change my wordpress permalink with custom structure "/%category%/%postname%/".
However, if i type manually "/poem/page/2", then it works. But the content of every page is the same. Wether it was "/page/3" or "/page/4", it was all the same.
at first, when i access "/page/2/", my page was giving "404 error". But i got this code from the internet, and when i use the code, it started to work. But, the content of every page is the same. The code goes on like this :
on function.php:
function vipx_filter_category_rewrite_rules( $rules ) {
$categories = get_categories( array( 'hide_empty' => false ) );
if ( is_array( $categories ) && ! empty( $categories ) ) {
$slugs = array();
foreach ( $categories as $category ) {
if ( is_object( $category ) && ! is_wp_error( $category ) ) {
if ( 0 == $category->category_parent ) {
$slugs[] = $category->slug;
} else {
$slugs[] = trim( get_category_parents( $category->term_id, false, '/', true ), '/' );
}
}
}
if ( ! empty( $slugs ) ) {
$rules = array();
foreach ( $slugs as $slug ) {
$rules[ '(' . $slug . ')/feed/(feed|rdf|rss|rss2|atom)?/?$' ] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
$rules[ '(' . $slug . ')/(feed|rdf|rss|rss2|atom)/?$' ] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
$rules[ '(' . $slug . ')(/page/(\d)+/?)?$' ] = 'index.php?category_name=$matches[1]&paged=$matches[3]';
}
}
}
return $rules;
}```
this is my loop post :
?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
echo $paged;
$term = get_queried_object();
$data = array( 'posts_per_page' => 2, 'category_name' => $term->slug, 'page'=>$paged );
$get_category_post = new WP_Query( $data );
?>
<?php if ( $get_category_post->have_posts() ) : while ( $get_category_post->have_posts() ) : $get_category_post->the_post(); ?>
<div class="post__list">
<?php if($term->slug === 'analect' || $term->slug === 'collection') { ?>
<span>for other categories</span>
<?php } else {?>
<span> <?php the_content(); ?> </span>
<?php } ?>
</div>
<?php endwhile; ?>
<?php
next_posts_link();
previous_posts_link();
?>
<?php endif; ?>

How to output WooCommerce product attributes as <UL>?

I am using Woocommerce for Wordpress, and trying to change the way product attribute is displayed (product-attribute.php). As you can see, the values are outputted in a td and each item is separated by a comma, but I need them in an unordered list instead, with each item into a separate li.
Can you help me with this, please?
<?php foreach ( $attributes as $attribute ) :
if ( empty( $attribute['is_visible'] ) || ( $attribute['is_taxonomy'] && ! taxonomy_exists( $attribute['name'] ) ) )
continue;
?>
<tr>
<th scope="row"><?php echo wc_attribute_label( $attribute->get_name() ); ?></th>
<?php
if ( $attribute['is_taxonomy'] ) {
$values = woocommerce_get_product_terms( $product->id, $attribute['name'], 'names' );
foreach ( $values as $value ) :
echo '<td>';
echo $value;
echo '</td>';
endforeach;
} else {
$values = array_map( 'trim', explode( '|', $attribute['value'] ) );
foreach ( $values as $value ) :
echo '<td>';
echo $value;
echo '</td>';
endforeach;
}
?>
</tr>
<?php endforeach; ?>
Right now I have
<td>item1, item2, item3</td>
but ideally, what I need is
<li>item1</li>
<li>item2</li>
<li>item3</li>
Thank you so much for you help!
here you go
<ul>
<?php foreach ( $attributes as $attribute ) :
if ( empty( $attribute['is_visible'] ) || ( $attribute['is_taxonomy'] && ! taxonomy_exists( $attribute['name'] ) ) ) {
continue;
}
?>
<p><?php echo wc_attribute_label( $attribute->get_name() ); ?></p>
<?php
if ( $attribute['is_taxonomy'] ) {
$values = woocommerce_get_product_terms( $product->id, $attribute['name'], 'names' );
foreach ( $values as $value ) :
echo '<li>';
echo $value;
echo '</li>';
endforeach;
} else {
$values = array_map( 'trim', explode( '|', $attribute['value'] ) );
foreach ( $values as $value ) :
echo '<li>';
echo $value;
echo '</li>';
endforeach;
}
?>
<?php endforeach; ?>
</ul>
i just replaces the <td> in your code with <li> and added the <ul> instead of <tr> also i modified the <th> to <p>

Woocommerce variable products

I am using woocommerce in my website. I need radio buttons instead of dropdown for variations on product page. For this, I have replaced the variable.php file with the following code :--
<?php
/**
* Variable product add to cart
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.5.0
*
* Modified to use radio buttons instead of dropdowns
* #author 8manos
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! function_exists( 'print_attribute_radio' ) ) {
function print_attribute_radio( $checked_value, $value, $label, $name ) {
global $product;
// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
$checked = sanitize_title( $checked_value ) === $checked_value ? checked( $checked_value, sanitize_title( $value ), false ) : checked( $checked_value, $value, false );
$input_name = 'attribute_' . esc_attr( $name ) ;
$esc_value = esc_attr( $value );
$id = esc_attr( $name . '_v_' . $value . $product->get_id() ); //added product ID at the end of the name to target single products
$filtered_label = apply_filters( 'woocommerce_variation_option_name', $label );
printf( '<div><input type="radio" name="%1$s" value="%2$s" id="%3$s" %4$s><label for="%3$s">%5$s</label></div>', $input_name, $esc_value, $id, $checked, $filtered_label );
}
}
global $product;
$attribute_keys = array_keys( $attributes );
do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->get_id() ); ?>" data-product_variations="<?php echo htmlspecialchars( wp_json_encode( $available_variations ) ) ?>">
<?php do_action( 'woocommerce_before_variations_form' ); ?>
<?php if ( empty( $available_variations ) && false !== $available_variations ) : ?>
<p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
<?php else : ?>
<table class="variations" cellspacing="0">
<tbody>
<?php foreach ( $attributes as $name => $options ) : ?>
<tr>
<td class="label"><label for="<?php echo sanitize_title( $name ); ?>"><?php echo wc_attribute_label( $name ); ?></label></td>
<?php
$sanitized_name = sanitize_title( $name );
if ( isset( $_REQUEST[ 'attribute_' . $sanitized_name ] ) ) {
$checked_value = $_REQUEST[ 'attribute_' . $sanitized_name ];
} elseif ( isset( $selected_attributes[ $sanitized_name ] ) ) {
$checked_value = $selected_attributes[ $sanitized_name ];
} else {
$checked_value = '';
}
?>
<td class="value">
<?php
if ( ! empty( $options ) ) {
if ( taxonomy_exists( $name ) ) {
// Get terms if this is a taxonomy - ordered. We need the names too.
$terms = wc_get_product_terms( $product->get_id(), $name, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, $options ) ) {
continue;
}
print_attribute_radio( $checked_value, $term->slug, $term->name, $sanitized_name );
}
} else {
foreach ( $options as $option ) {
print_attribute_radio( $checked_value, $option, $option, $sanitized_name );
}
}
}
echo end( $attribute_keys ) === $name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations cstm-reset" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<div class="single_variation_wrap">
<?php
do_action( 'woocommerce_before_single_variation' );
do_action( 'woocommerce_single_variation' );
do_action( 'woocommerce_after_single_variation' );
?>
</div>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
<?php endif; ?>
<?php do_action( 'woocommerce_after_variations_form' ); ?>
</form>
<?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
It shows the radio buttons as I needed but the problem is that, the price is not displaying on variation products.You can check my website http://www.itsjal.com/store/product/led-lamp-par38/
Price should be displayed at the end when I select variations options. Can anybody help me to solve this problem ?
Thanks
You can try this plugin. https://wordpress.org/plugins/woocommerce-radio-buttons/
This plugin also has its own template files if you want to customize it.

Wordpress - Post Category on page

I have a question: In wordpress, how can I display posts in a category on a page ?. I have many categories, and in a page, I just only want posts in a category are displayed.
Thanks for your support !
Here is an alternative to a plugin. This is a dynamic page template. This template lets you choose which category's posts to display on the specific page. You can use this template over and over inside the same theme
Add this to your functions.php or any functions related file
<?php
/**-----------------------------------------------------------------------------
*
*Add a post metabox with options to the admin page screen.
*After selcting the page-pop.php template as a page template,
*this metabox will appear in the admin page screen.
*From here you can choose which category's posts to display
*and how the posts will be displayed on the page
*
* #package WordPress
* #subpackage Twenty_Fourteen
* #since Twenty Fourteen 1.0
*
*------------------------------------------------------------------------------*/
/**-----------------------------------------------------------------------------
*
*1. Only add meta boxes for the pop template
*
* #since Twenty Fourteen 1.0
*
*------------------------------------------------------------------------------*/
add_action('admin_init', 'pietergoosen_add_pop_meta_box');
function pietergoosen_add_pop_meta_box(){
$post_id = isset( $_GET['post'] ) ? $_GET['post'] : 0 ;
if($post_id) {
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
if ($template_file == 'page-pop.php') {
add_meta_box('pop_meta_box', __( 'Page of Posts with the same name', 'pietergoosen' ), 'pietergoosen_pop_meta_options', 'page', 'side', 'core');
} else {
$meta = get_post_meta($post_id, '_cat_id', true);
if( $meta ) {
pietergoosen_pop_update_post_meta($post_id, '_cat_id', '');
pietergoosen_pop_update_post_meta($post_id, '_page_title', '');
pietergoosen_pop_update_post_meta($post_id, '_posts_title', '');
pietergoosen_pop_update_post_meta($post_id, '_order_by', '');
pietergoosen_pop_update_post_meta($post_id, '_asc', '');
pietergoosen_pop_update_post_meta($post_id, '_post_count', '');
pietergoosen_pop_update_post_meta($post_id, '_days', '');
remove_meta_box( 'pop_meta_box', 'page', 'side' );
}
}
}
add_action('save_post', 'pietergoosen_pop_update_post_meta_box');
}
/**-----------------------------------------------------------------------------
*
*2. Built the list to display the options in the metabox
*
* #since Twenty Fourteen 1.0
*
*------------------------------------------------------------------------------*/
$order_list = array(
'none' => array( 'value' => 'none','label' => 'None' ),
'id' => array( 'value' => 'ID','label' => 'Post ID' ),
'author' => array( 'value' => 'author','label' => 'Author' ),
'title' => array( 'value' => 'title','label' => 'Post Title' ),
'date' => array( 'value' => 'date', 'label' => 'Post Date'),
'modified' => array( 'value' => 'modified','label' => 'Modified Date' ),
'parent' => array( 'value' => 'parent','label' => 'Parent Post' ),
'rand' => array( 'value' => 'rand','label' => 'Random' ),
'comment_count' => array( 'value' => 'comment_count','label' => 'Comment Count' ),
'menu_order' => array( 'value' => 'menu_order','label' => 'Menu Order' ),
);
$sort = array(
'DESC' => array( 'value' => 'DESC','label' => 'Descending' ),
'ASC' => array( 'value' => 'ASC','label' => 'Ascending' ),
);
function pietergoosen_pop_meta_options(){
$post_id = !empty($_GET['post']) ? $_GET['post'] : 0;
if( !$post_id ) return;
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
if ($template_file != 'page-pop.php') return;
global $order_list,$post_styles,$sort;
$categories = get_categories();
//Check if we have values
$post_meta=array();
$post_meta = get_post_meta( $post_id,false );
$cat_id = isset( $post_meta['_cat_id'] ) ? $post_meta['_cat_id'][0] : 1;
$page_title = isset( $post_meta['_page_title'] ) && $post_meta['_page_title'] ? $post_meta['_page_title'][0] : '';
$posts_title = isset( $post_meta['_posts_title'] ) && $post_meta['_posts_title'] ? $post_meta['_posts_title'][0] : '';
$order_by = isset( $post_meta['_order_by'] ) ? $post_meta['_order_by'][0] : 'date';
$asc = isset( $post_meta['_asc'] ) ? $post_meta['_asc'][0] : 'DESC';
$post_count = isset( $post_meta['_post_count'] ) ? $post_meta['_post_count'][0] : get_option('posts_per_page');
if(!$post_count || !is_numeric( $post_count )) $post_count = get_option('posts_per_page');
$days = isset( $post_meta['_days'] ) ? $post_meta['_days'][0] : '0';
if($days && !is_numeric( $days )) $days = '0';
?>
<!-- Sart the meta boxes -->
<div class="inside">
<p><label><strong><?php _e( 'Page Title', 'pietergoosen' ); ?></strong></label></p>
<input id="_posts_title" name="_posts_title" type="text" style="width: 98%;" value="<?php echo $posts_title; ?>"/>
<p><label><strong><?php _e( 'Post Title', 'pietergoosen' ); ?></strong></label></p>
<input id="_page_title" name="_page_title" type="text" style="width: 98%;" value="<?php echo $page_title; ?>"/>
<p><label><strong><?php _e( 'Category', 'pietergoosen' ); ?></strong></label></p>
<select id="_cat_id" name="_cat_id">
<?php
//Category List
foreach ($categories as $cat) :
$selected = ( $cat->cat_ID == $cat_id ) ? ' selected = "selected" ' : '';
$option = '<option '.$selected .'value="' . $cat->cat_ID;
$option = $option .'">';
$option = $option .$cat->cat_name;
$option = $option .'</option>';
echo $option;
endforeach;
?>
</select>
<p><label><strong><?php _e( 'Sort by', 'pietergoosen' )?></strong></label></p>
<select id="_order_by" name="_order_by">
<?php
foreach ($order_list as $output) :
$selected = ( $output['value'] == $order_by ) ? ' selected = "selected" ' : '';
$option = '<option '.$selected .'value="' . $output['value'];
$option = $option .'">';
$option = $option .$output['label'];
$option = $option .'</option>';
echo $option;
endforeach;
?>
</select>
<p><label><strong><?php _e( 'Order', 'pietergoosen' )?><strong></label></p>
<select id="_asc" name="_asc">
<?php
foreach ($sort as $output) :
$selected = ( $output['value'] == $asc ) ? ' selected = "selected" ' : '';
$option = '<option '.$selected .'value="' . $output['value'];
$option = $option .'">';
$option = $option .$output['label'];
$option = $option .'</option>';
echo $option;
endforeach;
?>
</select>
<p><strong><label><?php _e( 'Posts per Page', 'pageofposts' ); ?><strong></label></p>
<input id="_post_count" name="_post_count" type="text" value="<?php echo $post_count; ?>" size="3" />
<p><strong><label><?php _e( 'Posts in the last days', 'pageofposts' ); ?><strong></label></p>
<input id="_days" name="_days" type="text" value="<?php echo $days; ?>" size="3" />
</div>
<!-- End page of posts meta box -->
<?php
}
function pietergoosen_pop_update_post_meta_box( $post_id ){
if ( empty( $_POST ) ) {
return;
} else {
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
if ($template_file != 'page-pop.php') return;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return $post_id;
} else {
if ( $_POST['post_type'] == 'page' ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
$meta = isset( $_POST['_cat_id'] ) ? $_POST['_cat_id'] : 1;
pietergoosen_pop_update_post_meta($post_id, '_cat_id', $meta);
$meta = isset( $_POST['_page_title'] ) ? $_POST['_page_title'] : '';
pietergoosen_pop_update_post_meta($post_id, '_page_title', $meta);
$meta = isset( $_POST['_posts_title'] ) ? $_POST['_posts_title'] : '';
pietergoosen_pop_update_post_meta($post_id, '_posts_title', $meta);
$meta = isset( $_POST['_order_by'] ) ? $_POST['_order_by'] : 'date';
pietergoosen_pop_update_post_meta($post_id, '_order_by', $meta);
$meta = isset( $_POST['_asc'] ) ? $_POST['_asc'] : 'DESC';
pietergoosen_pop_update_post_meta($post_id, '_asc', $meta);
$meta = isset( $_POST['_post_count'] ) ? $_POST['_post_count'] : get_option('posts_per_page');
pietergoosen_pop_update_post_meta($post_id, '_post_count', $meta);
$meta = isset( $_POST['_days'] ) ? $_POST['_days'] : 0;
pietergoosen_pop_update_post_meta($post_id, '_days', $meta);
return;
}
}
}
function pietergoosen_pop_update_post_meta($post_id, $key, $data) {
$post_meta = get_post_meta($post_id, $key, true);
if( $data != '' && $post_meta != $data) {
update_post_meta($post_id, $key, $data);
} elseif ( $post_meta != '' && $data == '' ) {
delete_post_meta($post_id, $key);
}
}
?>
Secondly, the page template. You have to call this template page-pop.php
<?php
/**
* Template Name: Page of Posts
*/
get_header(); ?>
<?php
//See if we have any values
$post_meta=array();
$post_meta = get_post_meta( $post->ID,false );
$catid = isset( $post_meta['_cat_id'] ) ? $post_meta['_cat_id'][0] : 1;
$page_title = isset( $post_meta['_page_title'] ) ? $post_meta['_page_title'][0] : '';
$posts_title = isset( $post_meta['_posts_title'] ) ? $post_meta['_posts_title'][0] : '';
$orderby = isset( $post_meta['_order_by'] ) ? $post_meta['_order_by'][0] : 'date';
$asc = isset( $post_meta['_asc'] ) ? $post_meta['_asc'][0] : 'DESC';
$list_style = isset( $post_meta['_list_style'] ) ? $post_meta['_list_style'][0] : 'default';
$post_count = isset( $post_meta['_post_count'] ) ? $post_meta['_post_count'][0] : get_option('posts_per_page');
if(!$post_count || !is_numeric( $post_count )) $post_count = get_option('posts_per_page');
$days = isset( $post_meta['_days'] ) ? $post_meta['_days'][0] : 0;
if($days && !is_numeric( $days )) $days = 0;
$do_not_show_stickies = ($list_style == 'default') ? 0 : 1;
?>
<div id="main-content" class="main-content">
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<!-- Page Title -->
<?php if( $posts_title ) : ?>
<article id="posts-title">
<header class="entry-header">
<h2 class="entry-title"><?php echo $posts_title; ?></h2>
</header><!-- .entry-header -->
</article><!-- #posts-title -->
<?php endif; ?>
<?php the_post(); ?>
<?php global $post;
if( $post->post_content || $page_title ) : ?>
<div class="entry-content">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if( $page_title ) : ?>
<header class="entry-header">
<h1 class="entry-title"><?php echo $page_title; ?></h1>
</header><!-- .entry-header -->
<?php endif; ?>
<?php if( $post->post_content ) : ?>
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'pietergoosen' ) . '</span>', 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<footer class="entry-meta">
</footer><!-- .entry-meta -->
<?php endif; ?>
</article><!-- #post-<?php the_ID(); ?> -->
</div>
<?php endif; ?>
<?php
/* Do we have any category */
global $post;
// Save posts for later use
$tmp_post = $post;
$args = array(
'cat' => $catid,
'posts_per_page' => $post_count,
'paged' => $paged,
'orderby' => $orderby,
'order' => $asc,
'ignore_sticky_posts' => $do_not_show_stickies,
);
if( $days ) {
function pop_filter_where( $where = '') {
global $days;
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-' .$days .' days')) . "'";
return $where;
}
add_filter( 'posts_where', 'pop_filter_where' );
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query( $args );
remove_filter( 'posts_where', 'pop_filter_where' );
} else {
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query( $args );
}
// Output
if ( $wp_query->have_posts() ) :
// Start the Loop.
while ( $wp_query->have_posts() ) : $wp_query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
pietergoosen_pagination();
else :
get_template_part( 'content', 'none' );
endif; ?>
<?php
// Reset the post to the page post
$post = $tmp_post;
?>
<?php if ( comments_open() || get_comments_number() ) {
comments_template();
} ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar( 'content' ); ?>
</div><!-- #main-content -->
<?php
get_footer();
You can now simply create a new page and choose the "page of posts" template, and publish your page. Once that is done, the Page of Posts metabox will appear. From that you can choose the category to display on that page.
There's a plugin that will do that. http://wordpress.org/plugins/posts-per-cat/
He used the plugin then set it to a ridiculous number so that it would show all the categories on the page. Here's the link to the website I found the answer on:
https://wordpress.stackexchange.com/questions/45856/show-posts-of-category-in-a-page
I hope that helps.

Woocommerce change from commas to table cells

I want to override the way Woocommerce outputs attributes. By default, the attributes are displayed in two columns - column 1 is a label, column 2 is a comma separated list of attributes. I want to override that and have each attribute display in its own cell. Here's the original code:
<?php foreach ( $attributes as $attribute ) :
if ( empty( $attribute['is_visible'] ) || ( $attribute['is_taxonomy'] && ! taxonomy_exists( $attribute['name'] ) ) )
continue;
?>
<tr class="<?php if ( ( $alt = $alt * -1 ) == 1 ) echo 'alt'; ?>">
<th><?php echo $woocommerce->get_helper( 'attribute' )->attribute_label( $attribute['name'] ); ?></th>
<td><?php
if ( $attribute['is_taxonomy'] ) {
$values = woocommerce_get_product_terms( $product->id, $attribute['name'], 'names' );
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
} else {
// Convert pipes to commas and display values
$values = array_map( 'trim', explode( '|', $attribute['value'] ) );
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
}
?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
I keep thinking I need to setup another foreach statement within the td but I'm not sure on how to get it setup.
Basicly you have, with that code, this output:
<tr>
<th>atribute label</th>
<td>atribute name</td>
<tr>
So, you only need to output that something like: "atribute label - atribute name", for that you only need to remove the th tags and put the code for label inside the td ones.
Someting like this:
<tr class="<?php if ( ( $alt = $alt * -1 ) == 1 ) echo 'alt'; ?>">
<td>
<?php echo $woocommerce->get_helper( 'attribute' )->attribute_label( $attribute['name'] ); ?>
<?php
if ( $attribute['is_taxonomy'] ) {
$values = woocommerce_get_product_terms( $product->id, $attribute['name'], 'names' );
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
} else {
// Convert pipes to commas and display values
$values = array_map( 'trim', explode( '|', $attribute['value'] ) );
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
}
?></td>
<?php endforeach; ?>
</tr>
I figured it out...I needed to adjust the the foreach statement and how the cells appear in the row (and what appears in them), here's my finalized code which is working, however, I would love suggestions to improve it:
<?php foreach ( $attributes as $attribute ) :
if ( empty( $attribute['is_visible'] ) || ( $attribute['is_taxonomy'] && ! taxonomy_exists( $attribute['name'] ) ) )
continue;
?>
<tr>
<th scope="row"><?php echo $woocommerce->attribute_label( $attribute['name'] ); ?></th>
<?php
if ( $attribute['is_taxonomy'] ) {
$values = woocommerce_get_product_terms( $product->id, $attribute['name'], 'names' );
foreach ( $values as $value ) :
echo '<td>';
echo $value;
echo '</td>';
endforeach;
} else {
$values = array_map( 'trim', explode( '|', $attribute['value'] ) );
foreach ( $values as $value ) :
echo '<td>';
echo $value;
echo '</td>';
endforeach;
}
?>
</tr><?php endforeach; ?>

Resources