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>
Related
This question already has an answer here:
Show product categories in a new column on WooCommerce "My account" orders table
(1 answer)
Closed 1 year ago.
There are orders that have already been completed or not, and they contain goods, clothing, courses, etc. In WordPress, I have all the products subdivided into categories, but I don't know how to display the names of these categories. Suppose I have an order from a T-shirt and some course, and in the category I have to display "Clothes, Courses" (In the picture below, it is marked in red where it is necessary to display). I was able to display only the "Categories" tab, but the categories themselves cannot be displayed at all :( Here is the code from the orders.php file
<table class="woocommerce-orders-table woocommerce-MyAccount-orders shop_table shop_table_responsive my_account_orders account-orders-table">
<thead>
<tr>
<?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
<th class="woocommerce-orders-table__header woocommerce-orders-table__header-<?php echo esc_attr( $column_id ); ?>"><span class="nobr"><?php echo esc_html( $column_name ); ?></span></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php
foreach ( $customer_orders->orders as $customer_order ) {
$order = wc_get_order( $customer_order ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$item_count = $order->get_item_count() - $order->get_item_count_refunded();
?>
<tr class="woocommerce-orders-table__row woocommerce-orders-table__row--status-<?php echo esc_attr( $order->get_status() ); ?> order">
<?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-<?php echo esc_attr( $column_id ); ?>" data-title="<?php echo esc_attr( $column_name ); ?>">
<?php if ( has_action( 'woocommerce_my_account_my_orders_column_' . $column_id ) ) : ?>
<?php do_action( 'woocommerce_my_account_my_orders_column_' . $column_id, $order ); ?>
<?php elseif ( 'order-number' === $column_id ) : ?>
<a href="<?php echo esc_url( $order->get_view_order_url() ); ?>">
<?php echo esc_html( _x( '#', 'hash before order number', 'woocommerce' ) . $order->get_order_number() ); ?>
</a>
<?php elseif ( 'order-cat' === $column_id ) : ?>
<?php elseif ( 'order-date' === $column_id ) : ?>
<time datetime="<?php echo esc_attr( $order->get_date_created()->date( 'c' ) ); ?>"><?php echo esc_html( wc_format_datetime( $order->get_date_created() ) ); ?></time>
<?php elseif ( 'order-status' === $column_id ) : ?>
<?php echo esc_html( wc_get_order_status_name( $order->get_status() ) ); ?>
<?php elseif ( 'order-total' === $column_id ) : ?>
<?php
/* translators: 1: formatted order total 2: total order items */
echo wp_kses_post( sprintf( _n( '%1$s for %2$s item', '%1$s for %2$s items', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count ) );
?>
<?php elseif ( 'order-actions' === $column_id ) : ?>
<?php
$actions = wc_get_account_orders_actions( $order );
if ( ! empty( $actions ) ) {
foreach ( $actions as $key => $action ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
echo '' . esc_html( $action['name'] ) . '';
}
}
?>
<?php endif; ?>
</td>
<?php endforeach; ?>
</tr>
<?php
}
?>
</tbody>
</table>
And here is the picture:
enter image description here
$items = $order->get_items();
$cats = '';
foreach($items as $key => $item) {
$product_id = $item['product_id'];
$term_obj_list = get_the_terms($product_id, 'product_cat');
$terms_string = implode(', ', wp_list_pluck($term_obj_list, 'name'));
$cats .= $terms_string;
}
echo $cats;
Put this in correct elseif block of your given code.
Updated according to the comment with code comment:
//Getting all the items in the order
$items = $order->get_items();
$c_list = [];
/**
* Loop through all items and get their categories [a product can have multiple category]
*/
foreach($items as $key => $item) {
//We need the product id to retrieved the categories
$product_id = $item['product_id'];
//Retrieving all the categories that product has
$term_obj_list = get_the_terms($product_id, 'product_cat');
//Loop through all the categories the current product has and saving it in a array with category id as index
//so that if the save category is assigned to another product we do not get the duplicate category
foreach($term_obj_list as $term_obj) {
$c_list[$term_obj->term_id] = $term_obj->name;
}
}
//lastly we concatenating all the categories with ", "
$cats = implode(', ', $c_list);
//printing the final result
echo $cats;
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
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.
Basicly, I would like to display the coupon used on Woocommerce thankyou.php. Here is the code I have added
$coupons = $order->get_items( 'coupon' );
foreach ( $coupons as $item_id => $item ) {
echo "<span class='coupon-name'><b>".$item['name']."</b></span>";
$post = get_post( $item_id );
echo "<p class='coupon-description'>".$post->post_excerpt."</p>";
}
However, only the coupon code is shown while the description does not.
Here is where I placed my code:
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( $order ) : ?>
<?php if ( $order->has_status( 'failed' ) ) : ?>
<p class="woocommerce-thankyou-order-failed"><?php _e( 'Unfortunately your order cannot be processed as the originating bank/merchant has declined your transaction. Please attempt your purchase again.', 'woocommerce' ); ?></p>
<p class="woocommerce-thankyou-order-failed-actions">
<?php _e( 'Pay', 'woocommerce' ) ?>
<?php if ( is_user_logged_in() ) : ?>
<?php _e( 'My Account', 'woocommerce' ); ?>
<?php endif; ?>
</p>
<?php else : ?>
<?php
$coupons = $order->get_items( 'coupon' );
foreach ( $coupons as $item_id => $item ) {
echo "<span class='coupon-name'><b>".$item['name']."</b></span>";
$post = get_post( $item_id );
echo "<p class='coupon-description'>".$post->post_excerpt."</p>";
}
?>
<p class="woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you. Your order has been received.', 'woocommerce' ), $order ); ?></p>
<ul class="woocommerce-thankyou-order-details order_details">
<li class="order">
<?php _e( 'Order Number:', 'woocommerce' ); ?>
<strong><?php echo $order->get_order_number(); ?></strong>
</li>
<li class="date">
<?php _e( 'Date:', 'woocommerce' ); ?>
<strong><?php echo date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) ); ?></strong>
</li>
<li class="total">
<?php _e( 'Total:', 'woocommerce' ); ?>
<strong><?php echo $order->get_formatted_order_total(); ?></strong>
</li>
<?php if ( $order->payment_method_title ) : ?>
<li class="method">
<?php _e( 'Payment Method:', 'woocommerce' ); ?>
<strong><?php echo $order->payment_method_title; ?></strong>
</li>
<?php endif; ?>
</ul>
<div class="clear"></div>
<?php endif; ?>
<?php do_action( 'woocommerce_thankyou_' . $order->payment_method, $order->id ); ?>
<?php do_action( 'woocommerce_thankyou', $order->id ); ?>
<p class="woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you. Your order has been received.', 'woocommerce' ), null ); ?></p>
Could anyone please help me with this?
add_filter('woocommerce_get_order_item_totals','change_discount',10,3);
function change_discount($total_rows, $order, $tax_display){
if ( $order->get_total_discount() > 0 ) {
$coupons = $order->get_used_coupons();
$total_rows['discount'] = array( 'label' => __( 'Coupon code: '.implode(",",$coupons), 'woocommerce' ), 'value' => '-' . $order->get_discount_to_display( $tax_display ), );
}
return $total_rows;
}
you can get like this
if( $order->get_used_coupons() ) {
$coupons_count = count( $order->get_used_coupons() );
echo '<h4>' . __('Coupons used') . ' (' . $coupons_count . ')</h4>';
echo '<p><strong>' . __('Coupons used') . ':</strong> ';
$i = 1;
$coupons_list = '';
foreach( $order->get_used_coupons() as $coupon) {
$coupons_list .= $coupon;
if( $i < $coupons_count )
$coupons_list .= ', ';
$i++;
}
echo '<p><strong>Coupons used (' . $coupons_count . ') :</strong> ' . $coupons_list . '</p>'; }
Please use this code, I hope it will work for you:
<?php
add_action('woocommerce_thankyou', 'apply_product_on_coupon');
function apply_product_on_coupon() {
global $woocommerce;
if ( ! empty( $woocommerce->cart->applied_coupons ) ) {
$my_coupon = $woocommerce->cart->get_coupons() ;
foreach($my_coupon as $coupon){
if ( $post = get_post( $coupon->id ) ) {
if ( !empty( $post->post_excerpt ) ) {
echo "<span class='coupon-name'><b>".$coupon->code."</b></span>";
echo "<p class='coupon-description'>".$post->post_excerpt."</p>";
}
}
}
}
}
?>
If this will not work then use coupon shortcode plugin and call shortcode on the thankyou page
https://wordpress.org/plugins/woocommerce-coupon-shortcodes/
<?php echo do_shortcode('[coupon_shortcode]'); ?>
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; ?>