Forum Replies Created

Viewing 1 replies (of 1 total)
  • Thought I would share the love and post the real solution to this problem.
    The problem manifests when WooCommerce attempts to save the shop_order post type to wp_posts table.
    When someone hits checkout, the woo data store attempts to save the order as a post:

    /wp-content/plugins/woocommerce/includes/data-stores/abstract-wc-order-data-store-cpt.php
    at public function create( &$order ) { line 56

    This is the code that inserts the order:
    $id = wp_insert_post(
    apply_filters(
    ‘woocommerce_new_order_data’,
    array(
    ‘post_date’ => gmdate( ‘Y-m-d H:i:s’, $order->get_date_created( ‘edit’ )->getOffsetTimestamp() ),
    ‘post_date_gmt’ => gmdate( ‘Y-m-d H:i:s’, $order->get_date_created( ‘edit’ )->getTimestamp() ),
    ‘post_type’ => $order->get_type( ‘edit’ ),
    ‘post_status’ => ‘wc-‘ . ( $order->get_status( ‘edit’ ) ? $order->get_status( ‘edit’ ) : apply_filters( ‘woocommerce_default_order_status’, ‘pending’ ) ),
    ‘ping_status’ => ‘closed’,
    ‘post_author’ => 1,
    ‘post_title’ => $this->get_post_title(),
    ‘post_password’ => wc_generate_order_key(),
    ‘post_parent’ => $order->get_parent_id( ‘edit’ ),
    ‘post_excerpt’ => $this->get_post_excerpt( $order ),
    )
    ), true
    );

    Now you will notice that the insert creates a value in post_password field using wc_generate_order_key()
    If you look in your database you will notice that the post_password table is a varchar with max length of 20 chars. I found that wc_generate_order_key() was generating a password LONGER than 20 chars and therefore the insert was failing which manifests further up the chain as “Unable to create order”

    Internally wc_generate_order_key() does this:
    function wc_generate_order_key() {
    return ‘wc_’ . apply_filters( ‘woocommerce_generate_order_key’, ‘order_’ . wp_generate_password( 13, false ) );
    }

    if you notice that wp_generate_password’s first parameter is the length of the password which in this case is 13
    the password is concatenated with “wc_” (length 3) and “order_” (length 6) so 13+3+6 = 22 chars of length

    So this got me thinking why would Woo write this code?
    Well it turns out that my wordpress version on the affected site was 4.7.12 ( we had ignored the updates to v5 as it as a heavy production site and we hadnt yet tested Gutenberg)
    I checked another site that was already at v5 of wordpress and sure enough the post_password field in wp_posts table is varchar(255) so later version of wordpress > 4.7.12 must have increased this field length:

    So the solution is:
    1. Update to latest WP version
    2. If you can update to latest WP Version because you havent tested it yet then go into PhpMyAdmin for your MySql database. You can find your Database name from wp-config.php
    Click on wp_posts or whatever prefix your posts table is using and click on “Structure” tab and modify the length of the post_password field to varchar(255)

    This drove me crazy and I see no solution anywhere on the internet for this.
    Hope this helps someone have a more productive day than I did lol
    ?? ??

Viewing 1 replies (of 1 total)