Making two variable equal

I’m trying to change the random barcode generated in a plugin to reflect the relevant order number ( order_id ) and not generate a unique number.
The current code that sets the variable ($barcode_string) looks like this:

// Get unqiue barcode string
$barcode_string = $this->get_barcode_string();

And then generates a unique number for it here:

/**
 * Get text string for barcode
 * @access  public
 * @since   1.0.0
 * @return  void
 */
public function get_barcode_string () {

	// Use PHP's uniqid() for the barcode
	$barcode_string = uniqid();

	// Check if this barcode already exists and add increment if so
	$existing_order_id = $this->get_barcode_order( $barcode_string );
	$orig_string = $barcode_string;
	$i = 1;
	while( $existing_order_id != 0 ) {
		$barcode_string = $orig_string . $i;
		$existing_order_id = $this->get_barcode_order( $barcode_string );
		++$i;
	}

	// Return unique barcode
	return apply_filters( $this->_token . '_barcode_string', $barcode_string );

} // End get_barcode_string ()

So, I imagine that I need to make the variable “$barcode_string” equal to “order_id”. But I’m battling to find a simple guide on doing that.

Any help please?

If you want a specific barcode generated or compared, should you pass that value in?

The value that I’m trying to pass to “$barcode_string” is “order_id” (generated by woocommerce). The original barcode plugin works well in that it inserts the barcode in the user order and email, but I need it to show the value of the order_id.

The only code you showed was the get_barcode_string and that generates a new code every time it is called.

Okay, if I could beg your patience please and let me explain in greater detail:

I need a barcode to display on the order confirmation page in Woocommerce. But the number on the barcode is a generated number from the plugin, and not the order number. Also, Thailand have a new regulation where all barcodes used for payment have to comply with bank standard (13 digit Tax Id, 2 digit Product ID, 18 digit Order Number, 18 digit Expiry Date of Order and 10 digit Amount Payable)

I was working with the code from the Woocommerce Order Barcode plugin (https://cida.site/serv//woocommerce-order-barcodes/), where I was trying to make the value of barcode string equal to order_id.
But in the light of the new Thai regulation, I think it might be simpler to create new code using picqer/php-barcode-generator.

I found this bit of code. and while it seems that it’s going to do the job, I need some help/pointers in getting the setup and placed in the right file.

<?php
include "src/BarcodeGenerator.php";
include('src/BarcodeGeneratorPNG.php');
 
$code = [
    "|012345678901200\n123456789012345678\n123456789012345678\n0",
    "|012345678901200\n123456789012345678\n123456789012345678\n0",
];
 
$i = 1;
 
$generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG();
 
foreach($code as $barcode){
 
    echo '<p style="font-size:14px;">';
    echo '<img width="380" height="35" src="data:image/png;base64,' . base64_encode($generatorPNG->getBarcode($barcode, $generatorPNG::TYPE_CODE_128)) . '">';
    echo '<br>';
    echo $barcode."</p>";
    
}
 
?>

Truly appreciate any help.
correctshop.online@gmail.com

I’m trying a different solution now. I modified the bit of code in the barcode plugin to include all the parameters that I need in the ‘barcode_string’. Defining those variables manually, using the code below, the barcode generator works perfectly and displays correctly on the completed order:

/**
 * Get text string for barcode
 * @access  public
 * @since   1.0.0
 * @return  void
 */
public function get_barcode_string () {

	# Define Variables for barcode_string

	// Constant - pipe
	$_pipe = '|';

	// Constant - Tax Number
	$_taxnum = '0994000160208';

	// Woocommerce product ID
	$_product_id = '02';

	// Woocommerce order number
	$_order_id = '196';

	// Order expiry date
	$_exp_date = '25062004';

	// Total value of order
	$_order_total = '110';

	// Concatenate variables in barcode_string
	$barcode_string = $_pipe . ' ' . $_taxnum . ' ' . $_product_id . ' ' . $_order_id . ' ' . $_exp_date . ' ' . $_order_total . '00';

	// Return barcode
	return apply_filters( $this->_token . '_barcode_string', $barcode_string );

} // End get_barcode_string ()

But when I try to ‘get’ the appropriate data for those variables it fails. This is the code that I trying to use:

/**
 * Get text string for barcode
 * @access  public
 * @since   1.0.0
 * @return  void
 */
public function get_barcode_string () {

	# Define Variables for barcode_string

	// Constant - pipe
	$_pipe = '|';

	// Constant - Tax Number
	$_taxnum = '0994000160208';

	// Woocommerce product ID
	$_product_id = $order->get_product_id();

	// Woocommerce order number
	$_order_id = $order->get_id();

	// Order date
	$_date = $order->get_post_date();

	// Total value of order
	$_order_total = $order->get_total();

	// Concatenate variables in barcode_string
	$barcode_string = $_pipe . ' ' . $_taxnum . ' ' . $_product_id . ' ' . $_order_id . ' ' . $_date . ' ' . $_order_total . '00';

	// Return barcode
	return apply_filters( $this->_token . '_barcode_string', $barcode_string );

} // End get_barcode_string ()

That’s because your error reporting is disabled. You should always have the maximum error reporting level on your development machine

error_reporting(-1);
ini_set('display_errors', true);

then you would get a definitive error message like:

"undefined variable $order; error: call to a member function foo on non-object"

read: https://www.php.net/manual/en/language.variables.scope.php

and global is NOT the answer, but this:

https://www.php.net/manual/en/functions.arguments.php

That’s very helpful. Thank you. I will read up along those guidelines.

Sponsor our Newsletter | Privacy Policy | Terms of Service