I am developing a plugin where I need to display some custom select product. So far I can able to make the option field but how can i save them as option field with comma separated product ids like.
45,78,55,48,
here is an example of searchable multiple select option for WooCommerce product.
Here is my code
function crp_select_products() {
global $post, $woocommerce;
$product_ids = array();
?>
<div class="options_group">
<?php if ( $woocommerce->version >= '3.0' ) : ?>
<p class="form-field">
<label for="related_ids"><?php _e( 'Search Products', 'woocommerce' ); ?></label>
<select class="wc-product-search" multiple="multiple" style="width: 50%;" id="related_ids" name="related_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations">
<?php
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
}
}
?>
</select> <?php echo wc_help_tip( __( 'Select products are for sale product.', 'woocommerce' ) ); ?>
</p>
<?php endif; ?>
</div>
<?php
}
First, there is something missing in your function, to display the saved data in it.
After, this special field need to be displayed inside a form that will have a submit button. So it depends where you are using your function.
Here below is an example displaying that custom field as a custom product setting, save the data and display the saved data in it:
function crp_get_product_related_ids() {
global $post, $woocommerce;
$product_ids = get_post_meta( $post->ID, '_related_ids', true );
if( empty($product_ids) )
$product_ids = array();
?>
<div class="options_group">
<?php if ( $woocommerce->version >= '3.0' ) : ?>
<p class="form-field">
<label for="related_ids"><?php _e( 'Search Products', 'woocommerce' ); ?></label>
<select class="wc-product-search" multiple="multiple" style="width: 50%;" id="related_ids" name="related_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations">
<?php
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
}
}
?>
</select> <?php echo wc_help_tip( __( 'Select products are for sale product.', 'woocommerce' ) ); ?>
</p>
<?php endif; ?>
</div>
<?php
}
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_fied_in_product_general_fields', 20 );
function add_custom_fied_in_product_general_fields() {
global $post, $woocommerce;
crp_get_product_related_ids();
}
add_action( 'woocommerce_process_product_meta', 'process_product_meta_custom_fied', 20, 1 );
function process_product_meta_custom_fied( $product_id ){
if( isset( $_POST['crosssell_ids'] ) ){
update_post_meta( $product_id, '_related_ids', array_map( 'intval', (array) wp_unslash( $_POST['related_ids'] ) ) );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Related
This is the code to add a new field in Dokan:
Here is also the link to how it works : https://nayemdevs.com/how-to-add-a-new-field-on-product-upload-form-dokan-multivendor/
Please can someone tell me how to add a drop down menu for attributes instead of a field.
I want to add a drop down menu of specific attributes in Dokan POP UP
Este codigo va en el tema hijo activo .
Function.php
/*
* Adding extra field on New product popup/without popup form
*/
add_action( 'dokan_new_product_after_product_tags','new_product_field',10 );
function new_product_field(){ ?>
<div class="dokan-form-group">
<input type="text" class="dokan-form-control" name="new_field" placeholder="<?php esc_attr_e( 'Product Code', 'dokan-lite' ); ?>">
</div>
<?php
}
/*
* Saving product field data for edit and update
*/
add_action( 'dokan_new_product_added','save_add_product_meta', 10, 2 );
add_action( 'dokan_product_updated', 'save_add_product_meta', 10, 2 );
function save_add_product_meta($product_id, $postdata){
if ( ! dokan_is_user_seller( get_current_user_id() ) ) {
return;
}
if ( ! empty( $postdata['new_field'] ) ) {
update_post_meta( $product_id, 'new_field', $postdata['new_field'] );
}
}
/*
* Showing field data on product edit page
*/
add_action('dokan_product_edit_after_product_tags','show_on_edit_page',99,2);
function show_on_edit_page($post, $post_id){
$new_field = get_post_meta( $post_id, 'new_field', true );
?>
<div class="dokan-form-group">
<input type="hidden" name="new_field" id="dokan-edit-product-id" value="<?php echo esc_attr( $post_id ); ?>"/>
<label for="new_field" class="form-label"><?php esc_html_e( 'Product Code', 'dokan-lite' ); ?></label>
<?php dokan_post_input_box( $post_id, 'new_field', array( 'placeholder' => __( 'product code', 'dokan-lite' ), 'value' => $new_field ) ); ?>
<div class="dokan-product-title-alert dokan-hide">
<?php esc_html_e( 'Please enter product code!', 'dokan-lite' ); ?>
</div>
</div> <?php
}
// showing on single product page
add_action('woocommerce_single_product_summary','show_product_code',13);
function show_product_code(){
global $product;
if ( empty( $product ) ) {
return;
}
$new_field = get_post_meta( $product->get_id(), 'new_field', true );
if ( ! empty( $new_field ) ) {
?>
<span class="details"><?php echo esc_attr__( 'Product Code:', 'dokan-lite' ); ?> <strong><?php echo esc_attr( $new_field ); ?></strong></span>
<?php
}
}
I am working on a woo-commerce theme and it's good but on the checkout page clients want's to change the order of some blocks. so he wanted to move payment methods before order summary. I did that by adding the code in function.php which is given below. The code works fine but after adding the code i can't see order summary on checkout page. I can't see Subtotal, Shipment and Total Cost. I don't know what is wrong with this code. please help me out with this.
add_action( 'woocommerce_checkout_order_review', 'reordering_checkout_order_review', 1 );
function reordering_checkout_order_review(){
remove_action('woocommerce_checkout_order_review','woocommerce_checkout_payment', 20 );
add_action( 'woocommerce_checkout_order_review', 'custom_checkout_payment', 8 );
add_action( 'woocommerce_checkout_order_review', 'after_custom_checkout_payment', 9 );
add_action( 'woocommerce_checkout_order_review', 'custom_checkout_place_order', 20 );
}
function after_custom_checkout_payment() {
?>
<div id="before-order-table" class="woocommerce-checkout-custom-text">
<h3><?php _e("Your order", "woocommerce"); ?></h3>
<p><?php /* _e("I have accepted the terms and bla bla bla", "woocommerce"); */?></p>
</div>
<?php
}
function custom_checkout_payment() {
$checkout = WC()->checkout();
if ( WC()->cart->needs_payment() ) {
$available_gateways = WC()->payment_gateways()->get_available_payment_gateways();
WC()->payment_gateways()->set_current_gateway( $available_gateways );
} else {
$available_gateways = array();
}
if ( ! is_ajax() ) {
// do_action( 'woocommerce_review_order_before_payment' );
}
?>
<div id="payment" class="woocommerce-checkout-payment-gateways">
<?php if ( WC()->cart->needs_payment() ) : ?>
<ul class="wc_payment_methods payment_methods methods">
<?php
if ( ! empty( $available_gateways ) ) {
foreach ( $available_gateways as $gateway ) {
wc_get_template( 'checkout/payment-method.php', array( 'gateway' => $gateway ) );
}
} else {
echo '<li class="woocommerce-notice woocommerce-notice--info woocommerce-info">';
echo apply_filters( 'woocommerce_no_available_payment_methods_message', WC()->customer->get_billing_country() ? esc_html__( 'Sorry, it seems that there are no available payment methods for your state. Please contact us if you require assistance or wish to make alternate arrangements.', 'woocommerce' ) : esc_html__( 'Please fill in your details above to see available payment methods.', 'woocommerce' ) ) . '</li>'; // #codingStandardsIgnoreLine
}
?>
</ul>
<?php endif; ?>
</div>
<?php
}
function custom_checkout_place_order() {
$checkout = WC()->checkout();
$order_button_text = apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) );
?>
<div id="payment-place-order" class="woocommerce-checkout-place-order">
<div class="form-row place-order">
<noscript>
<?php esc_html_e( 'Since your browser does not support JavaScript, or it is disabled, please ensure you click the <em>Update Totals</em> button before placing your order. You may be charged more than the amount stated above if you fail to do so.', 'woocommerce' ); ?>
<br/><button type="submit" class="button alt" name="woocommerce_checkout_update_totals" value="<?php esc_attr_e( 'Update totals', 'woocommerce' ); ?>"><?php esc_html_e( 'Update totals', 'woocommerce' ); ?></button>
</noscript>
<?php wc_get_template( 'checkout/terms.php' ); ?>
<?php do_action( 'woocommerce_review_order_before_submit' ); ?>
<?php echo apply_filters( 'woocommerce_order_button_html', '<button type="submit" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="' . esc_attr( $order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '">' . esc_html( $order_button_text ) . '</button>' ); // #codingStandardsIgnoreLine ?>
<?php do_action( 'woocommerce_review_order_after_submit' ); ?>
<?php wp_nonce_field( 'woocommerce-process_checkout', 'woocommerce-process-checkout-nonce' ); ?>
</div>
</div>
<?php
if ( ! is_ajax() ) {
do_action( 'woocommerce_review_order_after_payment' );
}
}
You can also check with:
add_filter( 'woocommerce_checkout_fields' , 'custom_order_comments_checkout_fields' );
function custom_order_comments_checkout_fields( $fields ) {
if ( !WC()->cart->is_empty()):
$output = '';
$count = 0;
$cart_item_count = WC()->cart->get_cart_contents_count();
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ):
$count++;
// Displaying the product title
$output .= 'Product title: ' . $cart_item['data']->post->post_title;
// New line (for next item)
if($count < $cart_item_count)
$output .= '';
endforeach;
$fields['order']['order_comments']['default'] = $output;
endif;
return $fields;
}
In WooCommerce, I'm trying to add some custom code into my active theme's functions.php file, which will enable for variable products to display the attribute dropdowns, the quantity field and the add to cart button on shop and archive pages.
Here's my actual code:
// Display variations dropdowns on shop page for variable products
add_filter( 'woocommerce_loop_add_to_cart_link', 'woo_display_variation_dropdown_on_shop_page' );
function woo_display_variation_dropdown_on_shop_page() {
global $product;
if( $product->is_type( 'variable' )) {
$attribute_keys = array_keys( $product->get_attributes() );
?>
<form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->id ); ?>" data-product_variations="<?php echo htmlspecialchars( json_encode( $product->get_available_variations() ) ) ?>">
<?php do_action( 'woocommerce_before_variations_form' ); ?>
<?php if ( empty( $product->get_available_variations() ) && false !== $product->get_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 ( $product->get_attributes() as $attribute_name => $options ) : ?>
<tr>
<td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
<td class="value">
<?php
$selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ) : $product->get_variation_default_attribute( $attribute_name );
wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) );
echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" 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
/**
* woocommerce_before_single_variation Hook.
*/
do_action( 'woocommerce_before_single_variation' );
/**
* woocommerce_single_variation hook. Used to output the cart button and placeholder for variation data.
* #since 2.4.0
* #hooked woocommerce_single_variation - 10 Empty div for variation data.
* #hooked woocommerce_single_variation_add_to_cart_button - 20 Qty and cart button.
*/
do_action( 'woocommerce_single_variation' );
/**
* woocommerce_after_single_variation Hook.
*/
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 } else {
echo sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() )
);
}
}
But the following errors are thrown and having issues making it work:
Your PHP code changes were rolled back due to an error on line 666 of file wp-content/plugins/woocommerce-composite-products/includes/wc-cp-template-functions.php. Please fix and try saving again.
Uncaught Error: Call to a member function get_type() on null in
wp-content/plugins/woocommerce-composite-products/includes/wc-cp-template-functions.php:666
Stack trace:
#0 wp-includes/class-wp-hook.php(286): wc_cp_before_add_to_cart_button('')
#1 wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters('', Array)
#2 wp-includes/plugin.php(465): WP_Hook->do_action(Array)
#3 wp-content/themes/electro-child/functions.php(2228): do_action('woocommerce_bef...')
#4 wp-includes/class-wp-hook.php(288): woo_display_variation_dropdown_on_shop_page('<a href="https:...')
#5 wp-includes/plugin.php(208): WP_Hook->apply_filters('<a href="https:...', Array)
#6 wp-content/plugins/woocommerce/templates/loop/add
I think there's a conflict with the Composite product plugin I'm using.
Any help is hugely appreciated.
The biggest mistake in your code is that you just forgot that you are using a filter hook and in that case, you need to return the output, but not to echo it.
Then you will use the hook arguments that are included in this filter hook instead.
Also you just need to change the behavior for variable products only, because, with your actual code, as you are also using the WooCommerce Composite Products plugin, it gives you the reported error.
So try instead something like (untested):
// Display variations dropdowns on shop page for variable products
add_filter( 'woocommerce_loop_add_to_cart_link', 'woo_display_variation_dropdown_on_shop_page', 10, 3 );
function woo_display_variation_dropdown_on_shop_page( $add_to_cart_link, $product, $args ) {
if( $product->is_type('variable') ) {
$attribute_keys = array_keys( $product->get_attributes() );
ob_start(); // Start buffering
?>
<form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->id ); ?>" data-product_variations="<?php echo htmlspecialchars( json_encode( $product->get_available_variations() ) ) ?>">
<?php do_action( 'woocommerce_before_variations_form' ); ?>
<?php if ( empty( $product->get_available_variations() ) && false !== $product->get_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 ( $product->get_attributes() as $attribute_name => $options ) : ?>
<tr>
<td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
<td class="value">
<?php
$selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ) : $product->get_variation_default_attribute( $attribute_name );
wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) );
echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" 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
/**
* woocommerce_before_single_variation Hook.
*/
do_action( 'woocommerce_before_single_variation' );
/**
* woocommerce_single_variation hook. Used to output the cart button and placeholder for variation data.
* #since 2.4.0
* #hooked woocommerce_single_variation - 10 Empty div for variation data.
* #hooked woocommerce_single_variation_add_to_cart_button - 20 Qty and cart button.
*/
do_action( 'woocommerce_single_variation' );
/**
* woocommerce_after_single_variation Hook.
*/
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
$add_to_cart_link = ob_get_clean(); // The buffered content
}
return $add_to_cart_link;
}
I have followed this answer How to add more custom field in Linked Product of Woocommerce to add a custom select field to my Linked Product screen in Woocommerce. This new field is meta key is subscription_toggle_product.
It's working fine, but once you have selected a product, you can't delete it (there is no "Empty" option or cross symbol). I haven't got a clue how I can allow the selection to be deselected. I've tried adding an <option> with an empty value, but it hasn't worked.
Here is my code:
// Display the custom fields in the "Linked Products" section
add_action( 'woocommerce_product_options_related', 'woocom_linked_products_data_custom_field' );
// Save to custom fields
add_action( 'woocommerce_process_product_meta', 'woocom_linked_products_data_custom_field_save' );
// Function to generate the custom fields
function woocom_linked_products_data_custom_field() {
global $woocommerce, $post;
$product = wc_get_product( $post->ID );
?>
<p class="form-field">
<label for="subscription_toggle_product"><?php _e( 'Subscription Toggle Product', 'woocommerce' ); ?></label>
<select class="wc-product-search" style="width: 50%;" id="subscription_toggle_product" name="subscription_toggle_product" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>">
<?php
$product_id = get_post_meta( $post->ID, '_subscription_toggle_product_id', true );
if ( $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
}
}
?>
</select>
</p>
<?php
}
// Function the save the custom fields
function woocom_linked_products_data_custom_field_save( $post_id ){
if (isset($_POST['subscription_toggle_product'])) {
$product_field_type = $_POST['subscription_toggle_product'];
update_post_meta( $post_id, '_subscription_toggle_product_id', $product_field_type );
}
}
This kind of select field only works with a defined multiple attribute and work with an array of values. so you can't use it for a simple ID. If you add to your select field multiple="multiple" attribute it will work.
Also since Woocommerce 3 things have changed:
- There are better hooks to save the data.
- You can now use CRUD Objects and related methods.
The following code will work for multiple product IDs (an array of products IDs):
// Display a custom select field in "Linked Products" section
add_action( 'woocommerce_product_options_related', 'display_linked_products_data_custom_field' );
function display_linked_products_data_custom_field() {
global $product_object, $post;
?>
<p class="form-field">
<label for="subscription_toggle_products"><?php _e( 'Subscription Toggle Products', 'woocommerce' ); ?></label>
<select class="wc-product-search" multiple="multiple" style="width: 50%;" id="subscription_toggle_ids" name="_subscription_toggle_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>">
<?php
$product_ids = $product_object->get_meta( '_subscription_toggle_ids' );
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
}
}
?>
</select>
</p>
<?php
}
// Save the values to the product
add_action( 'woocommerce_admin_process_product_object', 'save_linked_products_data_custom_field_value', 10, 1 );
function save_linked_products_data_custom_field_value( $product ){
$data = isset( $_POST['_subscription_toggle_ids'] ) ? array_map( 'intval', (array) $_POST['_subscription_toggle_ids'] ) : array();
$product->update_meta_data( '_subscription_toggle_ids', $data );
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
Here's what I'm attempting to do and where.
In the check out process for an order, in the plug-in WooCommerce; you are taken to a page when the check out is process is complete. It displays an overview of the order details. The template file used to output this page is order-details.php.
Here's what I'd like to add
I would like to display the product's product category like so:
This is area of code I'm attempting to add to, the first section of the order details table.
<tbody>
<?php
if ( sizeof( $order->get_items() ) > 0 ) {
foreach( $order->get_items() as $item ) {
$_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$item_meta = new WC_Order_Item_Meta( $item['item_meta'], $_product );
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
<td class="product-name">
<?php
if ( $_product && ! $_product->is_visible() )
echo apply_filters( 'woocommerce_order_item_name', $item['name'], $item );
else
echo apply_filters( 'woocommerce_order_item_name', sprintf( '%s', get_permalink( $item['product_id'] ), $item['name'] ), $item );
echo apply_filters( 'woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf( '× %s', $item['qty'] ) . '</strong>', $item );
This is my understanding so far;
That a custom query is being applied.
That for each product listed details are being echoed.
That a WooCommerce Class is being used to get specific product meta.
My understandings are observations at my current knowledge level.
What I was hoping was that I could use something like
echo apply_filters( 'woocommerce_order_item_name', ' <strong class="product-category">' . e_( $item['product_cat'] ) . '</strong>', $item );
I can't find a more relevant filter and I'm really not sure that ['product_cat'] is even relevant here. This is an example of the way I'm attempting to solve my requirement.
As you can tell, I don't work in PHP full time. I'm learning as much as I can when I can. Advise would be greatly appreciated
This is how you might add the product categories to the Product title. I don't particularly like how it is outputting for me, but then I am testing it on a variable subscription, so that is amending its own data. This would go in your theme's functions.php
function kia_woocommerce_order_item_name( $name, $item ){
$product_id = $item['product_id'];
$tax = 'product_cat';
$terms = wp_get_post_terms( $product_id, $tax, array( 'fields' => 'names' ) );
if( $terms && ! is_wp_error( $terms )) {
$taxonomy = get_taxonomy($tax);
$name .= '<label>' . $taxonomy->label . ': </label>' . implode( ', ', $terms );
}
return $name;
}
add_filter( 'woocommerce_order_item_name', 'kia_woocommerce_order_item_name', 10, 2 );
Otherwise you could copy the order-details.php template into your own theme (so yourtheme/woocommerce/order/order-details.php and add the code directly. This would give you more control over where you want the category to appear since there aren't a lot of hooks available in this section, but put you at risk if WooCommerce ever modifies this code. I have worked on a few sites that broke because the the theme was using outdated template.
Any way, an example would be:
<?php
/**
* Order details
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.2.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $woocommerce;
$order = new WC_Order( $order_id );
?>
<h2><?php _e( 'Order Details', 'woocommerce' ); ?></h2>
<table class="shop_table order_details">
<thead>
<tr>
<th class="product-name"><?php _e( 'Product', 'woocommerce' ); ?></th>
<th class="product-total"><?php _e( 'Total', 'woocommerce' ); ?></th>
</tr>
</thead>
<tfoot>
<?php
if ( $totals = $order->get_order_item_totals() ) foreach ( $totals as $total ) :
?>
<tr>
<th scope="row"><?php echo $total['label']; ?></th>
<td><?php echo $total['value']; ?></td>
</tr>
<?php
endforeach;
?>
</tfoot>
<tbody>
<?php
if ( sizeof( $order->get_items() ) > 0 ) {
foreach( $order->get_items() as $item ) {
$_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$item_meta = new WC_Order_Item_Meta( $item['item_meta'], $_product );
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
<td class="product-name">
<?php
if ( $_product && ! $_product->is_visible() )
echo apply_filters( 'woocommerce_order_item_name', $item['name'], $item );
else
echo apply_filters( 'woocommerce_order_item_name', sprintf( '%s', get_permalink( $item['product_id'] ), $item['name'] ), $item );
echo apply_filters( 'woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf( '× %s', $item['qty'] ) . '</strong>', $item );
$item_meta->display();
// insert product category here
$tax = 'product_cat';
$terms = wp_get_post_terms( $_product->id, $tax, array( 'fields' => 'names' ) );
if( $terms && ! is_wp_error( $terms )) {
$taxonomy = get_taxonomy($tax);
echo '<strong>' . $taxonomy->label . ': </strong>' . implode( ', ', $terms );
}
// end edit
if ( $_product && $_product->exists() && $_product->is_downloadable() && $order->is_download_permitted() ) {
$download_files = $order->get_item_downloads( $item );
$i = 0;
$links = array();
foreach ( $download_files as $download_id => $file ) {
$i++;
$links[] = '<small>' . sprintf( __( 'Download file%s', 'woocommerce' ), ( count( $download_files ) > 1 ? ' ' . $i . ': ' : ': ' ) ) . esc_html( $file['name'] ) . '</small>';
}
echo '<br/>' . implode( '<br/>', $links );
}
?>
</td>
<td class="product-total">
<?php echo $order->get_formatted_line_subtotal( $item ); ?>
</td>
</tr>
<?php
if ( $order->has_status( array( 'completed', 'processing' ) ) && ( $purchase_note = get_post_meta( $_product->id, '_purchase_note', true ) ) ) {
?>
<tr class="product-purchase-note">
<td colspan="3"><?php echo wpautop( do_shortcode( $purchase_note ) ); ?></td>
</tr>
<?php
}
}
}
do_action( 'woocommerce_order_items_table', $order );
?>
</tbody>
</table>
<?php do_action( 'woocommerce_order_details_after_order_table', $order ); ?>
<header>
<h2><?php _e( 'Customer details', 'woocommerce' ); ?></h2>
</header>
<dl class="customer_details">
<?php
if ( $order->billing_email ) echo '<dt>' . __( 'Email:', 'woocommerce' ) . '</dt><dd>' . $order->billing_email . '</dd>';
if ( $order->billing_phone ) echo '<dt>' . __( 'Telephone:', 'woocommerce' ) . '</dt><dd>' . $order->billing_phone . '</dd>';
// Additional customer details hook
do_action( 'woocommerce_order_details_after_customer_details', $order );
?>
</dl>
<?php if ( ! wc_ship_to_billing_address_only() && $order->needs_shipping_address() && get_option( 'woocommerce_calc_shipping' ) !== 'no' ) : ?>
<div class="col2-set addresses">
<div class="col-1">
<?php endif; ?>
<header class="title">
<h3><?php _e( 'Billing Address', 'woocommerce' ); ?></h3>
</header>
<address><p>
<?php
if ( ! $order->get_formatted_billing_address() ) _e( 'N/A', 'woocommerce' ); else echo $order->get_formatted_billing_address();
?>
</p></address>
<?php if ( ! wc_ship_to_billing_address_only() && $order->needs_shipping_address() && get_option( 'woocommerce_calc_shipping' ) !== 'no' ) : ?>
</div><!-- /.col-1 -->
<div class="col-2">
<header class="title">
<h3><?php _e( 'Shipping Address', 'woocommerce' ); ?></h3>
</header>
<address><p>
<?php
if ( ! $order->get_formatted_shipping_address() ) _e( 'N/A', 'woocommerce' ); else echo $order->get_formatted_shipping_address();
?>
</p></address>
</div><!-- /.col-2 -->
</div><!-- /.col2-set -->
<?php endif; ?>
<div class="clear"></div>