Skip Navigation

[Resolved] shortcode to get an image url from third party plugin

This support ticket is created 2 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 9 replies, has 2 voices.

Last updated by Rita 2 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#2317479

Hi there

I have a plugin installed that adds a unique QR Code to each Woocommerce order. Each time the plugin calls the code in both the woocommerce 'confirmed' email template and the order template it does it like this:

<img class="orderbarcodes_order_barcode" style="display: block; margin-left: auto; margin-right: auto;" src="<?php echo orderbarcodes_getBarcodeUrl($order); ?>" />

So I am guessing it is this URL that I need to be able to add the QR Code 'image' to toolset order templates:

<?php echo orderbarcodes_getBarcodeUrl($order); ?>

I understand that I cannot display this in toolset content templates or views so I am hoping you can help me to create a little shortcode to get that image url... I am so sorry but I don't know enough php to be able to do this...

Is this something you can help me with?

Is there any other information you need?

#2317497

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Rita,

Thank you for getting in touch.

To wrap this in a shortcode you can simply use the following tool.
hidden link

However I went ahead and generated the shortcode for you.

// Add Shortcode
function func_qr_img() {

	$qr_img = "<img class='orderbarcodes_order_barcode' style='display: block; margin-left: auto; margin-right: auto;' src='".orderbarcodes_getBarcodeUrl($order)."' />";
	return $qr_img;

}
add_shortcode( 'func_qr_img', 'func_qr_img' );

Please let me know if this helps.
Thanks,
Shane

#2318401
Screenshot 2022-03-16 at 11.57.59.png

Hi Shane

Thanks for your help!

I added this shortcode to the woocommerce order template: [func_qr_img]
But... unfortunately, this displays a "There has been a critical error on this website." message as per screenshot attached.

Maybe you need more context? This is the whole php section on 'displaying the code on the order post' in the plugins php file:

/**
* Add the barcode after the order details - web form
*/
function orderbarcodes_add_order_barcode($order) {
if (get_option('plugindistrict_orderbarcodes_myordershide') == '') {
$types = array("code11", "code2of5", "code39", "code49", "code93", "code128", "qrcode");
$barcodetype = (get_option('plugindistrict_orderbarcodes_barcodetype') == '' || !in_array(get_option('plugindistrict_orderbarcodes_barcodetype'), $types, true) ? 'code128' : get_option('plugindistrict_orderbarcodes_barcodetype'));
?>
<div class="orderbarcodes_order_barcode_div" style="text-align: center; margin: 10px;">
<img class="orderbarcodes_order_barcode" style="display: block; margin-left: auto; margin-right: auto;" src="<?php echo orderbarcodes_getBarcodeUrl($order); ?>" />
<p style="font-weight:bold; color:black;"><?php echo (get_option('plugindistrict_orderbarcodes_showtextupc') == 1 && $barcodetype == 'qrcode' ? $order->get_id() : '') ?></p>
</div>
<?php
}
}

Is this useful?

Rita

#2318571

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Rita,

The entire code context would've been helpful yes given that i was under the impression that the function was declared somewhere else.

Try adding the code like this.

/**
* Add the barcode after the order details - web form
*/
function orderbarcodes_add_order_barcode($order) {
if (get_option('plugindistrict_orderbarcodes_myordershide') == '') {
$types = array("code11", "code2of5", "code39", "code49", "code93", "code128", "qrcode");
$barcodetype = (get_option('plugindistrict_orderbarcodes_barcodetype') == '' || !in_array(get_option('plugindistrict_orderbarcodes_barcodetype'), $types, true) ? 'code128' : get_option('plugindistrict_orderbarcodes_barcodetype'));
?>
<div class="orderbarcodes_order_barcode_div" style="text-align: center; margin: 10px;">
<img class="orderbarcodes_order_barcode" style="display: block; margin-left: auto; margin-right: auto;" src="<?php echo orderbarcodes_getBarcodeUrl($order); ?>" />
<p style="font-weight:bold; color:black;"><?php echo (get_option('plugindistrict_orderbarcodes_showtextupc') == 1 && $barcodetype == 'qrcode' ? $order->get_id() : '') ?></p>
</div>
<?php
}
}

// Add Shortcode
function func_qr_img() {
 
    $qr_img = orderbarcodes_add_order_barcode($order);
    return $qr_img;
 
}
add_shortcode( 'func_qr_img', 'func_qr_img' );

Not sure what provides the value for $order but let me know if this helps. If not then you may need to seek assistance from the developers of this code to convert it to a shortcode for you.

Thanks,
Shane

#2318585

Hi Shane
Sorry about that. I should have given you the full code in the first place.
I dropped your code into functions.php but it won't save and explains the error: "Cannot redeclare orderbarcodes_add_order_barcode() (previously declared in wp-content/plugins/pd-orderbarcodes/plugindistrict-orderbarcodes.php:89)"
What do you think?

#2318629

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Rita,

Based on the error, it would appear that you have the function being declared twice on the page.
Remove this section here.

/**
* Add the barcode after the order details - web form
*/
function orderbarcodes_add_order_barcode($order) {
if (get_option('plugindistrict_orderbarcodes_myordershide') == '') {
$types = array("code11", "code2of5", "code39", "code49", "code93", "code128", "qrcode");
$barcodetype = (get_option('plugindistrict_orderbarcodes_barcodetype') == '' || !in_array(get_option('plugindistrict_orderbarcodes_barcodetype'), $types, true) ? 'code128' : get_option('plugindistrict_orderbarcodes_barcodetype'));
?>
<div class="orderbarcodes_order_barcode_div" style="text-align: center; margin: 10px;">
<img class="orderbarcodes_order_barcode" style="display: block; margin-left: auto; margin-right: auto;" src="<?php echo orderbarcodes_getBarcodeUrl($order); ?>" />
<p style="font-weight:bold; color:black;"><?php echo (get_option('plugindistrict_orderbarcodes_showtextupc') == 1 && $barcodetype == 'qrcode' ? $order->get_id() : '') ?></p>
</div>
<?php
}
}

This should clear up the redeclaration error. From there we can see if this solution works.

Thanks,
Shane

#2318705

Ok cool so I removed that first bit from functions.php. Added the shortcode [func_qr_img] to the toolset order post content template and yep it still gives the critical error as per screenshot above... Sorry!

#2318835

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Rita,

I think it might be best to consult with the authors of the plugin for this code as they are better able to construct a shortcode for this. They will know the full context of the code and what are the core functions that are needed for this to work correctly.

Thanks,
Shane

#2319491

Ok thanks Shane. Appreciate your help.
I have emailed the plugin's developers. Hopefully they will be able to advise.
I'll close the ticket now.
Rita

#2319497

Query closed...