• Resolved Carl Brubaker

    (@imconfused4sure)


    We have a product variation in Square with the variation names listed as “Case” which is the default and “6 Pack”. When it is imported into WooCommerce the resulting attribute stored in the database is “Case | Case – 6 Pack” which then displays the same options on the front end “Case” and “Case – 6 Pack”. This is confusing to users. Is this considered normal? Is there something different we should do?

    When you view this page it may be displayed correctly, as I am using a filter after product sync save to remove the redundancies. This will however double the hits on the database and I would like to avoid that.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support 3 Sons Development – a11n

    (@3sonsdevelopment)

    Hey @imconfused4sure,

    Thanks for reaching out about this. Has the site ever had WooCommerce as the system of record? I believe I’ve seen this happen on my own test site in the past when I switched between WooCommerce and Square as the system of record.

    Let us know and we’ll go from there.

    Thanks!

    Thread Starter Carl Brubaker

    (@imconfused4sure)

    @3sonsdevelopment Sorry, I forgot to post back. It’s the way Square saves them. Here was my solution:

    add_action(
        'woocommerce_square_create_product_data',
        [$this, 'action_remove_redundant_attribute_name_prefix']
    );
    
    public function action_remove_redundant_attribute_name_prefix($data)
    {
        if (!array_key_exists('type', $data) || $data['type'] !== 'variable') {
            return $data;
        }
    
        $old_options = [];
        $new_options = [];
    
        foreach ($data['variations'] as $k => $v) {
            $old_name = $v['name'];
            $old_options[] = $old_name;
            $exploded = explode(' - ', $old_name);
            $new_name = (count($exploded) > 1) ? $exploded[1] : $old_name;
            $data['variations'][$k]['name'] = $new_name;
            foreach ($v['attributes'] as $ak => $av) {
                if ($av['option'] && $av['option'] === $old_name) {
                    $data['variations'][$k]['attributes'][$ak]['option'] = $new_name;
                }
            }
            $new_options[] = $new_name;
        }
    
        foreach ($data['attributes'] as $dak => $dav) {
            if ($dav['options'] && $dav['options'] === $old_options) {
                $data['attributes'][$dak]['options'] = $new_options;
    
            }
        }
    
        return $data;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Imported variation attributes saving oddly’ is closed to new replies.