Skip to content

Fix customer missing attributes #382

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

Merged
merged 1 commit into from
Apr 24, 2025

Conversation

smolentzov-stripe
Copy link
Contributor

@smolentzov-stripe smolentzov-stripe commented Apr 23, 2025

Context

#375

It is possible for a store to get into a state where the TaxJar custom customer attributes were not created (e.g. if setup was interrupted). This will cause errors when trying to save customers, and customers will not sync to TaxJar.

Specifically: the TaxJar module's Observer/Customer/Save.php observer throws an error when customer attributes are missing:

Error: Call to a member function getValue() on null

This happens when tj_exemption_type, tj_regions, or tj_last_sync customer attributes don't exist in the system. Note - the issue isn't so much that any individual customer doesn't have one of these attributes, but that the attributes themselves don't exist in the database (so, can't be applied to an individual customer!).

Description

This PR adds:

  • Null value checks during save to ensure no errors are generated if for some reason the customer attributes are missing.
  • A configurable attribute management system for the custom TaxJar customer attributes:
  1. Detects when TaxJar customer attributes are missing
  2. Displays an admin UI notification on customer pages
  3. Provides a one-click "Install Missing Attributes Now" button to fix the issue
  4. Includes fallback instructions for using the command line if preferred
  • This solution ensures administrators understand why tax exemption settings aren't persisting and provides an immediate way to fix the problem without requiring command-line access.
image

Feature Additions:

  • Introduced a new block (AttributeCheck.php) to detect any missing TaxJar customer attributes.
  • Added Install.php controller to manually install missing customer attributes.
  • Updated admin configurations to include a setting for attribute warnings.
  • Updated layout files to integrate attribute check functionality in relevant views.

Code Adjustments:

  • Modified Observer logic to safely handle null attribute values.
  • Updated XML configuration files to reflect new options and settings.
  • Added new template files to display warnings and prompt user actions related to attribute management.

Performance

Minimal impact. Adds an extra check on the customers page in the Admin UI. This was intentionally not cached to provide real-time information. This does not impact the customer-facing storefront at all.

Testing

Step 1: Install the Updated TaxJar Module

  1. Install this branch of the TaxJar module:
    bin/magento setup:upgrade
    bin/magento cache:clean
    bin/magento cache:flush
    

Step 2: Reproduce the Issue

  1. Delete one or more TaxJar customer attributes from the database:

    -- Get attribute IDs
    SELECT attribute_id, attribute_code 
    FROM eav_attribute 
    WHERE attribute_code IN ('tj_exemption_type', 'tj_regions', 'tj_last_sync') 
    AND entity_type_id = (SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = 'customer');
    
    -- Delete attribute (replace {ATTRIBUTE_ID} with the actual ID)
    DELETE FROM eav_entity_attribute WHERE attribute_id = {ATTRIBUTE_ID};
    DELETE FROM customer_eav_attribute WHERE attribute_id = {ATTRIBUTE_ID};
    DELETE FROM eav_attribute WHERE attribute_id = {ATTRIBUTE_ID};
  2. Clear Magento cache:

    bin/magento cache:clean
    bin/magento cache:flush
    
  3. Navigate to the customer grid in the admin panel (Customers >> All Customers)

    • You should see a warning message about missing attributes with an "Install Missing Attributes Now" button. Resist the urge to click this button yet!
  4. Try to save a customer through the admin interface

    • Without the error fix: This would produce an error.
    • With the changes from this branch: Customer should save without errors AND display the warning message with the fix button.
      • Note that while the customer will save successfully in Magento, you will not see any information related to the custom TaxJar attributes synced to your TaxJar account.

Step 3: Test with All Attributes Present

  1. Run setup:upgrade to re-install all attributes:

    bin/magento setup:upgrade
    

    Note: If you encounter any errors during the setup:upgrade process, the command may not complete successfully. In this case, you can use our "Install Missing Attributes Now" button instead, which is designed to work even when setup:upgrade fails.

  2. Save a customer with TaxJar attributes set

    • The observer should function normally.
    • No warning messages should appear.
    • The customer information should sync to your TaxJar account.

Step 4: Test with Partial Attributes

  1. Delete only one attribute (e.g., tj_regions).

  2. Navigate to the customer grid or edit page.

    • You should see a warning message showing specifically which attribute is missing (tj_regions).
    • The "Install Missing Attributes Now" button should be visible.
  3. Click the "Install Missing Attributes Now" button

    • You should see a success message that attributes have been installed.
    • The warning message should disappear after page refresh.
  4. Edit and save a customer with tax exemption settings

    • The settings should now save correctly.
    • No errors should occur.
    • The customer information should sync to your TaxJar account.

Versions

  • Magento 2.4.7
  • Magento 2.4.6
  • Magento 2.3
  • Magento Open Source (formerly Magento 2 Community Edition)
  • Adobe Commerce (formerly Magento 2 Enterprise Edition)
  • PHP 8.2
  • PHP 8.1
  • PHP 7.4
  • PHP 7.3

@smolentzov-stripe smolentzov-stripe force-pushed the TJPLAT-2367-customer-attributes branch from bb28f2f to 84b05d4 Compare April 23, 2025 21:01
@smolentzov-stripe smolentzov-stripe force-pushed the TJPLAT-2367-customer-attributes branch from 84b05d4 to 1f4f443 Compare April 23, 2025 21:06
@smolentzov-stripe smolentzov-stripe merged commit 9998be8 into develop Apr 24, 2025
2 checks passed
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.

2 participants