Skip to content

Instantly share code, notes, and snippets.

@SiR-DanieL
Created November 7, 2023 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SiR-DanieL/325fb72c9f569ab3775fd70873c6ead4 to your computer and use it in GitHub Desktop.
Save SiR-DanieL/325fb72c9f569ab3775fd70873c6ead4 to your computer and use it in GitHub Desktop.
How to Make Your Edit Order Screen More Efficient
<?php
add_action( 'woocommerce_after_order_itemmeta', 'admin_order_add_product_info_and_link', 10, 3 );
function admin_order_add_product_info_and_link( $item_id, $item, $_product ) {
if ( ! $_product ) return;
$product_link = get_permalink( $_product->get_id() );
$attributes_to_display = ['slug_1', 'slug_2', 'slug_3']; // Specify the attribute slugs you want to display
$target_product = $_product;
if ($_product->is_type('variation')) {
$parent_id = $_product->get_parent_id();
$target_product = wc_get_product($parent_id);
}
$attributes = $target_product->get_attributes();
echo '<hr>';
echo '<table cellspacing="0" class="display_meta"><tbody>';
foreach ( $attributes_to_display as $attr_slug ) {
if ( isset( $attributes['pa_' . $attr_slug] ) ) {
$attribute = $attributes[ 'pa_' . $attr_slug ];
$attr_label = wc_attribute_label( 'pa_' . $attr_slug );
if ( $attribute->is_taxonomy() ) {
$values = wc_get_product_terms( $target_product->get_id(), 'pa_' . $attr_slug, array( 'fields' => 'names' ) );
if ( ! empty( $values ) ) {
echo '<tr><th>' . $attr_label . '</th><td>' . join( ', ', $values ) . '</td></tr>';
}
} else {
// For non-taxonomy based attributes
$values = array_map( 'trim', explode( WC_DELIMITER, $attribute->get_options() ) );
echo '<tr><th>' . $attr_label . '</th><td>' . join( ', ', $values ) . '</td></tr>';
}
}
}
echo '</tbody></table>';
echo '<a href="' . esc_url( $product_link ) . '" target="_blank">' . __( 'View Product', 'woocommerce' ) . '</a>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment