[wcj_order_meta]
Display any WooCommerce order's meta
Description
[wcj_order_meta]
shortcode displays any WooCommerce order’s meta by key name. For example you can use it in Booster’s PDF invoice template to display any (even third party plugin’s) meta.
Args
- meta_key
- (required) Meta key.
- Default: None
- before
- (optional) Text to place before the content. If content is empty this argument is ignored.
- Default: None
- after
- (optional) Text to place after the content. If content is empty this argument is ignored.
- Default: None
Example
[wcj_order_meta meta_key="_your_key" before="My key: "]
Accessible through:
- [wcj_order_meta]
Hello Cecil
I am having the same situation and would love to see the php code you used to accomplish adding Yes and No.
Would you be so kind as to share?
Thanks
Bill
I’m not a PHP programmer, but I was able to kludge this code together after some research. I’m sure a real programmer could suggest something better…but it works. You put the newly created short code where you want the Yes/No answer to appear. These are the two snippets of PHP I have added to the functions.php file in my child theme folder. Always create a child theme before editing your functions.php file.
// Display Yes or No for Booster single checkbox field – Pay Processing Fee
add_shortcode(‘booster_pay_fee’, ‘generate_pay_fee’);
function generate_pay_fee($atts) {
// Get the shortcode value and do the transform
$checkbox_value = do_shortcode(‘[wcj_order_meta meta_key="_order_wcj_checkout_field_2"]‘);
if ( $checkbox_value == 0 ) {
$output = “No”;
}
else {
$output = “Yes”;
}
return $output;
}
// Display Yes or No for Booster single checkbox field – Anonymous Donation
add_shortcode(‘booster_anon’, ‘generate_anon’);
function generate_anon($atts) {
// Get the shortcode value and do the transform
$checkbox_value = do_shortcode(‘[wcj_order_meta meta_key="_order_wcj_checkout_field_1"]‘);
if ( $checkbox_value == 0 ) {
$output = “No”;
}
else {
$output = “Yes”;
}
return $output;
}
Hi Cecil
Thanks for posting this code. I am just now getting back to this issue.
I inserted the code into the functions.php (and customized the names of the fields to fit my application), but then when I attempt to load the PDF of the invoice where the Yes/No should output, I get a ‘This page isn’t working’ error.
Did you add one of the newly created shortcodes to the template file as well?
I had added this code to my invoice template and it outputs the custom fields but not the answer.
custom_field(‘_billing_wcj_checkout_field_1’); ?>
custom_field(‘_billing_wcj_checkout_field_2’); ?>
I altered your code to this:
// Display Yes or No for Booster single checkbox field – Sign up for Newsletter
add_shortcode(‘booster_newsletter’, ‘generate_newsletter’);
function generate_newsletter($atts) {
// Get the shortcode value and do the transform
$checkbox_value = do_shortcode(‘[wcj_billing_meta meta_key=”_billing_wcj_checkout_field_2″]’);
if ( $checkbox_value == 0 ) {
$output = “No”
}
else {
$output = “Yes”
}
return $output;
}
// Display Yes or No for Booster single checkbox field – Is this a gift
add_shortcode(‘booster_gift’, ‘generate_gift’);
function generate_gift($atts) {
// Get the shortcode value and do the transform
$checkbox_value = do_shortcode(‘[wcj_billing_meta meta_key=”_billing_wcj_checkout_field_1″]’);
if ( $checkbox_value == 0 ) {
$output = “No”
}
else {
$output = “Yes”
}
return $output;
}
Medman,
I’ve only used the output from the php code above in one place -on custom thank you pages created with “Woocommerce Thank You Page – Nextmove”. To show the results, I have inserted the shortcodes I created with the php code with passed parameters as shown below:
[booster_pay_fee meta_id=”_order_wcj_checkout_field_2″]
[booster_anon meta_id=”_order_wcj_checkout_field_1″]
There is probably a method to insert the shortcodes into the pdf template file.
Hope this helps.
When using this shortcode with a Single checkbox field, it returns a value of “1” or “2” rather than the expected “Yes” or “No”.
Hi,
Values should be “0” or “1” but not “2”. This is because checkbox meta is saved as a boolean type which can only be those 2 values. Unfortunately it is not possible to change that yet.
Best regards,
Rokas – Support Team
Sorry about the typo on 2 rather than 0.
I was able to use some basic PHP to create a custom shortcode for the two checkbox fields I added to output “Yes” or “No” depending on the value your shortcode returned.
The meta shows the contents but HTML’s get filtered out.