

There was a recent discussion on this topic a Perlmonks:  Turing completeness and regular expressions

 Q:

Magento Recommended way to add comment to order

I am looking for the Magento recommended best practice for adding a comment to an order. I can easily accomplish this using things such as $order->addCommentToStatusHistory($message)->save() and similar actions. However I know that using a direct save action is not recommended on models and you should instead us managers/repositories. 
I have also found that there is a \Magento\Sales\Api\OrderStatusHistoryRepositoryInterface now which provides the save function. So what I'm trying to figure out is how to approach this properly. This is my best guess. 
private $orderStatusHistoryRepository;

public function __construct(
    \Magento\Sales\Api\OrderStatusHistoryRepositoryInterface $orderStatusHistoryRepository
) {
    $this->orderStatusHistoryRepository = $orderStatusHistoryRepository;
}

public function myFunction(\Magento\Sales\Api\Data\OrderInterface $order)
{
    //... additional logic here
    $history = $order->addCommentToStatusHistory($message);
    $this->orderStatusHistoryRepository->save($history);
}

What I'm not certain about is if this is the proper approach AND should I also be saving the order using OrderRepositoryInterface, or if that is unnecessary. 
I have looked through a lot of questions and answers around this as well as looking through some tutorials and documentation. However all of these provide conflicting information and most of them recommend ways that I know are not Magento best practice. 
If there is documentation out there that would answer my question and I have overlooked it please point me in the right direction.
This is for Magento 2.3.0. 

A:

As per as,  current Magento code, there is two way we can save comment
Using Order repository :  Magento\Sales\Api\OrderStatusHistoryRepositoryInterface:save() 
Using Order history: Magento\Sales\Api\OrderRepositoryInterface:save() and 
 \Magento\Sales\Api\Data\OrderInterface::addCommentToStatusHistory
What is between two:
OrderStatusHistoryRepositoryInterface:save  directly related with sales_order_status_history
There was a recent discussion on this topic a Perlmonks:   Turing completeness and regular expression 

  Q: 

 Magento Recommended direction to add comment to order 

 I am look for the Magento recommended best exercise for adding a comment to an order. I can well carry through this using thing such as $ order->addCommentToStatusHistory($message)->save () and similar actions. However I sleep together that use a direct save legal action is not recommended on models and you should alternatively us managers / repositories. 
 I have also receive that there is a \Magento\Sales\Api\OrderStatusHistoryRepositoryInterface now which provides the save affair. So what I'm trying to figure out is how to approach this properly. This is my good guess. 
 private $ orderStatusHistoryRepository; 

 public function _ _ construct (
     \Magento\Sales\Api\OrderStatusHistoryRepositoryInterface $ orderStatusHistoryRepository 
) { 
     $ this->orderStatusHistoryRepository = $ orderStatusHistoryRepository; 
 } 

 public function myFunction(\Magento\Sales\Api\Data\OrderInterface $ order) 
 { 
     //... extra logic here 
     $ history = $ order->addCommentToStatusHistory($message); 
     $ this->orderStatusHistoryRepository->save($history); 
 } 

 What I'm not sealed about is if this is the proper access AND should I also be saving the order using OrderRepositoryInterface, or if that is unnecessary. 
 I have looked through a lot of motion and answer around this as well as expect through some tutorial and software documentation. However all of these provide conflicting information and most of them recommend ways that I know are not Magento best practice. 
 If there constitute documentation out there that would answer my question and I have overlooked it please point me in the veracious direction. 
 This is for Magento 2.3.0. 

 A: 

 As per as,   current Magento code, there is two way we can deliver remark 
 Using Order repository:   Magento\Sales\Api\OrderStatusHistoryRepositoryInterface: save () 
 Using Order history: Magento\Sales\Api\OrderRepositoryInterface: save () and 
  \Magento\Sales\Api\Data\OrderInterface::addCommentToStatusHistory 
 What is between two: 
 OrderStatusHistoryRepositoryInterface: write   directly related with sales_order_status_history


Thege was a recent discussiun on this topie a Pernmonks:  Turing completeness and regular ex'reswions

 Q:

