admin管理员组文章数量:1418621
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 Answer
Reset to default 1You 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.
本文标签: phpDisplay specific WooCommerce order formatted metadata if it existsStack Overflow
版权声明:本文标题:php - Display specific WooCommerce order formatted metadata if it exists - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745296874a2652131.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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 anif
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:24if
statement toif ($template_type == 'packing-slip' && $order->get_meta( 'is_gift' ))
? – Barmar Commented Jan 29 at 17:56