Pridanie dát do rýchleho náhľadu objednávky / order preview

Pridanie vlastných dát do náhľadu / Display custom data on Woocommerce admin order preview

// Add custom order meta data to make it accessible in Order preview template
add_filter( 'woocommerce_admin_order_preview_get_order_details', 'admin_order_preview_add_custom_meta_data', 10, 2 );
function admin_order_preview_add_custom_meta_data( $data, $order ) {
    // Replace '_custom_meta_key' by the correct postmeta key
    if( $custom_value = $order->get_meta('_custom_meta_key') )
        $data['custom_key'] = $custom_value; // <= Store the value in the data array.

    return $data;
}

// Display custom values in Order preview
add_action( 'woocommerce_admin_order_preview_end', 'custom_display_order_data_in_admin' );
function custom_display_order_data_in_admin(){
    // Call the stored value and display it
    echo '<div>Value: {{data.custom_key}}</div><br>';
}

Pridá poznámky z objednávky do náhľadu

// Add order notes so you can accees in `woocommerce_admin_order_preview_start`
add_filter( 'woocommerce_admin_order_preview_get_order_details', 'admin_order_preview_add_order_notes_data', 10, 2 );
function admin_order_preview_add_order_notes_data( $data, $order ) {
        
    $notes = wc_get_order_notes([
        'order_id' => $order->get_id(),
    ]);

    ob_start();

    ?>
    <div class="wc-order-preview-order-note-container" style="padding: 20px;">
        <div class="wc-order-preview-custom-note">
        <h2 class="order-note">Order Note:</h2>
            <ul class="order_notes">
                <?php
                if ( $notes ) {
                    foreach ( $notes as $note ) {
                        $css_class   = array( 'note' );
                        $css_class[] = $note->customer_note ? 'customer-note' : '';
                        $css_class[] = 'system' === $note->added_by ? 'system-note' : '';
                        $css_class   = apply_filters( 'woocommerce_order_note_class', array_filter( $css_class ), $note );
                        ?>
                        <li rel="<?php echo absint( $note->id ); ?>" class="<?php echo esc_attr( implode( ' ', $css_class ) ); ?>">
                            <div class="note_content">
                                <?php echo wpautop( wptexturize( wp_kses_post( $note->content ) ) ); // @codingStandardsIgnoreLine ?>
                            </div>
                            <p class="meta">
                                <abbr class="exact-date" title="<?php echo esc_attr( $note->date_created->date( 'Y-m-d H:i:s' ) ); ?>">
                                    <?php
                                    
                                    echo esc_html( sprintf( __( '%1$s at %2$s', 'woocommerce' ), $note->date_created->date_i18n( wc_date_format() ), $note->date_created->date_i18n( wc_time_format() ) ) );
                                    ?>
                                </abbr>
                                <?php
                                if ( 'system' !== $note->added_by ) :
                                    
                                    echo esc_html( sprintf( ' ' . __( 'by %s', 'woocommerce' ), $note->added_by ) );
                                endif;
                                ?>
                                <a href="#" class="delete_note" role="button"><?php esc_html_e( 'Delete note', 'woocommerce' ); ?></a>
                            </p>
                        </li>
                        <?php
                    }
                } else {
                    ?>
                    <li class="no-items"><?php esc_html_e( 'There are no notes yet.', 'woocommerce' ); ?></li>
                    <?php
                }
                ?>
            </ul>
        </div>
    </div>
    <?php

    $order_notes = ob_get_clean();  

    $data['order_notes'] = $order_notes;

    return $data;

}

//order notes
add_action( 'woocommerce_admin_order_preview_start', 'woocommerce_admin_order_preview_order_notes' );
function woocommerce_admin_order_preview_order_notes() {
    ?> {{{data.order_notes}}} <?php
}

Pridaj komentár

Vaša e-mailová adresa nebude zverejnená.