Michael Maxwell, 59, was spotted by store detectives standing next to a female member of staff with his penis on show at The Range shop last year.
Maxwell claimed he was just shopping and that he had accidentally exposed himself due to the dodgy fastenings.
But store security called in the police and the farm worker, of Gordon, Berwickshires, was arrested and charged with public indecency following the incident.
Maxwell pleaded guilty to exposing himself at the home and garden store at the capital’s Milton Link last December when he appeared at Edinburgh Sheriff Court on Tuesday.
He escaped being placed on the sex offenders register after the Crown said it believed there had been “no sexual element” to his flashing.
Sheriff Peter McCormack decided to mark the offence by fining Maxwell a total of £360.
Fiscal depute Nicole Lavelle said Maxwell was spotted acting suspiciously at The Range by store security at around 2.30pm on December 21 last year.
Ms Lavelle told the court the store detective followed him around the shop for around 20 minutes as he was “not acting as a normal shopper”.
Ms Lavelle said: “He was walking around the shop with an empty basket and after 20 minutes the store detective concluded he was homeless and was within to get warm.
“He then spotted the accused in the regatta section at the back door and he appeared to be standing close to a female member of staff who was fixing a display.
“The female was on her knees and the accused was standing near to the back of her.”
A second store detective also saw Maxwell acting suspiciously and observed he had “his left hand behind the basket”.
The fiscal added: “Through the gap in the basket, which the accused was holding in front of his crotch, the security member could see he was holding his exposed penis.”
The prosecutor said Maxwell’s jean buttons were “undone but his trousers were still up”.
He was immediately confronted by the store staff who then “observed the accused’s penis was outwith his trousers”.
Ms Lavelle added: “The accused said ‘it just fell out’. His penis was in a flaccid state and he was still holding it in his hand.
“He said the buttons on his jeans were faulty.”
Defence solicitor Milly Virtue said her client worked at a farm near his home in the Borders and he was single and lived with his elderly mother.
Ms Virtue said Maxwell had gone to the store to buy containers for ice and he maintains that “there was a difficultly with the fastening on his trousers and he became exposed”.
The brief added: “He panicked as to what to do and was exposed for a couple of minutes. He thought he shouldn’t move it around and try and rectify it.
Ms Virtue said: “He accepts he took too long and was apprehended by security in the state described.
“He is utterly embarrassed at having to come to court for this offence.”
Maxwell admitted causing an offence of public indecency by exposing his penis at The Range, Milton Link, Edinburgh, on December 21 last year.
Sign up to our newsletter: enter your email in the box at the top of this article to get daily updates straight to your inbox.