Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Broken multi-level (nested) associative array values after getting submitted from checkout page #307

Merged

Conversation

ratnakardubey
Copy link
Contributor

Issue: #306


Description

Instead of processing the submitted values using explode used a PHP function parse_str which can handle multi-level (nested) associative arrays.

Steps to test:

  1. Copy & paste the following block of code at the end of the file functions.php of child theme of your website
/**
 * Add some HTML input fields on the checkout page
 *
 * @return void
 */
function ratnakar_custom_checkout_array_fields() {
	for ( $i = 0; $i < 3; $i++ ) {
		for ( $j = 0; $j < 2; $j++ ) {
			?>
			<input type="hidden" name="ratnakar_input[field_<?php echo esc_attr( $i ); ?>][]" value="ratnakar_input_value_<?php echo esc_attr( $i . '_' . $j ); ?>">
			<?php
		}
	}
}
add_action( 'woocommerce_checkout_after_customer_details', 'ratnakar_custom_checkout_array_fields' );

/**
 * Process the submitted values on the checkout page
 *
 * @return void
 */
function ratnakar_custom_checkout_processed_array_fields() {
	$logger  = wc_get_logger();
	$context = array( 'source' => 'ratnakar' );
	ob_start();
	var_dump( $_REQUEST );
	$logger->log( 'debug', 'ratnakar_input: ' . ob_get_clean() . ' ' . __FILE__ . ' ' . __LINE__, $context );
}
add_action( 'woocommerce_new_order', 'ratnakar_custom_checkout_processed_array_fields' );
  1. Save the functions.php file
  2. Now, go to the website, add a product to cart, navigate to the checkout page
  3. On the checkout page, select PayPal Payments & click on the yellow button of the PayPal
  4. Go to WooCommerce > Status > Logs
  5. Look in the dropdown menu for log file name starting with 'ratnakar'
  6. Click on the View button to open the log
  7. Go through the submitted form values, especially for the field ratnakar_input. You'll notice the square box that represents an array has become the part of the array key as string

Result

After applying this fix, the submitted form values will be formatted correctly, as shown in the attached screenshot

Screenshot from 2021-10-05 12-53-55

Comparison in the results before & after applying the fix

Screenshot from 2021-10-05 14-05-58

Documentation

Don't think that this PR will require any change in the documentation

Changelog entry

Fix: Broken multi-level (nested) associative array values after getting submitted from checkout page

Closes #306

@Dinamiko
Copy link
Contributor

Hello @ratnakardubey,

Thanks for the PR, I´ve checked and it looks good. When you have time can you please rename the file to CreateOrderEndpoint.php? (we have just changed class names to follow PSR-4). Once renamed we´ll merge this PR to trunk.

@Dinamiko Dinamiko self-requested a review October 13, 2021 10:00
Copy link
Contributor

@Dinamiko Dinamiko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename file from class-createorderendpoint.php to CreateOrderEndpoint.php.

@ratnakardubey
Copy link
Contributor Author

Hey @Dinamiko,

Thank you for your feedback.

I've pushed a new commit in the same PR as you suggested.

@Dinamiko Dinamiko self-requested a review October 13, 2021 10:37
@Dinamiko Dinamiko merged commit 686f691 into woocommerce:trunk Oct 13, 2021
@isabellainpsyde isabellainpsyde added this to the Next Version milestone Oct 28, 2021
@Dinamiko Dinamiko mentioned this pull request Nov 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Breaking multi-level (nested) associative array values that are submitted on the checkout page (330)
3 participants