Drupal 9 image field migration

It's really nice module which makes customers flow better on website with no need to reload the page on every action. https://www.drupal.org/project/commerce_cart_flyout 
One thing I dont like is a lot of not needed information was present in responses from cart or product. In my case there were internal fields of Product Variations entity which are not displayed for customers.
There is no default settings to handle that case so optimization should be done. You need custom Normalizations for cart and variation cases.

Variations normalization. Add to services.yml in your module
  my_commerce.cart_normalizer.product_variation:
    class: Drupal\my_commerce\Normalizer\VariationCartNormalizer
    tags:
      - { name: normalizer, priority: 2 }

then add a file to my_commerce/src/Normalizer/VariationCartNormalizer.php.
The main idea is to have an array $whitelistFields for fields that need to be in response.

https://gist.github.com/onesixromcom/bcc813af2934a82d781d1c97b10f30bf

 

For Order items I use

  my_commerce.cart_normalizer.order_item:
    class: Drupal\my_commerce\Normalizer\OrderItemCartNormalizer
    tags:
      - { name: normalizer, priority: 2 }
and fields variable
  private $whitelistFields = [
    'purchased_entity',
    'quantity',
    'title',
    'total_price',
    'unit_price',
    'order_item_id',

For order itself:

my_commerce.cart_normalizer.order:
    class: Drupal\my_commerce\Normalizer\OrderCartNormalizer
    tags:
      - { name: normalizer, priority: 2 }

It's a bit different variable here, since this array describes blocked fields.

  private $blockedFields = [
    'uuid',
    'store_id',
    'order_number',
    'coupons',
  ];         

Add new comment

The content of this field is kept private and will not be shown publicly.
  • No HTML tags allowed.