Errors when Adding "Show Single Variation" Code into functions.php - php

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;
}

Related

Hide, remove or disable the Add to Cart button in WooCommerce, if the product price is zero or 100% Discount

I need a solution to hide the "Add to Cart" button from the product page when the product price is zero or 100% Discount
I think the following code is probably for the add to cart section
This code was in the following path
mytheme / woocommerce / single-product / add-to-cart / simple.php
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product;
if ( ! $product->is_purchasable() ) {
return;
}
echo wc_get_stock_html( $product );
$prefix = '_studiare_';
$woo_studiare_btn_link = get_post_meta(get_the_id(), $prefix . 'woo_course_url', true);
$woo_studiare_btn_label = get_post_meta(get_the_id(), $prefix . 'woo_course_label', true);?>
<?php if ( ( empty( $woo_studiare_btn_label ) ) && ( empty( $woo_studiare_btn_link ) ) ) :
if ( $product->is_in_stock() ) : ?>
<?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<form class="cart" method="post" enctype='multipart/form-data'>
<?php
/**
* #since 2.1.0.
*/
do_action( 'woocommerce_before_add_to_cart_button' );
/**
* #since 3.0.0.
*/
do_action( 'woocommerce_before_add_to_cart_quantity' );
woocommerce_quantity_input( array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', 1, $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : $product->get_min_purchase_quantity(),
) );
/**
* #since 3.0.0.
*/
do_action( 'woocommerce_after_add_to_cart_quantity' );
?>
<button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<?php
/**
* #since 2.1.0.
*/
do_action( 'woocommerce_after_add_to_cart_button' );
?>
</form>
<?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
<?php endif; ?>
<?php else: ?>
<?php echo esc_attr($woo_studiare_btn_label); ?>
<?php endif; ?>
change this code:
if ( $product->is_in_stock() ) : ?>
to:
if ( $product->is_in_stock() && $product->get_price() > 0 ) : ?>
Or add below code in your active theme functions.php and check it.
function wpcustom_is_purchasable( $purchasable, $product ){
if( $product->get_price() == 0 )
$purchasable = false;
return $purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'wpcustom_is_purchasable', 10, 2 );

How to open downloadable products in WooCommerce in new tab

