最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Display specific WooCommerce order formatted metadata if it exists - Stack Overflow

programmeradmin1浏览0评论

I keep going round in circles with my code, can anyone point me in the right direction please?

I've created a custom field for the order details page so the site admin can add a note to be printed on packing slips.

I need the 'div class="delivery-date"' section at the end of the below code to only appear IF the gift_message field is populated.

At the moment, it just displays an empty section.

add_action( 'woocommerce_admin_order_data_after_order_details', 'misha_editable_order_meta_general' );

function misha_editable_order_meta_general( $order ){

    ?>
        <br class="clear" />
        <h3>Admin note <a href="#" class="edit_address">Edit</a></h3>
        <?php
            $is_gift = $order->get_meta( 'is_gift' );
            $gift_message = $order->get_meta( 'gift_message' );
        ?>
        <div class="address">
            <p><strong>Add admin note?</strong><?php echo $is_gift ? 'Yes' : 'No' ?></p>
            <?php
                // we show the rest fields in this column only if this order is marked as a gift
                if( $is_gift ) :
                ?>
                    <p><strong>Admin note:</strong> <?php echo wpautop( esc_html( $gift_message ) ) ?></p>
                <?php
                endif;
            ?>
        </div>
        <div class="edit_address">
            <?php

                woocommerce_wp_radio( array(
                    'id' => 'is_gift',
                    'label' => 'Add admin note?',
                    'value' => $is_gift,
                    'options' => array(
                        '' => 'No',
                        '1' => 'Yes'
                    ),
                    'style' => 'width:16px', // required for checkboxes and radio buttons
                    'wrapper_class' => 'form-field-wide' // always add this class
                ) );

                woocommerce_wp_textarea_input( array(
                    'id' => 'gift_message',
                    'label' => 'Admin note:',
                    'value' => $gift_message,
                    'wrapper_class' => 'form-field-wide'
                ) );

            ?>
        </div>
    <?php 
}

add_action( 'woocommerce_process_shop_order_meta', 'misha_save_general_details' );

function misha_save_general_details( $order_id ){
    
    $order = wc_get_order( $order_id );
    $order->update_meta_data( 'is_gift', wc_clean( $_POST[ 'is_gift' ] ) );
    $order->update_meta_data( 'gift_message', wc_sanitize_textarea( $_POST[ 'gift_message' ] ) );
    // wc_clean() and wc_sanitize_textarea() are WooCommerce sanitization functions
    $order->save();
    
}

add_action( 'wpo_wcpdf_after_customer_notes', 'wpo_wcpdf_delivery_date', 10, 2 );
function wpo_wcpdf_delivery_date ($template_type, $order) {
    if ($template_type == 'packing-slip') {
        ?>
        <div class="delivery-date" style="border: 1px dashed;padding: 8px 10px 10px 10px;margin-top: 5px;min-width: 200px;">
            <div><strong>Admin note</strong></div>
            <div><?php echo $order->get_meta('gift_message'); ?></div>
        </div>
        <?php
    }
}

I keep going round in circles with my code, can anyone point me in the right direction please?

I've created a custom field for the order details page so the site admin can add a note to be printed on packing slips.

I need the 'div class="delivery-date"' section at the end of the below code to only appear IF the gift_message field is populated.

At the moment, it just displays an empty section.

add_action( 'woocommerce_admin_order_data_after_order_details', 'misha_editable_order_meta_general' );

function misha_editable_order_meta_general( $order ){

    ?>
        <br class="clear" />
        <h3>Admin note <a href="#" class="edit_address">Edit</a></h3>
        <?php
            $is_gift = $order->get_meta( 'is_gift' );
            $gift_message = $order->get_meta( 'gift_message' );
        ?>
        <div class="address">
            <p><strong>Add admin note?</strong><?php echo $is_gift ? 'Yes' : 'No' ?></p>
            <?php
                // we show the rest fields in this column only if this order is marked as a gift
                if( $is_gift ) :
                ?>
                    <p><strong>Admin note:</strong> <?php echo wpautop( esc_html( $gift_message ) ) ?></p>
                <?php
                endif;
            ?>
        </div>
        <div class="edit_address">
            <?php

                woocommerce_wp_radio( array(
                    'id' => 'is_gift',
                    'label' => 'Add admin note?',
                    'value' => $is_gift,
                    'options' => array(
                        '' => 'No',
                        '1' => 'Yes'
                    ),
                    'style' => 'width:16px', // required for checkboxes and radio buttons
                    'wrapper_class' => 'form-field-wide' // always add this class
                ) );

                woocommerce_wp_textarea_input( array(
                    'id' => 'gift_message',
                    'label' => 'Admin note:',
                    'value' => $gift_message,
                    'wrapper_class' => 'form-field-wide'
                ) );

            ?>
        </div>
    <?php 
}

add_action( 'woocommerce_process_shop_order_meta', 'misha_save_general_details' );

function misha_save_general_details( $order_id ){
    
    $order = wc_get_order( $order_id );
    $order->update_meta_data( 'is_gift', wc_clean( $_POST[ 'is_gift' ] ) );
    $order->update_meta_data( 'gift_message', wc_sanitize_textarea( $_POST[ 'gift_message' ] ) );
    // wc_clean() and wc_sanitize_textarea() are WooCommerce sanitization functions
    $order->save();
    
}

add_action( 'wpo_wcpdf_after_customer_notes', 'wpo_wcpdf_delivery_date', 10, 2 );
function wpo_wcpdf_delivery_date ($template_type, $order) {
    if ($template_type == 'packing-slip') {
        ?>
        <div class="delivery-date" style="border: 1px dashed;padding: 8px 10px 10px 10px;margin-top: 5px;min-width: 200px;">
            <div><strong>Admin note</strong></div>
            <div><?php echo $order->get_meta('gift_message'); ?></div>
        </div>
        <?php
    }
}
Share Improve this question edited Jan 30 at 21:42 LoicTheAztec 255k24 gold badges399 silver badges446 bronze badges asked Jan 29 at 12:54 adem007adem007 1214 bronze badges 3
  • 1 So what have you tried? There seems to be nothing coded that would do anything like what you want to do?? Although what you want to do does not seem particularly complicated – RiggsFolly Commented Jan 29 at 13:04
  • I need the 'div class="delivery-date"' section at the end of the below code to only appear IF the gift_message field is populated...ok, so where exactly are you stuck with that? Have you tried an if statement? What was the issue precisely? It sounds straightforward, so was there some unanticipated complication that we need to know about? – ADyson Commented Jan 29 at 13:24
  • Can't you just change the if statement to if ($template_type == 'packing-slip' && $order->get_meta( 'is_gift' ))? – Barmar Commented Jan 29 at 17:56
Add a comment  | 

1 Answer 1

Reset to default 1

You simply need to check in an if statement that the gift message exists (is not empty).

Try the following revised code:

add_action( 'wpo_wcpdf_after_customer_notes', 'wpo_wcpdf_delivery_date', 10, 2 );

function wpo_wcpdf_delivery_date($document_type, $order) {
    if ( 'packing-slip' != $document_type ) return; // Target only Packing slip
    
    // Check that the gift message exists (is not empty)
    if ( $gift_message = $order->get_meta('gift_message') ) {
        printf('<div class="delivery-date" style="border:1px dashed;padding:8px 10px 10px 10px;margin-top:5px;min-width:200px;">
            <div><strong>%s</strong></div>
            <div>%s</div>
        </div>', esc_html__('Admin note'), $gift_message );
    }
}

it should work.

发布评论

评论列表(0)

  1. 暂无评论