• Resolved Moobs

    (@nickyb713)


    For the life of me I can’t figure out why these attributes are set & saved correctly according to the error log checks, but not actually added to the product or postmeta. Both WP & WC are updated to the latest versions.

    For reference all the attributes are custom product attributes and the product does not have any attributes to start with:

    error_log('Before setting attributes: ' . print_r($parent_product->get_attributes(), true));
        $parent_product->set_attributes($parent_attributes);
    
        error_log('After setting attributes: ' . print_r($parent_product->get_attributes(), true));
    
        $save_result = $parent_product->save();
    
        error_log('Save result: ' . $save_result);

    Output:

    [10-Nov-2023 11:37:24 UTC] Before setting attributes: Array
    (
    )
    
    [10-Nov-2023 11:37:24 UTC] After setting attributes: Array
    (
        [fit] => WC_Product_Attribute Object
            (
                [data:protected] => Array
                    (
                        [id] => 0
                        [name] => fit
                        [options] => Array
                            (
                                [0] => Male Fit
                                [1] => Female Fit
                            )
    
                        [position] => 0
                        [visible] => 1
                        [variation] => 1
                    )
    
            )
    
        [colors] => WC_Product_Attribute Object
            (
                [data:protected] => Array
                    (
                        [id] => 0
                        [name] => colors
                        [options] => Array
                            (
                                [0] => White
                                [1] => Black
                                [2] => Dark Chocolate
                                [3] => Heather Royal
                                [4] => Heather Red
                            )
    
                        [position] => 1
                        [visible] => 1
                        [variation] => 1
                    )
    
            )
    
        [sizes] => WC_Product_Attribute Object
            (
                [data:protected] => Array
                    (
                        [id] => 0
                        [name] => sizes
                        [options] => Array
                            (
                                [0] => S
                                [1] => M
                                [2] => L
                                [3] => XL
                                [4] => 2XL
                                [5] => 3XL
                            )
    
                        [position] => 2
                        [visible] => 1
                        [variation] => 1
                    )
    
            )
    
        [pid] => WC_Product_Attribute Object
            (
                [data:protected] => Array
                    (
                        [id] => 0
                        [name] => pid
                        [options] => Array
                            (
                                [0] => 3524
                                [1] => 3586
                            )
    
                        [position] => 3
                        [visible] => 1
                        [variation] => 1
                    )
    
            )
    
    )
    
    [10-Nov-2023 11:37:25 UTC] Save result: 2221

    The attribute arrays appear to be structured properly and it shows as saving to the correct product.

    • This topic was modified 1 year, 8 months ago by Moobs.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Moobs

    (@nickyb713)

    I’m still trying to resolve this issue, but I have ruled out conflicts with other plugins & theme by disabling all other plugins and testing in storefront. So strange that the save is returning the order ID which is a successful response, but the custom attributes are not being added in the DB.

    $new_attribute = new WC_Product_Attribute();
    $new_attribute->set_name($attribute_name);
    $new_attribute->set_options($options);
    $new_attribute->set_visible($visible);
    $new_attribute->set_position($position);
    $new_attribute->set_variation($variation);
    $new_attribute->set_id(0); // Explicitly setting ID to 0 for custom attribute
    $parent_attributes[$attribute_name] = $new_attribute;
    
    error_log('Before setting attributes: ' . print_r($parent_product->get_attributes(), true));
    
        $parent_product->set_attributes($parent_attributes);
        $save_result = $parent_product->save();
    
        error_log('After setting attributes: ' . print_r($parent_product->get_attributes(), true));
        error_log('Save result: ' . $save_result);

    Output:

    [11-Nov-2023 01:24:41 UTC] Before setting attributes: Array
    (
    )
    
    [11-Nov-2023 01:24:41 UTC] After setting attributes: Array
    (
        [fit] => WC_Product_Attribute Object
            (
                [data:protected] => Array
                    (
                        [id] => 0
                        [name] => fit
                        [options] => Array
                            (
                                [0] => Male Fit
                                [1] => Female Fit
                            )
    
                        [position] => 0
                        [visible] => 1
                        [variation] => 1
                    )
    
            )
    
        [colors] => WC_Product_Attribute Object
            (
                [data:protected] => Array
                    (
                        [id] => 0
                        [name] => colors
                        [options] => Array
                            (
                                [0] => White
                                [1] => Black
                                [2] => Dark Chocolate
                                [3] => Heather Royal
                                [4] => Heather Red
                            )
    
                        [position] => 1
                        [visible] => 1
                        [variation] => 1
                    )
    
            )
    
        [sizes] => WC_Product_Attribute Object
            (
                [data:protected] => Array
                    (
                        [id] => 0
                        [name] => sizes
                        [options] => Array
                            (
                                [0] => S
                                [1] => M
                                [2] => L
                                [3] => XL
                                [4] => 2XL
                                [5] => 3XL
                            )
    
                        [position] => 2
                        [visible] => 1
                        [variation] => 1
                    )
    
            )
    
        [pid] => WC_Product_Attribute Object
            (
                [data:protected] => Array
                    (
                        [id] => 0
                        [name] => pid
                        [options] => Array
                            (
                                [0] => 3524
                                [1] => 3586
                            )
    
                        [position] => 3
                        [visible] => 1
                        [variation] => 1
                    )
    
            )
    
    )
    
    [11-Nov-2023 01:24:41 UTC] Save result: 2221
    Plugin Support Shameem – a11n

    (@shameemreza)

    Hi @nickyb713

    Based on the information you shared, it looks like that the attributes are being set correctly on the product object, but they are not being saved in the database.

    This could be due to a few reasons. One possibility is that there might be a database error preventing the save operation. Please check your server’s error logs for any related messages.

    Another possibility is that the data is not being saved because it’s not passing the validation in the WooCommerce core. WooCommerce has some built-in validation rules for product data, and if the data does not pass these rules, it will not be saved.

    You could also try calling $parent_product->get_data_store()->update($parent_product); directly after $parent_product->save(); to force the data store to update immediately.

    If these suggestions don’t resolve the issue, we recommend asking development questions on the #developers channel of the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question.

    I wish I could help more, but hopefully, this gets you going in the right direction to get the job done.

    Did you find a solution to that? I see the same problem.

    I just want to change the set_variation(false) on the attributes fetched by

    $product->get_attributes();

    So all I do is iterating through the array, calling set_variation(false) and then running:

    $product->save();

    I don’t change anything else. But the post metadata for the meta_key ” _product_attributes” is not updated.

    And I assume using this methods is the correct way, not directly editing _product_attributes using update_post_meta.

    • This reply was modified 1 year, 7 months ago by lordandy1984.
    Thread Starter Moobs

    (@nickyb713)

    Unfortunately, I did not find the issue, though I believe it was somehow related to the data being saved. Not quite sure how it wasn’t sanitized or formatted correctly, especially with even reconstructing all the strings at one point.

    I was importing products from a 3rd party provider and wasn’t too keen on their integration to begin with. Ended up spending a few days creating my own plugin to handle all the product management with this vendor and it’s worked out really well. I’m still using the same method in the new plugin and it works as expected.

    Hi @nickyb713

    This is a bit of a complicated topic that would need some customization to address. Unfortunately, custom coding is not something we can assist with directly. However, I’ll keep this thread open for a bit to see if anyone from the community can lend a hand.

    If you have any other questions related to development or custom coding, don’t hesitate to reach out to some of the great resources we have available for support. The WooCommerce community is filled with talented open-source developers, and many of them are active on the channels listed below:

    Hope this helps!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Attributes Not Added To Product After Successful Save’ is closed to new replies.