2017 was a year of resurgence for megaretailer Walmart (NYSE:WMT). Not only did the company's thousands of stores produce consistent and impressive sales growth, but e-commerce became a cornerstone of the business rather than an afterthought. Walmart's U.S. e-commerce sales grew by 63%, 60%, and 50% year over year during the first three quarters of the year, respectively. Those are blistering numbers given Walmart's size.

Things didn't look nearly as good during the fourth quarter. U.S. e-commerce sales rose just 23% year over year, with the company blaming the lapping of its acquisition of Jet.com, as well as operational challenges, for the lackluster growth.

Walmart expects various new initiatives to drive 40% U.S. e-commerce growth in 2018, so the fourth-quarter dip may just be a speed bump in Walmart's effort to compete head-on with Amazon.com (NASDAQ:AMZN). But this ongoing battle with the king of e-commerce is already leading to some bottom-line pain for Walmart, a situation that's unlikely to change any time soon.

A man holding two Walmart boxes opening a door.

Image source: Walmart.

Margin trouble

While Walmart grew its total revenue by 4.2% during the fourth quarter, its gross profit barely budged. Chalk that up to a slumping gross margin. Walmart's fourth-quarter gross margin was 24.7%, down from 25.4% in the prior-year period. That may not seem like a big change, but remember we're talking about a company that sells $500 billion worth of stuff each year.

Walmart blamed lower prices, higher transportation costs, and a mix shift toward e-commerce. Walmart started offering free two-day shipping for online orders above $35 in early 2017, so the company's shipping costs in the fourth quarter were likely quite a bit higher than in the past.

On top of the gross margin decline, Walmart's operating expenses jumped 8%, leading to a steep 28% decline in operating income. There were some one-time charges that could reasonably be backed out, but heavy investments in e-commerce played a role as well. Walmart's operating margin was just 3.3% in the fourth quarter, down from 4.7% in the prior-year period.

Walmart does expect its adjusted earnings to grow in 2018, although a lower expected tax rate, thanks to the U.S. tax bill, is part of the reason. Given its performance in the fourth quarter, Walmart will need to keep investing intensely in its online business in order to hit 40% e-commerce growth this year. The company's margins may not recover any time soon.

Making the right moves

Despite the negative effects on the bottom line, Walmart's expensive push into e-commerce is the right move. Largely ignoring e-commerce was fine when Amazon was small and sales of major categories like grocery and apparel were, for the most part, confined to physical stores. But with all shopping now shifting online, and with Amazon buying Whole Foods and toying with other store concepts, it's clear that an omnichannel approach is the only way to go.

Walmart is trying to use its stores to its advantage. Its online grocery service, live at more than 1,000 stores, allows customers to pick up their online grocery orders curbside at no charge, with delivery available in some markets. Pickup discounts on certain online items knocks down prices for customers willing to pick up their orders in-store instead of having them delivered. The company is even testing out the idea of store employees dropping off packages during their commutes home in an effort to reduce shipping costs and increase shipping speeds.

Free two-day shipping is the central pillar of Walmart's strategy, and it's likely costing it a pretty penny. With tens of millions of Amazon Prime members convinced by the e-commerce giant to prepay for fast shipping, Walmart had no other option than to have a compelling free-shipping offer of its own. That, along with a vastly expanded assortment of online items, has turbocharged Walmart's online growth.

Investors are no doubt disappointed that Walmart's margins suffered during the fourth quarter because of this online push. But they'd be even more disappointed in the long run if Walmart pulled back on its e-commerce efforts. Competing with Amazon isn't cheap, but it's the only path forward.

John Mackey, CEO of Whole Foods Market, an Amazon subsidiary, is a member of The Motley Fool's board of directors. Timothy Green has no position in any of the stocks mentioned. The Motley Fool owns shares of and recommends Amazon. The Motley Fool has a disclosure policy.