I added a custom download link for my resellers to download product images. But I would like to add target="_blank" rel="noopener noreferrer" attributes to the link.
Here's my custom download link:
// Add a Custom Download File to My Account
add_filter( 'woocommerce_customer_get_downloadable_products', 'my_custom_default_download', 9999, 1 );
function my_custom_default_download( $downloads ) {
if ( is_admin() && !defined('DOING_AJAX') )
return;
// Only on checkout page and logged in users
if ( ! is_wc_endpoint_url() || ! is_user_logged_in() )
return;
$wpuser_object = wp_get_current_user();
$allowed_roles = array( 'administrator', 'default_wholesaler', 'wholesaler-non-vat-registered', 'wholesaler-ex-vat-registered', 'wholesaler-ex-non-vat-registered', 'shop_manager');
if ( array_intersect($allowed_roles, $wpuser_object->roles) ){
$downloads[] = array(
'product_name' => 'Product Images (High Resolution)',
'download_name' => 'Download',
'download_url' => 'https://drive.google.com/drive/folders/',
);
return $downloads;
}
}
Here's the updated my-downloads.php file saved to my child theme:
<?php
defined( 'ABSPATH' ) || exit;
$downloads = WC()->customer->get_downloadable_products();
if ( $downloads ) : ?>
<?php do_action( 'woocommerce_before_available_downloads' ); ?>
<h2><?php echo apply_filters( 'woocommerce_my_account_my_downloads_title', esc_html__( 'Available downloads', 'woocommerce' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></h2>
<ul class="woocommerce-Downloads digital-downloads">
<?php foreach ( $downloads as $download ) : ?>
<li>
<?php
do_action( 'woocommerce_available_download_start', $download );
if ( is_numeric( $download['downloads_remaining'] ) ) {
/* translators: %s product name */
echo apply_filters( 'woocommerce_available_download_count', '<span class="woocommerce-Count count">' . sprintf( _n( '%s download remaining', '%s downloads remaining', $download['downloads_remaining'], 'woocommerce' ), $download['downloads_remaining'] ) . '</span> ', $download ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
echo apply_filters( 'woocommerce_available_download_link', '' . $download['download_name'] . '', $download ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
do_action( 'woocommerce_available_download_end', $download );
?>
</li>
<?php endforeach; ?>
</ul>
<?php do_action( 'woocommerce_after_available_downloads' ); ?>
<?php endif; ?>
You've got to update the order-downloads.php file not the my-downloads.php file and save it to the child theme.
Here's the solution:
<?php
/**
* Order Downloads.
*
* This template can be overridden by copying it to yourtheme/woocommerce/order/order-downloads.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* #see https://docs.woocommerce.com/document/template-structure/
* #package WooCommerce\Templates
* #version 3.3.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<section class="woocommerce-order-downloads">
<?php if ( isset( $show_title ) ) : ?>
<h2 class="woocommerce-order-downloads__title"><?php esc_html_e( 'Downloads', 'woocommerce' ); ?></h2>
<?php endif; ?>
<table class="woocommerce-table woocommerce-table--order-downloads shop_table shop_table_responsive order_details">
<thead>
<tr>
<?php foreach ( wc_get_account_downloads_columns() as $column_id => $column_name ) : ?>
<th class="<?php echo esc_attr( $column_id ); ?>"><span class="nobr"><?php echo esc_html( $column_name ); ?></span></th>
<?php endforeach; ?>
</tr>
</thead>
<?php foreach ( $downloads as $download ) : ?>
<tr>
<?php foreach ( wc_get_account_downloads_columns() as $column_id => $column_name ) : ?>
<td class="<?php echo esc_attr( $column_id ); ?>" data-title="<?php echo esc_attr( $column_name ); ?>">
<?php
if ( has_action( 'woocommerce_account_downloads_column_' . $column_id ) ) {
do_action( 'woocommerce_account_downloads_column_' . $column_id, $download );
} else {
switch ( $column_id ) {
case 'download-product':
if ( $download['product_url'] ) {
echo '' . esc_html( $download['product_name'] ) . '';
} else {
echo esc_html( $download['product_name'] );
}
break;
case 'download-file':
echo '' . esc_html( $download['download_name'] ) . '';
break;
case 'download-remaining':
echo is_numeric( $download['downloads_remaining'] ) ? esc_html( $download['downloads_remaining'] ) : esc_html__( '∞', 'woocommerce' );
break;
case 'download-expires':
if ( ! empty( $download['access_expires'] ) ) {
echo '<time datetime="' . esc_attr( date( 'Y-m-d', strtotime( $download['access_expires'] ) ) ) . '" title="' . esc_attr( strtotime( $download['access_expires'] ) ) . '">' . esc_html( date_i18n( get_option( 'date_format' ), strtotime( $download['access_expires'] ) ) ) . '</time>';
} else {
esc_html_e( 'Never', 'woocommerce' );
}
break;
}
}
?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
</section>

Move the output message "On stock" beneath the add to cart button

I am using Wordpress with woocommerce and trying to set up some variable products. But variable products show the "In Stock" messages directly below the input/select field. I want it to be shown below the "Add to cart" button.
I am using a child theme for the files, simple.php, variable.php and variation.php.
Feel free to look at the picture to see what I want to do.
http://art-wood.de/instock-move.png
Currently my variable.php looks like this:
<?php
/**
* Variable product add to cart
*
* This template can be overridden by copying it to yourtheme/woocommerce/single-product/add-to-cart/variable.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you (the theme developer).
* will need to copy the new files to your theme to maintain compatibility. We try to do this.
* as little as possible, but it does happen. When this occurs the version of the template file will.
* be bumped and the readme will list any important changes.
*
* #see http://docs.woothemes.com/document/template-structure/
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
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->id ); ?>" data-product_variations="<?php echo htmlspecialchars( json_encode( $available_variations ) ) ?>">
<?php do_action( 'woocommerce_before_variations_form' ); ?>
<?php if ( empty( $available_variations ) && false !== $available_variations ) : ?>
<div class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></div>
<?php else : ?>
<span class="variations" cellspacing="0">
<?php foreach ( $attributes as $attribute_name => $options ) : ?>
<label class="label" for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label>
<div class="value">
<?php
$selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( $_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>' ) : '';
?>
</div>
<?php endforeach;?>
</span>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<div class="single_variation_wrap">
/**
* 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 do_action( 'woocommerce_after_add_to_cart_form' ); ?>
Does somebody know how to move the message down below the Add to cart button? This would be amazing! May its a simple add to the functions.php maybe its a switch in the variable.php... I don't know that. Otherwise I wouldn't ask :)
I found the solution by myself. Maybe its useful for someone so here it is ;)
Simply add this to your functions.php
remove_action( 'woocommerce_single_variation' , 'woocommerce_single_variation', 10);
add_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 20 );

Parse error: syntax error, unexpected '<' on line 51 - [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
new here and have an issue. I'm trying to find a parse error in the code. It says "line 51" in the message, but I've tried deleting, tags and no resolve. Can someone point me in the right direction as I'm really new to php
<?php
/**
* Variable product add to cart
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.4.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
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->id ); ?>" data-product_variations="<?php echo esc_attr( 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 $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( $_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, 'class' => 'sc-combobox' ) );
echo end( $attribute_keys ) === $attribute_name ? '<a class="reset_variations" href="#">' . __( 'Clear selection', 'woocommerce' ) . '</a>' : '';
?>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<div class="single_variation_wrap" style="display:none;">
<?php
/**
* woocommerce_before_single_variation Hook
*/
do_action( 'woocommerce_before_single_variation' );
<div class="single_variation clearfix"></div> <<--THIS IS LINE 51
<div class="variations_button">
<?php woocommerce_quantity_input( array( 'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : 1 ) ); ?>
<button type="submit" class="sc-button single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<input type="hidden" name="add-to-cart" value="<?php echo absint( $product->id ); ?>" />
<input type="hidden" name="product_id" value="<?php echo absint( $product->id ); ?>" />
<input type="hidden" name="variation_id" class="variation_id" value="" />
</div>
/**
* woocommerce_after_single_variation Hook
*/
do_action( 'woocommerce_after_single_variation' );
?>
</div>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
<?php else : ?>
<p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'scalia' ); ?></p>
<?php endif; ?>
<?php do_action( 'woocommerce_after_variations_form' ); ?>
</form>
<?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
In this block you're opening a PHP block twice without closing a previous one.
<?php
/**
* woocommerce_before_single_variation Hook
*/
do_action( 'woocommerce_before_single_variation' );
<div class="single_variation clearfix"></div> <<--THIS IS LINE 51
<div class="variations_button">
<?php woocommerce_quantity_input

WooCommerce - Adding product category to order-details table

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>

Categories