Walmart (NYSE:WMT) disappointed investors the last couple of quarters with underwhelming e-commerce sales -- growth came in at just 23% in the fourth quarter and 33% in the first. Many investors, myself included, doubted management's ability to make good on its forecast of 40% online sales growth for fiscal 2019.

But Walmart hit that number right on the nose for U.S. e-commerce in its second-quarter results. With somewhat easier comps in the back half of the year, management reiterated its expectations of full-year 40% growth. And Walmart is making all the right investments to ensure it hits that mark, particularly in online grocery sales.

A customer shops Walmart.com on a laptop

Image source: Walmart.

That said, the retail giant faces ample competition, not least of which is from Amazon (NASDAQ:AMZN). The online retailer is currently rolling out digital grocery ordering and pickup from its Whole Foods locations. It's also expanding its free delivery service.

Increasing investments in e-commerce

During Walmart's earnings call, management noted they're accelerating investments, particularly in grocery pickup and delivery. The company now offers grocery pickup from 1,800 stores across the United States, and it plans to reach more than 2,000 by the end of the year. It also has more than 320 locations that offer grocery delivery, and it's piloting a self-driving car program in Arizona for grocery pickup.

That expansion is one of the major driving forces behind growing e-commerce sales. Management previously cited the progress in grocery as a key reason it believes it can achieve that 40% target.

Walmart has more stores than anyone else and nationwide coverage, so it can leverage its technology investments better than the competition, even Amazon. Whole Foods has around 450 stores total, while Walmart could have that many stores offering grocery delivery by the end of the year.

Keeping prices low

Unfortunately, Walmart's growing online business is also negatively impacting margins. If groceries are priced the same online as they are in store, the company sees reduced profitability from having to pick, pack, and deliver groceries to a customer's car. The same is true to a lesser extent for in-store pickup orders and perhaps to a greater extent for items shipped from stores due to fulfillment expenses.

The company continues to experiment with pricing, but for the most part, customers won't find any big discrepancies in online prices and in-store prices. Walmart is currently running its online operations at a loss due to the lower gross margin on online sales. That loss is actually increasing this year, according to management's commentary.

As Walmart makes gains in e-commerce, it's also working to automate more parts of the process. Pickup Towers in stores allow customers to automatically find their packages without the need for an associate to go to the back room and retrieve them. Walmart is also working on automating the picking process for grabbing items off shelves for online orders. Automation technology costs a lot up front, but it can offset the gross margin pressure of online sales in the long run.

Where do online sales go from here?

Walmart will have rolled out online grocery ordering to more than half of its stores by the end of the year. It's still just getting started on delivery, but it expects to cover 40% of the U.S. population by the end of the year.

Walmart will have to keep innovating and finding ways to drive customers to its website after rolling out online grocery pickup and delivery to practically every metro area it operates in. It's adding new brands and has overhauled its website, but it's still not a destination for most online shoppers the way Amazon is.

The big-box retailer will have to convert its online grocery shoppers into regular online retail customers in order to sustain growth and provide a point of differentiation from Amazon. Continued investments in price and automation will help, but Amazon is equally capable of keeping prices low and offers great convenience through its ever-popular Prime program. Investors should watch for management commentary on the future of its e-commerce business as Walmart approaches the top half of the curve on its online grocery rollout.

John Mackey, CEO of Whole Foods Market, an Amazon subsidiary, is a member of The Motley Fool's board of directors. Adam Levy owns shares of Amazon. The Motley Fool owns shares of and recommends Amazon. The Motley Fool has a disclosure policy.