Skip to content

Magento 2 : Graphql setShippingAddressesOnCart default query duplicates the addresses in quote_address table

I’m using Magento version 2.4.5-p1 And I’m trying to setShippingAddressesOnCart using graphql for logged-in customers.

This is the default Magento query I’m using but it’s duplicating the data for the shipping address in quote_address,

For Example,
If I setShippingAddressesOnCart query once there will be 3 records in the quote_address table.
first and second records will be for shipping and billing, and the third will be a duplicate record for shipping

And on Cart Query, If I run this query once it will show double
records for shipping_address, and even without running the shipping
address if I hit the cart Query again and again it will double and
Triple the records for shipping_address, For this Cart Query issue, I
Created the plugin on MagentoQuoteGraphQlModelCartGetCartForUser
and deleted the extra address which resolves the cart query issue,
but for the SetShippingAddressesOnCart query, in the quote_address
table, I’m still seeing duplicate records.

GetCartForUser Plugin

public function __construct(
    MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
    CartRepositoryInterface $cartRepository,
    StoreRepositoryInterface $storeRepository = null
) {
    $this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
    $this->cartRepository = $cartRepository;
    $this->storeRepository = $storeRepository ?: ObjectManager::getInstance()->get(StoreRepositoryInterface::class);
}

public function beforeExecute(MagentoQuoteGraphQlModelCartGetCartForUser $subject, string $cartHash, int $customerId, int $storeId)
{
    // Getting the cartid and deleting the duplicate data
    try {
        $cartId = $this->maskedQuoteIdToQuoteId->execute($cartHash);
    } catch (NoSuchEntityException $exception) {
        throw new GraphQlNoSuchEntityException(
            __('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash])
        );
    }

    $quote = $this->cartRepository->getActive($cartId);
    var_dump($quote->getId());
    $this->_addresses = $quote->getAllShippingAddresses();
    if (count($this->_addresses) > 1) {
        for($i = 1; $i < count($this->_addresses); $i++) {
            $address = $this->_addresses[$i];
            $address->isDeleted(true);
        }
    }
}

SetShippingAddressesOnCart Query

mutation { setShippingAddressesOnCart(
input: {
  cart_id: "6EyeisgYP1VTwAKqC092n6Xe3DQGZLcB"
  shipping_addresses: [
    {
      address: {
        firstname: "Mohitttttttt"
        lastname: "RRrrrrrrrrr"
        company: "DCKAP"
        street: ["Test1", "income tax1"]    
        city: "Test4444444e"
        region: "TX"
        postcode: "07803"
        country_code: "US"
        telephone: "8675309"
        save_in_address_book: false
      },
    }
  ]
}
)
{
cart {
  shipping_addresses {
    firstname
    lastname
    company
    street
    city
    region {
      code
      label
    }
    postcode
    telephone
    country {
      code
      label
    }
    available_shipping_methods{
      carrier_code
      carrier_title
      method_code
      method_title
    }
  }
}

}
}

Cart Query

{ 
cart(cart_id: "6EyeisgYP1VTwAKqC092n6Xe3DQGZLcB") {
email
billing_address {
  city
  country {
    code
    label
  }
  firstname
  lastname
  postcode
  region {
    code
    label
  }
  street
  telephone
}
shipping_addresses {
  firstname
  lastname
  street
  city
  region {
    code
    label
  }
  country {
    code
    label
  }
  telephone
  available_shipping_methods {
    amount {
      currency
      value
    }
    available
    carrier_code
    carrier_title
    error_message
    method_code
    method_title
    price_excl_tax {
      value
      currency
    }
    price_incl_tax {
      value
      currency
    }
  }
  selected_shipping_method {
    amount {
      value
      currency
    }
    carrier_code
    carrier_title
    method_code
    method_title
  }
}
items {
  id
  product {
    name
    sku
  }
  quantity
  errors {
    code
    message
  }
}
available_payment_methods {
  code
  title
}
selected_payment_method {
  code
  title
}
applied_coupons {
  code
}
prices {
  grand_total {
    value
    currency
  }
}

}
}

Let me know if anyone has any ideas.
Thank you.