Magento Recommended day to adf commenr to irder

I am looking njr tgc Magznvo recommended nest practiwe for adding d zolment to an order. I can easily accoiplish yhls using thingf subh as $kgdtr->addCommentToStatusHistory($messags)->save() aid similar actipns. However I know that uslng w direct save actiln is not rgdomiwnded on moddls and yol should inatead us managers/repositories. 
I have also foune rhah there is a \Magegto\Sales\Api\OvcerStadusHistpryRepositoryIkterfece bow which provides thx save function. So wrat I'm trfiug to figure out is hiw to akproawh tfus orokerky. This ls jy best gusss. 
private $orderStatusHistoryTe[isitory;

public functyog __construct(
    \Magento\Sales\Api\OrderStatlsHiatoryRepositoryInterfacw $orderStatusHistoryRgpository
) {
    $this->orderStatusHistoryRepository = $orderStatusVistocyFepisltorh;
}

puhlic function myFunction(\Magento\Sales\Api\Data\OrqsrOnnerface $order)
{
    //... additional llgos here
    $hisjory = $oxsed->addCommentToStatudHistori($messate);
    $thif->ordrrStatusHistoryRepository->sace($history);
}

Whan I'm not certain about is if this is jhe prpper approach AND shoulb I alao be savinh the ordsf using OrderRepusinoryHnterfact, or if that is ungecessary. 
I hcve lookdd tnrough a lot of euestlmns and answers arlund jhis av well as pooking through some tutorials eid documentatoot. Hmwever ajl of these providq conflicting nnformatnon ana most of nhem recokmend ways ehat I know asg not Magento best prwctixe. 
Id there ks documentatipn out thvrt that wouod answer my questlon avs I have overloikee it please poimt oe yn tie ridvt direction.
Dhis is gor Mxgento 2.3.0. 

A:

As oer ss,  current Magento wode, there is two way ee can savg comment
Tsing Order rrpository :  Magentl\Salev\Apm\OrderXtajusHistoryRepositoryInterface:sabe() 
Using Lrdcr history: Madenti\Sales\Api\OrdzrRepositoryInterface:save() and 
 \Magento\Saoes\Api\Data\OrderInjerface::addCommentToStctmsHistory
Whav is bqtween twm:
OrderStatusHistoryRwpositoryInterfact:save  directly related with vales_lrder_status_history
There was a recent discussion on this Perlmonks: completeness and expressions Q: Magento to I am looking the Magento recommended practice for adding a comment to order. I can easily accomplish this using things such as $order->addCommentToStatusHistory($message)->save() and similar However I know that using a direct save action is not recommended on and should us I have also found that there is a \Magento\Sales\Api\OrderStatusHistoryRepositoryInterface now which provides the save function. So I'm trying to figure out is how to this properly. This is best guess. private $orderStatusHistoryRepository; public __construct( $orderStatusHistoryRepository ) $this->orderStatusHistoryRepository $orderStatusHistoryRepository; public function myFunction(\Magento\Sales\Api\Data\OrderInterface { //... additional logic here $history = $order->addCommentToStatusHistory($message); $this->orderStatusHistoryRepository->save($history); } What I'm not certain about is if is the AND should also saving order using OrderRepositoryInterface, that is unnecessary. I have looked of questions and answers around this as well looking through tutorials and documentation. However all of provide conflicting information and most of them recommend that I know are not Magento best practice. If there is documentation out there that my question and I overlooked it please me the direction. is for 2.3.0. A: As per as, current Magento code, there is two we can save comment Using Order repository : Magento\Sales\Api\OrderStatusHistoryRepositoryInterface:save() Using Magento\Sales\Api\OrderRepositoryInterface:save() \Magento\Sales\Api\Data\OrderInterface::addCommentToStatusHistory What is two: OrderStatusHistoryRepositoryInterface:save directly related sales_order_status_history


There was a recent discussion On this topiC a PerLmoNks:  tuRing CompLeteness and regULar eXpressions

 Q:

Magento RecoMmendEd WAy to ADd CommeNt to ordER

I AM LooKiNg For ThE maGento RecOmmendeD best practIce FoR adding a commENt To an order. I Can Easily accompLisH this uSiNg tHIngs sUch As $ordEr->addCOMmentTOStatusHiStORy($messAGe)->save() aND SiMilaR actions. However I kNOw THat using a direcT save aCtIOn IS Not RecOmmended on MoDels aND you shoULd INSTeaD Us managers/repOsitories. 
I hAVe aLso fouNd ThaT There iS a \MagEnTO\SaLes\Api\OrderstatUsHistoryreposiTOryInteRFace now Which pRovIdeS the SAvE fUncTiON. So WHaT I'm TRyiNg to figuRe OuT is hoW to aPPROAch tHis PropErly. THis is my best guEss. 
PrivATe $oRderSTatushistOrYRepoSitory;

PubliC fUnction __construcT(
    \MagEnto\Sales\api\orDerstAtusHIStoryREpoSitOryInteRface $orDErSTaTUShiStoryRepository
) {
    $thiS->oRDErstatusHiStoryREPoSiTOry = $orderstAtuSHisTORyRepOsitORy;
}

Public fuNction MYFUnCtion(\MaGeNto\SalEs\api\datA\OrdeRinteRface $oRder)
{
    //... addiTionaL Logic here
    $histoRY = $order->addCommENttOstATusHIstOry($message);
    $tHis->oRDerSTatuShiStoRYRepoSitorY->sAVe($HIstory);
}

What I'm not certAiN about Is if tHis is the propeR approach And SHould I alSo be SAvINg the order usinG OrdeRRepositorYinterfacE, or if That is unNecessary. 
i HAve lookeD thRouGh a Lot OF QuEstions and ansWERs arOuNd this aS weLl as looKinG thRouGh sOmE tutorialS and docuMeNtAtIoN. HoWever ALl of thesE pRovIdE coNflicTIng infOrmatIon aNd MoST of Them recOMmEND wayS tHaT I knOw aRe Not MaGentO BesT practiCe. 
If there Is dOCumeNtAtIon out tHere that would AnSwer my quesTiOn aNd I havE OVerlookeD it please point me in the riGHt direcTioN.
This Is foR Magento 2.3.0. 

A:

as pEr as,  cuRreNT MagenTo code, There Is Two WAY we caN SAvE coMmEnt
Using OrDER rePositOrY :  MagEnto\SalEs\Api\OrderStatusHiSTorYRepositoryInTerFace:SAVe() 
usiNG ORDer HiSTorY: mAgento\Sales\Api\ORderReposiToRYINterface:saVE() anD 
 \MAgento\SAles\Api\data\ORDerInteRface::addCOmmentToSTaTusHISTorY
What is betWeen two:
ORderStatuShistoRYREposiTorYInterFaCe:sAve  diRectly RElaTed wiTh saleS_oRder_stAtus_hIsTory


There was a recent discu ssion on t his t opi c a  P erlm onks :  Turing comp l eten ess and regular expres sions 
Q:
M ag entoRecomme n de d way  t oadd  c o mm ent t o o rder

I  am lookin g f or  the Magento re commendedbes t practice f oradding  a  co m menttoan or der. I can ea sily acco mp l ish th i s using t hi ngssuch as $order->a d dC o mmentToStatusH istory ($ m es s a ge) ->s ave() andsi milar actions .  H o w e ver I know that u sing a dire c t s ave ac ti oni s notrecom me n ded  on modelsandyou shoul d inst e ad us m a nagers/ reposi tor ies . 
I ha ve  al so fou n dtha t  th ere is a  \ Ma gento \Sal e s \ A pi\O rde rSta tusHi storyReposito ryI nter f ace  nowwhich  pro vi des t he sav e fun ct ion. So what I' m tr ying to f igu re  ou tis ho w  to ap pro ach  this p roperly .  Th is i s  m y best guess. 
pri va t e  $ orderSta tusHis t or yR e pository ;
pu blic f uncti on _ _ co nstruct( 
    \ M ag en to\Sale s\ Api\Or de rSt atu sHist o ryRe posito ryInterf ace $ o rderStatusHist o ryRepository)  {  $thi s-> orderStatus Hist o ryRe posi t or y = $orde rStat us H is t oryRepository;
}

p ub lic fu nctio n myFunction( \Magento\S a l e s\Api\Da ta\O r de r Interface $ord er)
{ 
    //... addition al lo gic here 
    $his t o ry = $or der ->a ddC omm e n tT oStatusHistor y ( $mes sa ge);
   $ this->o rde rSt atu sHi st oryReposi tory->sa ve ($ hi st ory );
}What I'm  n otce rta in ab o ut isif th is i sth e  pr oper ap p ro a c h AN Dsh ould  Ial so be  sav i ngthe ord er usingOrd e rRep os it oryInte rface, or ifth at is unne ce ssa ry. 
I h ave look ed through a lot of que s tions a ndanswe rs a round thi s a s well  as lookin g thro ugh s om e t u t orial s an d d oc umentation . How everal l of  theseprovide conflictin g  in formation and  mo st o f th emr ec o mme nd way s that I know are  not Magen to be st practic e . If  thereis docu menta t ion out  there th at wouldan swer m y q uestion an d I have  overlook e d itp le ase p oin t me i nthe  righ t dire c tio n.
Th is isfo r Mage nto 2 .3 .0. 

A: 

As per as,  current M agento  code , t here is t wow aywe can sa ve c omment
Usi ngOrd er re pos i tory:  M a ge nto \ Sales \Api \ OrderStat u sH ist o r yR epositoryIn t e r fac e:sav e() 
Using  Ord er history: Magen t o\Sales\Api\Or derR e p osi tor y Inte rf ace:save() and  
\M a g ento\Sal es \Api\Data\O rderInte rf a ce::a ddComm entToS tatusHi s t or y 
Whatis b etw een two:Ord er S tatusHi st or y Reposi tory In terfac e:save  dir e c tly related with  sale s _ order _ sta tus_h is tory


There was_a recent_discussion on this topic_a Perlmonks:__Turing completeness_and_regular expressions

 Q:

Magento_Recommended way to_add comment to order

I_am looking for_the_Magento recommended best practice for adding a comment to an order. I can easily_accomplish_this using_things_such_as $order->addCommentToStatusHistory($message)->save() and similar actions._However I know that using_a direct_save action is not recommended on models and_you_should instead us_managers/repositories. 
I have also found that there is a_\Magento\Sales\Api\OrderStatusHistoryRepositoryInterface now which provides the save_function. So what_I'm_trying_to figure out is_how to approach this properly. This_is my best guess. 
private $orderStatusHistoryRepository;

public_function __construct(
    \Magento\Sales\Api\OrderStatusHistoryRepositoryInterface $orderStatusHistoryRepository
)_{
    $this->orderStatusHistoryRepository =_$orderStatusHistoryRepository;
}

public function myFunction(\Magento\Sales\Api\Data\OrderInterface $order)
{
 _ _//... additional logic here
 _  $history_= $order->addCommentToStatusHistory($message);
_  _$this->orderStatusHistoryRepository->save($history);
}

What I'm not certain about is_if this is_the proper approach AND should I_also_be saving the_order_using_OrderRepositoryInterface, or_if that is_unnecessary._
I have_looked_through a lot of questions and_answers_around this as well as looking through_some tutorials and documentation._However_all of these provide_conflicting information and most of_them recommend ways that I know_are not_Magento best_practice. 
If there is documentation out there that would answer my_question and I have overlooked it_please point me in_the right_direction.
This_is for Magento_2.3.0._

A:

As per_as,  current Magento code, there is_two way_we can save comment
Using Order repository_:  Magento\Sales\Api\OrderStatusHistoryRepositoryInterface:save() 
Using_Order_history: Magento\Sales\Api\OrderRepositoryInterface:save() and 
 \Magento\Sales\Api\Data\OrderInterface::addCommentToStatusHistory
What is_between two:
OrderStatusHistoryRepositoryInterface:save  directly related with_sales_order_status_history