Store custom boolean data in $item of order
-
I’ve added a custom product field “Bio”. Bio means “organic” in german.
I followed the documentation at http://www.remicorson.com/mastering-woocommerce-products-custom-fields/
Adding and Saving the “Bio?” boolean checkbox is done with:
// Add Fields add_action( 'woocommerce_product_options_general_product_data', 'fr_add_bio_field' ); function fr_add_bio_field() { global $woocommerce, $post; woocommerce_wp_checkbox( array( 'id' => '_fr_bio_checkbox', 'label' => __( 'Bio?', 'woocommerce' ), 'desc_tip' => 'true', 'description' => __( 'Aktivieren, wenn Bio-Artikel. Wird dann so auf PDF-Rechnung ausgezeichnet.', 'woocommerce' ) ) ); } // Save Fields add_action( 'woocommerce_process_product_meta', 'fr_add_bio_field_save' ); function fr_add_bio_field_save( $post_id ){ $bio_checkbox = $_POST['_fr_bio_checkbox']; if ( !empty( $bio_checkbox) ) { update_post_meta( $post_id, '_fr_bio_checkbox', esc_attr( $bio_checkbox ) ); } else { delete_post_meta( $post_id, '_fr_bio_checkbox' ); } }I can now check whether the “Bio” checkbox is set in the invoice.php with
<?php if (fr_sku_is_bio($item['sku'])) { echo 'Biozertifiziert nach DE-Γko-039'; } ?>and calling:
// Called from invoice.php in woocommerce/pdf/MyCustomModern function fr_sku_is_bio( $sku ) { $product_id = wc_get_product_id_by_sku($sku); return fr_is_bio( $product_id ); } function fr_is_bio( $product_id ) { $bio_checkbox = get_post_meta( $product_id, '_fr_bio_checkbox', true ); if ($bio_checkbox) { return true; } return false; }Problem with this approach is that this always checks against the current product value of “Bio”. But I would need to check against the value that “Bio” was set to, when the order was made. Because the “Organic certificate” was only just allowed to be used and using this code, also the items from orders from the past would be “Bio” (although these are just currently on product level, but are not on order item’s level).
I’ve seen the custom hook wpo_wcpdf_order_item_data.
Does anybody know how I can store custom data (here just a boolean “Bio”) on a order item? Like
$item['tax_class']or$item['qty']is store, but just$item['my_custom_bio']?https://ww.wp.xz.cn/plugins/woocommerce-pdf-invoices-packing-slips/
The topic ‘Store custom boolean data in $item of order’ is closed to new replies.