• HS Code attribute is never created, wc_create_attribute() called with the output column names instead of its input arguments (endless error loop)

    Plugin version: 3.2.4 · WordPress 7.0.2 · WooCommerce 11.0.0 · PHP 8.3

    Hi,

    After enabling WP_DEBUG_LOG on a production store, I found the plugin writing this line to the debug log on every single page load, 96 entries in 22 minutes on my install:

    [bpost] Failed to create HS Code product attribute: Please, provide an attribute name.

    Cause

    init_hs_code_attribute() in includes/class-woo-Bpost.php (array built at line 1573, call at line 1581) passes wc_create_attribute() the database column names rather than the arguments the function actually takes:

    $attribute_data = array(
        'attribute_label'   => $attribute_label,
        'attribute_name'    => $attribute_name,
        'attribute_type'    => 'text',
        'attribute_orderby' => 'menu_order',
        'attribute_public'  => 0,
    );
    $attribute_id = wc_create_attribute($attribute_data);

    Those five keys are exactly what wc_create_attribute() produces, not what it consumes. It builds them itself, near the end of the function, from a different set of names:

    $data = array(
        'attribute_label'   => $args['name'],
        'attribute_name'    => $slug,
        'attribute_type'    => $args['type'],
        'attribute_orderby' => $args['order_by'],
        'attribute_public'  => isset( $args['has_archives'] ) ? (int) $args['has_archives'] : 0,
    );

    So the expected arguments are name, slug, type, order_by and has_archives. Since $args['name'] is never set, the function returns on its very first check:

    if ( empty( $args['name'] ) ) {
        return new WP_Error( 'missing_attribute_name',
            __( 'Please, provide an attribute name.', 'woocommerce' ),
            array( 'status' => 400 ) );
    }

    Reproduced as-is on my install, passing your exact array:

    WP_Error  code=missing_attribute_name  message=Please, provide an attribute name.

    The same call with the correct keys creates the attribute without error.

    Why it repeats indefinitely, init_hs_code_attribute() is hooked to init (line 291), unconditionally, no
    setting gates it. It checks whether the attribute exists, fails to create it, logs the error, and since the attribute is still missing on the next request, it tries again. One failed attempt and one log write per request, forever. Impact

    The HS Code attribute never exists, so get_hs_code_from_attribute() can never return a value and get_product_hs_code() always falls through to the _hs_code meta. No stray database rows are created, since WooCommerce returns the error before any write.

    The practical problem is log flooding, which makes WP_DEBUG_LOG unusable for anything else, precisely when you need it, for instance while watching a WooCommerce major upgrade.

    Suggested fix

    $attribute_data = array(
        'name'         => $attribute_label,
        'slug'         => $attribute_name,
        'type'         => 'select',
        'order_by'     => 'menu_order',
        'has_archives' => false,
    );

    Worth noting while you are in there: text is not a valid attribute type. wc_get_attribute_types() only ever declares select — which WooCommerce now labels “Text” in the UI — plus wc-visual under certain conditions. Anything else is silently coerced to select, so passing select directly is clearer. Workaround for other users

    Creating the attribute by hand satisfies the existence check and stops the loop. The slug must be exactly hs_code:

    Products → Attributes → Name “HS Code”, Slug hs_code

    Verified on my install: not a single new log entry afterwards, over roughly thirty requests, and still silent after upgrading to WooCommerce 11.0.

    Unrelated, but while you’re at it

    The plugin header still declares WC tested up to: 6.9.0, several majors behind.

    Thanks!

Viewing 1 replies (of 1 total)
  • wikafi

    (@wikafi)

    I’m getting the same error on my end, and it’s preventing me from creating a new page.

Viewing 1 replies (of 1 total)

You must be logged in to reply to this topic.