hi everyone i want to try add tag on the attributes item woocommerce to show any item per line .
can i help me to do that?
here is my product-attributes.php file code
<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; ?>
you can try below code for example:
<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 str_replace(',','<br>',wp_kses_post( $product_attribute['value'] )); ?></td>
</tr>
<?php endforeach; ?>
I didn't check on a real project. let me know if it works or not.
Please try following code and check if it's working.
<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'] ).'<br>'; ?></td>
</tr>
Related
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
I can't echo the product name on my order page. I'm using ultimate member plugin with WooCommerce. "get_id();" is working just fine. I used "$order->get_name()" to echo the ordered product name. but it shows me error when tried to load the page.
<tbody>
<?php
foreach( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order->ID );
$order_id = $customer_order->ID;
$order_data = $order->get_data();
$order_date = strtotime( $order->get_date_created() );
?>
<tr class="order" data-order_id="<?php echo esc_attr( $order_id ); ?>">
<?php do_action( 'um_woocommerce_orders_tab_before_table_row', $order, $customer_orders ); ?>
<td class="order-total" data-title="<?php _e( 'Item', 'um-woocommerce' ); ?>">
<?php echo $order->get_name() ?></td>
<td class="order-date" data-title="<?php _e( 'Date', 'um-woocommerce' ); ?>">
<time
datetime="<?php echo wp_date( 'Y-m-d', $order_date ); ?>"><?php echo wp_date( $date_time_format, $order_date ); ?></time>
</td>
<td class="order-status" data-title="<?php _e( 'Status', 'um-woocommerce' ); ?>">
<span
class="um-woo-status <?php echo $order->get_status(); ?>"><?php echo wc_get_order_status_name( $order->get_status() ); ?></span>
</td>
<td class="order-total" data-title="<?php _e( 'Total', 'um-woocommerce' ); ?>">
<?php echo $order->get_formatted_order_total() ?></td>
<td class="order-detail">
<a href="<?php echo esc_url( "$url#$order_id" ); ?>" class="um-woo-view-order um-tip-n"
title="<?php esc_attr_e( 'View order', 'um-woocommerce' ); ?>"><i class="um-icon-eye"></i></a>
<?php do_action( 'um_woocommerce_orders_tab_actions', $order, $customer_orders ); ?>
</td>
<?php
if ( UM()->options()->get('woo_account_order_ations') ) {
$actions = wc_get_account_orders_actions( $order );
echo '<td class="order-actions">';
if ( !empty( $actions ) ) {
foreach ( $actions as $key => $action ) {
echo '' . esc_html( $action['name'] ) . '';
}
}
echo '</td>';
}
?>
<?php do_action( 'um_woocommerce_orders_tab_after_table_row', $order, $customer_orders ); ?>
</tr>
<?php } ?>
</tbody>
There is no get_name() function for order object. You need loop through the order items to get the product object and use the $product->get_name() function
<?php foreach ($order->get_items() as $item_key => $item) {
$product = $item->get_product();
?>
<td class="order-total" data-title="<?php _e( 'Item', 'um-woocommerce' ); ?>">
<?php echo $product->get_name() ?>
</td>
<?php } ?>
I am using WordPress with WooCommerce and WooCommerce subscriptions plugins and below is my code in which I have added a custom field called (Mindesk VAR Client User - Dropdown) to show in "Edit Subscription" admin page saving to my custom field based on subscription ID.
This is how it's looking like.
And this is my working code.
<?php
// action triggered when we go to add/edit subscription page
add_action('woocommerce_admin_order_data_after_order_details', 'showWCSubscriptionCustomFields');
add_action('woocommerce_process_shop_order_meta', 'saveWCSubscriptionCustomFields');
function showWCSubscriptionCustomFields($subscription) {
$currentPage = get_current_screen();
// If page is "Edit Subscription" page, then only show
if ($currentPage->action == 'add')
return;
// Getting all the users
$mindeskUsers = getAllUsers();
?>
<br class="clear" />
<p class="form-field form-field-wide">
<label for="mindesk_wc_subscriptions_var_client_user_id">Mindesk VAR Client User:</label>
<?php
$selectedUser = get_post_meta($subscription->get_id(), 'mindesk_wc_subscriptions_var_client_user_id', true);
echo getUsersListSelect('mindesk_wc_subscriptions_var_client_user_id', $selectedUser, $mindeskUsers, 'mindesk_select2');
?>
</p>
<?php
}
function saveWCSubscriptionCustomFields($subscription_id) {
// wc_clean() and wc_sanitize_textarea() are WooCommerce sanitization functions
update_post_meta($subscription_id, 'mindesk_wc_subscriptions_var_client_user_id', wc_clean($_POST['mindesk_wc_subscriptions_var_client_user_id']));
}
This is working fine for me.
Now I have a custom requirement to add a button something called as Transfer in each row in http:://www.mywebsite.com/my-account/subscriptions/ page.
For example this page somewhere beside Total.
After clicking on that button, the popup should come with a form and I should be able to save a field "Transfer to VAR Client User" based on subscription ID same as I have shown you a working code above with my custom field.
I have tried to do R & D but most of the links suggesting to add custom fields and all in "My Account" page (/my-account) But I want to achieve the same in "my-account/subscriptions" page.
Can anyone guide me how can I achieve this? Any suggestion will be highly appreciated.
Thanks
You can copy the my-subscriptions.php file from the woocommerce-subscriptions/templates/myaccount and add it to your active theme woocommerce folder create folder myaccount and paste my-subscriptions.php. and then modify as per your requirement.
<?php if ( ! empty( $subscriptions ) ) : ?>
<table class="my_account_subscriptions my_account_orders woocommerce-orders-table woocommerce-MyAccount-subscriptions shop_table shop_table_responsive woocommerce-orders-table--subscriptions">
<thead>
<tr>
<th class="subscription-id order-number woocommerce-orders-table__header woocommerce-orders-table__header-order-number woocommerce-orders-table__header-subscription-id"><span class="nobr"><?php esc_html_e( 'Subscription', 'woocommerce-subscriptions' ); ?></span></th>
<th class="subscription-status order-status woocommerce-orders-table__header woocommerce-orders-table__header-order-status woocommerce-orders-table__header-subscription-status"><span class="nobr"><?php esc_html_e( 'Status', 'woocommerce-subscriptions' ); ?></span></th>
<th class="subscription-next-payment order-date woocommerce-orders-table__header woocommerce-orders-table__header-order-date woocommerce-orders-table__header-subscription-next-payment"><span class="nobr"><?php echo esc_html_x( 'Next payment', 'table heading', 'woocommerce-subscriptions' ); ?></span></th>
<th class="subscription-total order-total woocommerce-orders-table__header woocommerce-orders-table__header-order-total woocommerce-orders-table__header-subscription-total"><span class="nobr"><?php echo esc_html_x( 'Total', 'table heading', 'woocommerce-subscriptions' ); ?></span></th>
<th class="subscription-total order-total woocommerce-orders-table__header woocommerce-orders-table__header-order-total woocommerce-orders-table__header-subscription-total"><span class="nobr"><?php echo esc_html_x( 'Transfer', 'table heading', 'woocommerce-subscriptions' ); ?></span></th>
<th class="subscription-actions order-actions woocommerce-orders-table__header woocommerce-orders-table__header-order-actions woocommerce-orders-table__header-subscription-actions"> </th>
</tr>
</thead>
<tbody>
<?php /** #var WC_Subscription $subscription */ ?>
<?php foreach ( $subscriptions as $subscription_id => $subscription ) : ?>
<tr class="order woocommerce-orders-table__row woocommerce-orders-table__row--status-<?php echo esc_attr( $subscription->get_status() ); ?>">
<td class="subscription-id order-number woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-id woocommerce-orders-table__cell-order-number" data-title="<?php esc_attr_e( 'ID', 'woocommerce-subscriptions' ); ?>">
<?php echo esc_html( sprintf( _x( '#%s', 'hash before order number', 'woocommerce-subscriptions' ), $subscription->get_order_number() ) ); ?>
<?php do_action( 'woocommerce_my_subscriptions_after_subscription_id', $subscription ); ?>
</td>
<td class="subscription-status order-status woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-status woocommerce-orders-table__cell-order-status" data-title="<?php esc_attr_e( 'Status', 'woocommerce-subscriptions' ); ?>">
<?php echo esc_attr( wcs_get_subscription_status_name( $subscription->get_status() ) ); ?>
</td>
<td class="subscription-next-payment order-date woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-next-payment woocommerce-orders-table__cell-order-date" data-title="<?php echo esc_attr_x( 'Next Payment', 'table heading', 'woocommerce-subscriptions' ); ?>">
<?php echo esc_attr( $subscription->get_date_to_display( 'next_payment' ) ); ?>
<?php if ( ! $subscription->is_manual() && $subscription->has_status( 'active' ) && $subscription->get_time( 'next_payment' ) > 0 ) : ?>
<br/><small><?php echo esc_attr( $subscription->get_payment_method_to_display( 'customer' ) ); ?></small>
<?php endif; ?>
</td>
<td class="subscription-total order-total woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-total woocommerce-orders-table__cell-order-total" data-title="<?php echo esc_attr_x( 'Total', 'Used in data attribute. Escaped', 'woocommerce-subscriptions' ); ?>">
<?php echo wp_kses_post( $subscription->get_formatted_order_total() ); ?>
</td>
<td class="subscription-actions order-actions woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-actions woocommerce-orders-table__cell-order-actions">
<?php echo esc_html_x( 'Transfer', 'Transfer a subscription', 'woocommerce-subscriptions' ); ?>
</td>
<td class="subscription-actions order-actions woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-actions woocommerce-orders-table__cell-order-actions">
<?php echo esc_html_x( 'View', 'view a subscription', 'woocommerce-subscriptions' ); ?>
<?php do_action( 'woocommerce_my_subscriptions_actions', $subscription ); ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php if ( 1 < $max_num_pages ) : ?>
<div class="woocommerce-pagination woocommerce-pagination--without-numbers woocommerce-Pagination">
<?php if ( 1 !== $current_page ) : ?>
<a class="woocommerce-button woocommerce-button--previous woocommerce-Button woocommerce-Button--previous button" href="<?php echo esc_url( wc_get_endpoint_url( 'subscriptions', $current_page - 1 ) ); ?>"><?php esc_html_e( 'Previous', 'woocommerce-subscriptions' ); ?></a>
<?php endif; ?>
<?php if ( intval( $max_num_pages ) !== $current_page ) : ?>
<a class="woocommerce-button woocommerce-button--next woocommerce-Button woocommerce-Button--next button" href="<?php echo esc_url( wc_get_endpoint_url( 'subscriptions', $current_page + 1 ) ); ?>"><?php esc_html_e( 'Next', 'woocommerce-subscriptions' ); ?></a>
<?php endif; ?>
</div>
<?php endif; ?>
<?php else : ?>
<p class="no_subscriptions woocommerce-message woocommerce-message--info woocommerce-Message woocommerce-Message--info woocommerce-info">
<?php if ( 1 < $current_page ) :
printf( esc_html__( 'You have reached the end of subscriptions. Go to the %sfirst page%s.', 'woocommerce-subscriptions' ), '', '' );
else :
esc_html_e( 'You have no active subscriptions.', 'woocommerce-subscriptions' );
?>
<a class="woocommerce-Button button" href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>">
<?php esc_html_e( 'Browse products', 'woocommerce-subscriptions' ); ?>
</a>
<?php
endif; ?>
</p>
<?php endif; ?>
Tested and works
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>
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.