Skip to content

Commit c359f73

Browse files
committed
Fix #454 #456 #457 simple template extra fields are not working.
Fix #454 #456 #457 simple template extra fields are not working.
1 parent a1495f8 commit c359f73

File tree

1 file changed

+72
-36
lines changed

1 file changed

+72
-36
lines changed

includes/wcdn-template-functions.php

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -898,94 +898,130 @@ function wcdn_get_product_name( $product, $order, $item ) {
898898
$addon_name = $item->get_meta( '_wc_pao_addon_name', true );
899899
$addon_value = $item->get_meta( '_wc_pao_addon_value', true );
900900
$is_addon = ! empty( $addon_value );
901-
if ( $is_addon ) { // Displaying options of product addon.
902-
$addon_html = '<div class="wc-pao-order-item-name">' . esc_html( $addon_name ) . '</div><div class="wc-pao-order-item-value">' . esc_html( $addon_value ) . '</div></div>';
901+
if ( $is_addon ) {
902+
// Displaying options of product addon.
903+
$addon_html = '<div class="wc-pao-order-item-name">' . esc_html( $addon_name ) . '</div><div class="wc-pao-order-item-value">' . esc_html( $addon_value ) . '</div>';
903904
echo wp_kses_post( $addon_html );
905+
echo '</div>';
904906
} else {
905907
$product_id = $item['product_id'];
906908
$prod_name = get_post( $product_id );
907-
$product_name = $prod_name->post_title;
909+
$product_name = $prod_name ? $prod_name->post_title : '';
910+
// Product name.
908911
echo wp_kses_post( apply_filters( 'wcdn_order_item_name', $product_name, $item ) );
909912
echo '</div>';
910-
$item_meta_fields = apply_filters( 'wcdn_product_meta_data', $item['item_meta'], $item );
913+
// Item meta.
914+
$item_meta_fields = apply_filters( 'wcdn_product_meta_data', $item['item_meta'], $item );
915+
if ( null === $item_meta_fields ) {
916+
$item_meta_fields = array();
917+
}
911918
$product_addons = array();
912919
$woocommerce_product_addon = 'woocommerce-product-addons/woocommerce-product-addons.php';
913920
if ( in_array( $woocommerce_product_addon, apply_filters( 'active_plugins', get_option( 'active_plugins', array() ) ), true ) ) {
914-
$product_id = $item['product_id'];
915921
if ( class_exists( 'WC_Product_Addons_Helper' ) ) {
916922
$product_addons = WC_Product_Addons_Helper::get_product_addons( $product_id );
917923
}
918924
}
925+
// Handle YITH add-ons.
926+
$yith_addon_meta_map = array();
927+
if ( isset( $item_meta_fields['_ywapo_meta_data'] ) && is_array( $item_meta_fields['_ywapo_meta_data'] ) ) {
928+
foreach ( $item_meta_fields['_ywapo_meta_data'] as $group ) {
929+
if ( ! is_array( $group ) ) {
930+
continue;
931+
}
932+
foreach ( (array) $group as $maybe ) {
933+
if ( isset( $maybe['addon_id'] ) ) {
934+
$option_id = isset( $maybe['option_id'] ) ? $maybe['option_id'] : 0;
935+
$meta_key = 'ywapo-addon-' . $maybe['addon_id'] . '-' . $option_id;
936+
$yith_addon_meta_map[ $meta_key ] = $maybe;
937+
} else {
938+
foreach ( (array) $maybe as $sub ) {
939+
if ( isset( $sub['addon_id'] ) ) {
940+
$option_id = isset( $sub['option_id'] ) ? $sub['option_id'] : 0;
941+
$meta_key = 'ywapo-addon-' . $sub['addon_id'] . '-' . $option_id;
942+
$yith_addon_meta_map[ $meta_key ] = $sub;
943+
}
944+
}
945+
}
946+
}
947+
}
948+
foreach ( $yith_addon_meta_map as $meta_key => $addon ) {
949+
if ( isset( $addon['display_label'] ) && isset( $addon['display_value'] ) ) {
950+
echo '<div><strong>' . esc_html( $addon['display_label'] ) . ' : </strong>' . wp_kses_post( $addon['display_value'] ) . '</div>';
951+
}
952+
if ( isset( $item_meta_fields[ $meta_key ] ) ) {
953+
unset( $item_meta_fields[ $meta_key ] );
954+
}
955+
}
956+
}
957+
// Handle Extra Product Options (EPO).
958+
$epo_data = $item->get_meta( '_tmcartepo_data', true );
959+
if ( ! empty( $epo_data ) && is_array( $epo_data ) ) {
960+
foreach ( $epo_data as $epo ) {
961+
if ( ! empty( $epo['name'] ) && isset( $epo['value'] ) ) {
962+
echo '<div><strong>' . esc_html( $epo['name'] ) . ' : </strong>' . wp_kses_post( $epo['value'] ) . '</div>';
963+
}
964+
}
965+
}
966+
// Handle normal attributes / addons.
919967
if ( version_compare( get_option( 'woocommerce_version' ), '3.0.0', '>=' ) ) {
920968
if ( isset( $item['variation_id'] ) && 0 !== $item['variation_id'] ) {
921969
$variation = wc_get_product( $item['product_id'] );
922970
foreach ( $item_meta_fields as $key => $value ) {
923-
if ( ! ( 0 === strpos( $key, '_' ) ) ) {
971+
if ( 0 !== strpos( $key, '_' ) ) {
924972
if ( is_array( $value ) ) {
925973
continue;
926974
}
927975
$term_wp = get_term_by( 'slug', $value, $key );
928976
$attribute_name = wc_attribute_label( $key, $variation );
929977
if ( ! empty( $product_addons ) ) {
930978
foreach ( $product_addons as $addon ) {
931-
if ( 'file_upload' === $addon['type'] ) {
932-
if ( $key === $addon['name'] ) {
933-
$value = wp_basename( $value );
934-
}
979+
if ( 'file_upload' === $addon['type'] && $key === $addon['name'] ) {
980+
$value = wp_basename( $value );
935981
}
936982
}
937983
}
938984
if ( isset( $term_wp->name ) ) {
939-
echo '<br>' . wp_kses_post( $attribute_name . ':' . $term_wp->name );
985+
echo '<div><strong>' . $attribute_name . ' : </strong>' . $term_wp->name . '</div>'; // phpcs:ignore
940986
} else {
941-
echo '<br>' . wp_kses_post( $attribute_name . ':' . $value );
987+
echo '<div><strong>' . $attribute_name . ' : </strong>' . $value . '</div>'; // phpcs:ignore
942988
}
943989
}
944990
}
945991
} else {
946992
foreach ( $item_meta_fields as $key => $value ) {
947-
if ( ! ( 0 === strpos( $key, '_' ) ) ) {
993+
if ( 0 !== strpos( $key, '_' ) ) {
948994
if ( is_array( $value ) ) {
949995
continue;
950996
}
951997
if ( ! empty( $product_addons ) ) {
952998
foreach ( $product_addons as $addon ) {
953-
if ( 'file_upload' === $addon['type'] ) {
954-
if ( $key === $addon['name'] ) {
955-
$value = wp_basename( $value );
956-
}
999+
if ( 'file_upload' === $addon['type'] && $key === $addon['name'] ) {
1000+
$value = wp_basename( $value );
9571001
}
9581002
}
9591003
}
960-
echo '<br>' . wp_kses_post( $key . ':' . $value );
1004+
echo '<div><strong>' . wc_attribute_label( $key ) . ' : </strong>' . $value . '</div>'; // phpcs:ignore
9611005
}
9621006
}
9631007
}
9641008
} else {
9651009
$item_meta_new = new WC_Order_Item_Meta( $item_meta_fields, $product );
9661010
$item_meta_new->display();
9671011
}
1012+
1013+
// Extras like downloads + custom fields.
9681014
echo '<dl class="extras">';
969-
if ( $product && $product->exists() && $product->is_downloadable() && $order->is_download_permitted() ) :
970-
echo '<dt>';
971-
esc_attr_e( 'Download:', 'woocommerce-delivery-notes' );
972-
echo '</dt>';
973-
echo '<dd>';
974-
// translators: files count.
975-
printf( esc_attr__( '%s Files', 'woocommerce-delivery-notes' ), count( $item->get_item_downloads() ) );
976-
echo '</dd>';
977-
978-
endif;
1015+
if ( $product && $product->exists() && $product->is_downloadable() && $order->is_download_permitted() ) {
1016+
echo '<dt>' . esc_attr__( 'Download:', 'woocommerce-delivery-notes' ) . '</dt>';
1017+
echo '<dd>' . sprintf( esc_attr__( '%s Files', 'woocommerce-delivery-notes' ), count( $item->get_item_downloads() ) ) . '</dd>';// phpcs:ignore
1018+
}
9791019
wcdn_print_extra_fields( $item );
9801020
$fields = apply_filters( 'wcdn_order_item_fields', array(), $product, $order, $item );
981-
foreach ( $fields as $field ) :
982-
echo '<dt>';
983-
echo esc_html( $field['label'] );
984-
echo '</dt>';
985-
echo '<dd>';
986-
echo esc_html( $field['value'] );
987-
echo '</dd>';
988-
endforeach;
1021+
foreach ( $fields as $field ) {
1022+
echo '<dt>' . esc_html( $field['label'] ) . '</dt>';
1023+
echo '<dd>' . esc_html( $field['value'] ) . '</dd>';
1024+
}
9891025
echo '</dl>';
9901026
}
9911027
}

0 commit comments

Comments
 (0)