Нема описа

wc-template-functions.php 115KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837
  1. <?php
  2. /**
  3. * WooCommerce Template
  4. *
  5. * Functions for the templating system.
  6. *
  7. * @package WooCommerce\Functions
  8. * @version 2.5.0
  9. */
  10. use Automattic\Jetpack\Constants;
  11. defined( 'ABSPATH' ) || exit;
  12. /**
  13. * Handle redirects before content is output - hooked into template_redirect so is_page works.
  14. */
  15. function wc_template_redirect() {
  16. global $wp_query, $wp;
  17. // phpcs:disable WordPress.Security.NonceVerification.Recommended
  18. // When default permalinks are enabled, redirect shop page to post type archive url.
  19. if ( ! empty( $_GET['page_id'] ) && '' === get_option( 'permalink_structure' ) && wc_get_page_id( 'shop' ) === absint( $_GET['page_id'] ) && get_post_type_archive_link( 'product' ) ) {
  20. wp_safe_redirect( get_post_type_archive_link( 'product' ) );
  21. exit;
  22. }
  23. // phpcs:enable WordPress.Security.NonceVerification.Recommended
  24. // When on the checkout with an empty cart, redirect to cart page.
  25. if ( is_page( wc_get_page_id( 'checkout' ) ) && wc_get_page_id( 'checkout' ) !== wc_get_page_id( 'cart' ) && WC()->cart->is_empty() && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) && ! is_customize_preview() && apply_filters( 'woocommerce_checkout_redirect_empty_cart', true ) ) {
  26. wc_add_notice( __( 'Checkout is not available whilst your cart is empty.', 'woocommerce' ), 'notice' );
  27. wp_safe_redirect( wc_get_cart_url() );
  28. exit;
  29. }
  30. // Logout.
  31. if ( isset( $wp->query_vars['customer-logout'] ) && ! empty( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'customer-logout' ) ) {
  32. wp_safe_redirect( str_replace( '&amp;', '&', wp_logout_url( apply_filters( 'woocommerce_logout_default_redirect_url', wc_get_page_permalink( 'myaccount' ) ) ) ) );
  33. exit;
  34. }
  35. // Redirect to the correct logout endpoint.
  36. if ( isset( $wp->query_vars['customer-logout'] ) && 'true' === $wp->query_vars['customer-logout'] ) {
  37. wp_safe_redirect( esc_url_raw( wc_get_account_endpoint_url( 'customer-logout' ) ) );
  38. exit;
  39. }
  40. // Trigger 404 if trying to access an endpoint on wrong page.
  41. if ( is_wc_endpoint_url() && ! is_account_page() && ! is_checkout() && apply_filters( 'woocommerce_account_endpoint_page_not_found', true ) ) {
  42. $wp_query->set_404();
  43. status_header( 404 );
  44. include get_query_template( '404' );
  45. exit;
  46. }
  47. // Redirect to the product page if we have a single product.
  48. if ( is_search() && is_post_type_archive( 'product' ) && apply_filters( 'woocommerce_redirect_single_search_result', true ) && 1 === absint( $wp_query->found_posts ) ) {
  49. $product = wc_get_product( $wp_query->post );
  50. if ( $product && $product->is_visible() ) {
  51. wp_safe_redirect( get_permalink( $product->get_id() ), 302 );
  52. exit;
  53. }
  54. }
  55. // Ensure gateways and shipping methods are loaded early.
  56. if ( is_add_payment_method_page() || is_checkout() ) {
  57. // Buffer the checkout page.
  58. ob_start();
  59. // Ensure gateways and shipping methods are loaded early.
  60. WC()->payment_gateways();
  61. WC()->shipping();
  62. }
  63. }
  64. add_action( 'template_redirect', 'wc_template_redirect' );
  65. /**
  66. * When loading sensitive checkout or account pages, send a HTTP header to limit rendering of pages to same origin iframes for security reasons.
  67. *
  68. * Can be disabled with: remove_action( 'template_redirect', 'wc_send_frame_options_header' );
  69. *
  70. * @since 2.3.10
  71. */
  72. function wc_send_frame_options_header() {
  73. if ( ( is_checkout() || is_account_page() ) && ! is_customize_preview() ) {
  74. send_frame_options_header();
  75. }
  76. }
  77. add_action( 'template_redirect', 'wc_send_frame_options_header' );
  78. /**
  79. * No index our endpoints.
  80. * Prevent indexing pages like order-received.
  81. *
  82. * @since 2.5.3
  83. */
  84. function wc_prevent_endpoint_indexing() {
  85. // phpcs:disable WordPress.Security.NonceVerification.Recommended, WordPress.PHP.NoSilencedErrors.Discouraged
  86. if ( is_wc_endpoint_url() || isset( $_GET['download_file'] ) ) {
  87. @header( 'X-Robots-Tag: noindex' );
  88. }
  89. // phpcs:enable WordPress.Security.NonceVerification.Recommended, WordPress.PHP.NoSilencedErrors.Discouraged
  90. }
  91. add_action( 'template_redirect', 'wc_prevent_endpoint_indexing' );
  92. /**
  93. * Remove adjacent_posts_rel_link_wp_head - pointless for products.
  94. *
  95. * @since 3.0.0
  96. */
  97. function wc_prevent_adjacent_posts_rel_link_wp_head() {
  98. if ( is_singular( 'product' ) ) {
  99. remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
  100. }
  101. }
  102. add_action( 'template_redirect', 'wc_prevent_adjacent_posts_rel_link_wp_head' );
  103. /**
  104. * Show the gallery if JS is disabled.
  105. *
  106. * @since 3.0.6
  107. */
  108. function wc_gallery_noscript() {
  109. ?>
  110. <noscript><style>.woocommerce-product-gallery{ opacity: 1 !important; }</style></noscript>
  111. <?php
  112. }
  113. add_action( 'wp_head', 'wc_gallery_noscript' );
  114. /**
  115. * When the_post is called, put product data into a global.
  116. *
  117. * @param mixed $post Post Object.
  118. * @return WC_Product
  119. */
  120. function wc_setup_product_data( $post ) {
  121. unset( $GLOBALS['product'] );
  122. if ( is_int( $post ) ) {
  123. $post = get_post( $post );
  124. }
  125. if ( empty( $post->post_type ) || ! in_array( $post->post_type, array( 'product', 'product_variation' ), true ) ) {
  126. return;
  127. }
  128. $GLOBALS['product'] = wc_get_product( $post );
  129. return $GLOBALS['product'];
  130. }
  131. add_action( 'the_post', 'wc_setup_product_data' );
  132. /**
  133. * Sets up the woocommerce_loop global from the passed args or from the main query.
  134. *
  135. * @since 3.3.0
  136. * @param array $args Args to pass into the global.
  137. */
  138. function wc_setup_loop( $args = array() ) {
  139. $default_args = array(
  140. 'loop' => 0,
  141. 'columns' => wc_get_default_products_per_row(),
  142. 'name' => '',
  143. 'is_shortcode' => false,
  144. 'is_paginated' => true,
  145. 'is_search' => false,
  146. 'is_filtered' => false,
  147. 'total' => 0,
  148. 'total_pages' => 0,
  149. 'per_page' => 0,
  150. 'current_page' => 1,
  151. );
  152. // If this is a main WC query, use global args as defaults.
  153. if ( $GLOBALS['wp_query']->get( 'wc_query' ) ) {
  154. $default_args = array_merge(
  155. $default_args,
  156. array(
  157. 'is_search' => $GLOBALS['wp_query']->is_search(),
  158. 'is_filtered' => is_filtered(),
  159. 'total' => $GLOBALS['wp_query']->found_posts,
  160. 'total_pages' => $GLOBALS['wp_query']->max_num_pages,
  161. 'per_page' => $GLOBALS['wp_query']->get( 'posts_per_page' ),
  162. 'current_page' => max( 1, $GLOBALS['wp_query']->get( 'paged', 1 ) ),
  163. )
  164. );
  165. }
  166. // Merge any existing values.
  167. if ( isset( $GLOBALS['woocommerce_loop'] ) ) {
  168. $default_args = array_merge( $default_args, $GLOBALS['woocommerce_loop'] );
  169. }
  170. $GLOBALS['woocommerce_loop'] = wp_parse_args( $args, $default_args );
  171. }
  172. add_action( 'woocommerce_before_shop_loop', 'wc_setup_loop' );
  173. /**
  174. * Resets the woocommerce_loop global.
  175. *
  176. * @since 3.3.0
  177. */
  178. function wc_reset_loop() {
  179. unset( $GLOBALS['woocommerce_loop'] );
  180. }
  181. add_action( 'woocommerce_after_shop_loop', 'woocommerce_reset_loop', 999 );
  182. /**
  183. * Gets a property from the woocommerce_loop global.
  184. *
  185. * @since 3.3.0
  186. * @param string $prop Prop to get.
  187. * @param string $default Default if the prop does not exist.
  188. * @return mixed
  189. */
  190. function wc_get_loop_prop( $prop, $default = '' ) {
  191. wc_setup_loop(); // Ensure shop loop is setup.
  192. return isset( $GLOBALS['woocommerce_loop'], $GLOBALS['woocommerce_loop'][ $prop ] ) ? $GLOBALS['woocommerce_loop'][ $prop ] : $default;
  193. }
  194. /**
  195. * Sets a property in the woocommerce_loop global.
  196. *
  197. * @since 3.3.0
  198. * @param string $prop Prop to set.
  199. * @param string $value Value to set.
  200. */
  201. function wc_set_loop_prop( $prop, $value = '' ) {
  202. if ( ! isset( $GLOBALS['woocommerce_loop'] ) ) {
  203. wc_setup_loop();
  204. }
  205. $GLOBALS['woocommerce_loop'][ $prop ] = $value;
  206. }
  207. /**
  208. * Set the current visbility for a product in the woocommerce_loop global.
  209. *
  210. * @since 4.4.0
  211. * @param int $product_id Product it to cache visbiility for.
  212. * @param bool $value The poduct visibility value to cache.
  213. */
  214. function wc_set_loop_product_visibility( $product_id, $value ) {
  215. wc_set_loop_prop( "product_visibility_$product_id", $value );
  216. }
  217. /**
  218. * Gets the cached current visibility for a product from the woocommerce_loop global.
  219. *
  220. * @since 4.4.0
  221. * @param int $product_id Product id to get the cached visibility for.
  222. *
  223. * @return bool|null The cached product visibility, or null if on visibility has been cached for that product.
  224. */
  225. function wc_get_loop_product_visibility( $product_id ) {
  226. return wc_get_loop_prop( "product_visibility_$product_id", null );
  227. }
  228. /**
  229. * Should the WooCommerce loop be displayed?
  230. *
  231. * This will return true if we have posts (products) or if we have subcats to display.
  232. *
  233. * @since 3.4.0
  234. * @return bool
  235. */
  236. function woocommerce_product_loop() {
  237. return have_posts() || 'products' !== woocommerce_get_loop_display_mode();
  238. }
  239. /**
  240. * Output generator tag to aid debugging.
  241. *
  242. * @param string $gen Generator.
  243. * @param string $type Type.
  244. * @return string
  245. */
  246. function wc_generator_tag( $gen, $type ) {
  247. $version = Constants::get_constant( 'WC_VERSION' );
  248. switch ( $type ) {
  249. case 'html':
  250. $gen .= "\n" . '<meta name="generator" content="WooCommerce ' . esc_attr( $version ) . '">';
  251. break;
  252. case 'xhtml':
  253. $gen .= "\n" . '<meta name="generator" content="WooCommerce ' . esc_attr( $version ) . '" />';
  254. break;
  255. }
  256. return $gen;
  257. }
  258. /**
  259. * Add body classes for WC pages.
  260. *
  261. * @param array $classes Body Classes.
  262. * @return array
  263. */
  264. function wc_body_class( $classes ) {
  265. $classes = (array) $classes;
  266. if ( is_shop() ) {
  267. $classes[] = 'woocommerce-shop';
  268. }
  269. if ( is_woocommerce() ) {
  270. $classes[] = 'woocommerce';
  271. $classes[] = 'woocommerce-page';
  272. } elseif ( is_checkout() ) {
  273. $classes[] = 'woocommerce-checkout';
  274. $classes[] = 'woocommerce-page';
  275. } elseif ( is_cart() ) {
  276. $classes[] = 'woocommerce-cart';
  277. $classes[] = 'woocommerce-page';
  278. } elseif ( is_account_page() ) {
  279. $classes[] = 'woocommerce-account';
  280. $classes[] = 'woocommerce-page';
  281. }
  282. if ( is_store_notice_showing() ) {
  283. $classes[] = 'woocommerce-demo-store';
  284. }
  285. foreach ( WC()->query->get_query_vars() as $key => $value ) {
  286. if ( is_wc_endpoint_url( $key ) ) {
  287. $classes[] = 'woocommerce-' . sanitize_html_class( $key );
  288. }
  289. }
  290. $classes[] = 'woocommerce-no-js';
  291. add_action( 'wp_footer', 'wc_no_js' );
  292. return array_unique( $classes );
  293. }
  294. /**
  295. * NO JS handling.
  296. *
  297. * @since 3.4.0
  298. */
  299. function wc_no_js() {
  300. ?>
  301. <script type="text/javascript">
  302. (function () {
  303. var c = document.body.className;
  304. c = c.replace(/woocommerce-no-js/, 'woocommerce-js');
  305. document.body.className = c;
  306. })();
  307. </script>
  308. <?php
  309. }
  310. /**
  311. * Display the classes for the product cat div.
  312. *
  313. * @since 2.4.0
  314. * @param string|array $class One or more classes to add to the class list.
  315. * @param object $category object Optional.
  316. */
  317. function wc_product_cat_class( $class = '', $category = null ) {
  318. // Separates classes with a single space, collates classes for post DIV.
  319. echo 'class="' . esc_attr( join( ' ', wc_get_product_cat_class( $class, $category ) ) ) . '"';
  320. }
  321. /**
  322. * Get the default columns setting - this is how many products will be shown per row in loops.
  323. *
  324. * @since 3.3.0
  325. * @return int
  326. */
  327. function wc_get_default_products_per_row() {
  328. $columns = get_option( 'woocommerce_catalog_columns', 4 );
  329. $product_grid = wc_get_theme_support( 'product_grid' );
  330. $min_columns = isset( $product_grid['min_columns'] ) ? absint( $product_grid['min_columns'] ) : 0;
  331. $max_columns = isset( $product_grid['max_columns'] ) ? absint( $product_grid['max_columns'] ) : 0;
  332. if ( $min_columns && $columns < $min_columns ) {
  333. $columns = $min_columns;
  334. update_option( 'woocommerce_catalog_columns', $columns );
  335. } elseif ( $max_columns && $columns > $max_columns ) {
  336. $columns = $max_columns;
  337. update_option( 'woocommerce_catalog_columns', $columns );
  338. }
  339. if ( has_filter( 'loop_shop_columns' ) ) { // Legacy filter handling.
  340. $columns = apply_filters( 'loop_shop_columns', $columns );
  341. }
  342. $columns = absint( $columns );
  343. return max( 1, $columns );
  344. }
  345. /**
  346. * Get the default rows setting - this is how many product rows will be shown in loops.
  347. *
  348. * @since 3.3.0
  349. * @return int
  350. */
  351. function wc_get_default_product_rows_per_page() {
  352. $rows = absint( get_option( 'woocommerce_catalog_rows', 4 ) );
  353. $product_grid = wc_get_theme_support( 'product_grid' );
  354. $min_rows = isset( $product_grid['min_rows'] ) ? absint( $product_grid['min_rows'] ) : 0;
  355. $max_rows = isset( $product_grid['max_rows'] ) ? absint( $product_grid['max_rows'] ) : 0;
  356. if ( $min_rows && $rows < $min_rows ) {
  357. $rows = $min_rows;
  358. update_option( 'woocommerce_catalog_rows', $rows );
  359. } elseif ( $max_rows && $rows > $max_rows ) {
  360. $rows = $max_rows;
  361. update_option( 'woocommerce_catalog_rows', $rows );
  362. }
  363. return $rows;
  364. }
  365. /**
  366. * Reset the product grid settings when a new theme is activated.
  367. *
  368. * @since 3.3.0
  369. */
  370. function wc_reset_product_grid_settings() {
  371. $product_grid = wc_get_theme_support( 'product_grid' );
  372. if ( ! empty( $product_grid['default_rows'] ) ) {
  373. update_option( 'woocommerce_catalog_rows', absint( $product_grid['default_rows'] ) );
  374. }
  375. if ( ! empty( $product_grid['default_columns'] ) ) {
  376. update_option( 'woocommerce_catalog_columns', absint( $product_grid['default_columns'] ) );
  377. }
  378. wp_cache_flush(); // Flush any caches which could impact settings or templates.
  379. }
  380. add_action( 'after_switch_theme', 'wc_reset_product_grid_settings' );
  381. /**
  382. * Get classname for woocommerce loops.
  383. *
  384. * @since 2.6.0
  385. * @return string
  386. */
  387. function wc_get_loop_class() {
  388. $loop_index = wc_get_loop_prop( 'loop', 0 );
  389. $columns = absint( max( 1, wc_get_loop_prop( 'columns', wc_get_default_products_per_row() ) ) );
  390. $loop_index ++;
  391. wc_set_loop_prop( 'loop', $loop_index );
  392. if ( 0 === ( $loop_index - 1 ) % $columns || 1 === $columns ) {
  393. return 'first';
  394. }
  395. if ( 0 === $loop_index % $columns ) {
  396. return 'last';
  397. }
  398. return '';
  399. }
  400. /**
  401. * Get the classes for the product cat div.
  402. *
  403. * @since 2.4.0
  404. *
  405. * @param string|array $class One or more classes to add to the class list.
  406. * @param object $category object Optional.
  407. *
  408. * @return array
  409. */
  410. function wc_get_product_cat_class( $class = '', $category = null ) {
  411. $classes = is_array( $class ) ? $class : array_map( 'trim', explode( ' ', $class ) );
  412. $classes[] = 'product-category';
  413. $classes[] = 'product';
  414. $classes[] = wc_get_loop_class();
  415. $classes = apply_filters( 'product_cat_class', $classes, $class, $category );
  416. return array_unique( array_filter( $classes ) );
  417. }
  418. /**
  419. * Adds extra post classes for products via the WordPress post_class hook, if used.
  420. *
  421. * Note: For performance reasons we instead recommend using wc_product_class/wc_get_product_class instead.
  422. *
  423. * @since 2.1.0
  424. * @param array $classes Current classes.
  425. * @param string|array $class Additional class.
  426. * @param int $post_id Post ID.
  427. * @return array
  428. */
  429. function wc_product_post_class( $classes, $class = '', $post_id = 0 ) {
  430. if ( ! $post_id || ! in_array( get_post_type( $post_id ), array( 'product', 'product_variation' ), true ) ) {
  431. return $classes;
  432. }
  433. $product = wc_get_product( $post_id );
  434. if ( ! $product ) {
  435. return $classes;
  436. }
  437. $classes[] = 'product';
  438. $classes[] = wc_get_loop_class();
  439. $classes[] = $product->get_stock_status();
  440. if ( $product->is_on_sale() ) {
  441. $classes[] = 'sale';
  442. }
  443. if ( $product->is_featured() ) {
  444. $classes[] = 'featured';
  445. }
  446. if ( $product->is_downloadable() ) {
  447. $classes[] = 'downloadable';
  448. }
  449. if ( $product->is_virtual() ) {
  450. $classes[] = 'virtual';
  451. }
  452. if ( $product->is_sold_individually() ) {
  453. $classes[] = 'sold-individually';
  454. }
  455. if ( $product->is_taxable() ) {
  456. $classes[] = 'taxable';
  457. }
  458. if ( $product->is_shipping_taxable() ) {
  459. $classes[] = 'shipping-taxable';
  460. }
  461. if ( $product->is_purchasable() ) {
  462. $classes[] = 'purchasable';
  463. }
  464. if ( $product->get_type() ) {
  465. $classes[] = 'product-type-' . $product->get_type();
  466. }
  467. if ( $product->is_type( 'variable' ) && $product->get_default_attributes() ) {
  468. $classes[] = 'has-default-attributes';
  469. }
  470. $key = array_search( 'hentry', $classes, true );
  471. if ( false !== $key ) {
  472. unset( $classes[ $key ] );
  473. }
  474. return $classes;
  475. }
  476. /**
  477. * Get product taxonomy HTML classes.
  478. *
  479. * @since 3.4.0
  480. * @param array $term_ids Array of terms IDs or objects.
  481. * @param string $taxonomy Taxonomy.
  482. * @return array
  483. */
  484. function wc_get_product_taxonomy_class( $term_ids, $taxonomy ) {
  485. $classes = array();
  486. foreach ( $term_ids as $term_id ) {
  487. $term = get_term( $term_id, $taxonomy );
  488. if ( empty( $term->slug ) ) {
  489. continue;
  490. }
  491. $term_class = sanitize_html_class( $term->slug, $term->term_id );
  492. if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
  493. $term_class = $term->term_id;
  494. }
  495. // 'post_tag' uses the 'tag' prefix for backward compatibility.
  496. if ( 'post_tag' === $taxonomy ) {
  497. $classes[] = 'tag-' . $term_class;
  498. } else {
  499. $classes[] = sanitize_html_class( $taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id );
  500. }
  501. }
  502. return $classes;
  503. }
  504. /**
  505. * Retrieves the classes for the post div as an array.
  506. *
  507. * This method was modified from WordPress's get_post_class() to allow the removal of taxonomies
  508. * (for performance reasons). Previously wc_product_post_class was hooked into post_class. @since 3.6.0
  509. *
  510. * @since 3.4.0
  511. * @param string|array $class One or more classes to add to the class list.
  512. * @param int|WP_Post|WC_Product $product Product ID or product object.
  513. * @return array
  514. */
  515. function wc_get_product_class( $class = '', $product = null ) {
  516. if ( is_null( $product ) && ! empty( $GLOBALS['product'] ) ) {
  517. // Product was null so pull from global.
  518. $product = $GLOBALS['product'];
  519. }
  520. if ( $product && ! is_a( $product, 'WC_Product' ) ) {
  521. // Make sure we have a valid product, or set to false.
  522. $product = wc_get_product( $product );
  523. }
  524. if ( $class ) {
  525. if ( ! is_array( $class ) ) {
  526. $class = preg_split( '#\s+#', $class );
  527. }
  528. } else {
  529. $class = array();
  530. }
  531. $post_classes = array_map( 'esc_attr', $class );
  532. if ( ! $product ) {
  533. return $post_classes;
  534. }
  535. // Run through the post_class hook so 3rd parties using this previously can still append classes.
  536. // Note, to change classes you will need to use the newer woocommerce_post_class filter.
  537. // @internal This removes the wc_product_post_class filter so classes are not duplicated.
  538. $filtered = has_filter( 'post_class', 'wc_product_post_class' );
  539. if ( $filtered ) {
  540. remove_filter( 'post_class', 'wc_product_post_class', 20 );
  541. }
  542. $post_classes = apply_filters( 'post_class', $post_classes, $class, $product->get_id() );
  543. if ( $filtered ) {
  544. add_filter( 'post_class', 'wc_product_post_class', 20, 3 );
  545. }
  546. $classes = array_merge(
  547. $post_classes,
  548. array(
  549. 'product',
  550. 'type-product',
  551. 'post-' . $product->get_id(),
  552. 'status-' . $product->get_status(),
  553. wc_get_loop_class(),
  554. $product->get_stock_status(),
  555. ),
  556. wc_get_product_taxonomy_class( $product->get_category_ids(), 'product_cat' ),
  557. wc_get_product_taxonomy_class( $product->get_tag_ids(), 'product_tag' )
  558. );
  559. if ( $product->get_image_id() ) {
  560. $classes[] = 'has-post-thumbnail';
  561. }
  562. if ( $product->get_post_password() ) {
  563. $classes[] = post_password_required( $product->get_id() ) ? 'post-password-required' : 'post-password-protected';
  564. }
  565. if ( $product->is_on_sale() ) {
  566. $classes[] = 'sale';
  567. }
  568. if ( $product->is_featured() ) {
  569. $classes[] = 'featured';
  570. }
  571. if ( $product->is_downloadable() ) {
  572. $classes[] = 'downloadable';
  573. }
  574. if ( $product->is_virtual() ) {
  575. $classes[] = 'virtual';
  576. }
  577. if ( $product->is_sold_individually() ) {
  578. $classes[] = 'sold-individually';
  579. }
  580. if ( $product->is_taxable() ) {
  581. $classes[] = 'taxable';
  582. }
  583. if ( $product->is_shipping_taxable() ) {
  584. $classes[] = 'shipping-taxable';
  585. }
  586. if ( $product->is_purchasable() ) {
  587. $classes[] = 'purchasable';
  588. }
  589. if ( $product->get_type() ) {
  590. $classes[] = 'product-type-' . $product->get_type();
  591. }
  592. if ( $product->is_type( 'variable' ) && $product->get_default_attributes() ) {
  593. $classes[] = 'has-default-attributes';
  594. }
  595. // Include attributes and any extra taxonomies only if enabled via the hook - this is a performance issue.
  596. if ( apply_filters( 'woocommerce_get_product_class_include_taxonomies', false ) ) {
  597. $taxonomies = get_taxonomies( array( 'public' => true ) );
  598. $type = 'variation' === $product->get_type() ? 'product_variation' : 'product';
  599. foreach ( (array) $taxonomies as $taxonomy ) {
  600. if ( is_object_in_taxonomy( $type, $taxonomy ) && ! in_array( $taxonomy, array( 'product_cat', 'product_tag' ), true ) ) {
  601. $classes = array_merge( $classes, wc_get_product_taxonomy_class( (array) get_the_terms( $product->get_id(), $taxonomy ), $taxonomy ) );
  602. }
  603. }
  604. }
  605. /**
  606. * WooCommerce Post Class filter.
  607. *
  608. * @since 3.6.2
  609. * @param array $classes Array of CSS classes.
  610. * @param WC_Product $product Product object.
  611. */
  612. $classes = apply_filters( 'woocommerce_post_class', $classes, $product );
  613. return array_map( 'esc_attr', array_unique( array_filter( $classes ) ) );
  614. }
  615. /**
  616. * Display the classes for the product div.
  617. *
  618. * @since 3.4.0
  619. * @param string|array $class One or more classes to add to the class list.
  620. * @param int|WP_Post|WC_Product $product_id Product ID or product object.
  621. */
  622. function wc_product_class( $class = '', $product_id = null ) {
  623. echo 'class="' . esc_attr( implode( ' ', wc_get_product_class( $class, $product_id ) ) ) . '"';
  624. }
  625. /**
  626. * Outputs hidden form inputs for each query string variable.
  627. *
  628. * @since 3.0.0
  629. * @param string|array $values Name value pairs, or a URL to parse.
  630. * @param array $exclude Keys to exclude.
  631. * @param string $current_key Current key we are outputting.
  632. * @param bool $return Whether to return.
  633. * @return string
  634. */
  635. function wc_query_string_form_fields( $values = null, $exclude = array(), $current_key = '', $return = false ) {
  636. if ( is_null( $values ) ) {
  637. // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  638. $values = $_GET;
  639. } elseif ( is_string( $values ) ) {
  640. $url_parts = wp_parse_url( $values );
  641. $values = array();
  642. if ( ! empty( $url_parts['query'] ) ) {
  643. // This is to preserve full-stops, pluses and spaces in the query string when ran through parse_str.
  644. $replace_chars = array(
  645. '.' => '{dot}',
  646. '+' => '{plus}',
  647. );
  648. $query_string = str_replace( array_keys( $replace_chars ), array_values( $replace_chars ), $url_parts['query'] );
  649. // Parse the string.
  650. parse_str( $query_string, $parsed_query_string );
  651. // Convert the full-stops, pluses and spaces back and add to values array.
  652. foreach ( $parsed_query_string as $key => $value ) {
  653. $new_key = str_replace( array_values( $replace_chars ), array_keys( $replace_chars ), $key );
  654. $new_value = str_replace( array_values( $replace_chars ), array_keys( $replace_chars ), $value );
  655. $values[ $new_key ] = $new_value;
  656. }
  657. }
  658. }
  659. $html = '';
  660. foreach ( $values as $key => $value ) {
  661. if ( in_array( $key, $exclude, true ) ) {
  662. continue;
  663. }
  664. if ( $current_key ) {
  665. $key = $current_key . '[' . $key . ']';
  666. }
  667. if ( is_array( $value ) ) {
  668. $html .= wc_query_string_form_fields( $value, $exclude, $key, true );
  669. } else {
  670. $html .= '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( wp_unslash( $value ) ) . '" />';
  671. }
  672. }
  673. if ( $return ) {
  674. return $html;
  675. }
  676. echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  677. }
  678. /**
  679. * Get the terms and conditons page ID.
  680. *
  681. * @since 3.4.0
  682. * @return int
  683. */
  684. function wc_terms_and_conditions_page_id() {
  685. $page_id = wc_get_page_id( 'terms' );
  686. return apply_filters( 'woocommerce_terms_and_conditions_page_id', 0 < $page_id ? absint( $page_id ) : 0 );
  687. }
  688. /**
  689. * Get the privacy policy page ID.
  690. *
  691. * @since 3.4.0
  692. * @return int
  693. */
  694. function wc_privacy_policy_page_id() {
  695. $page_id = get_option( 'wp_page_for_privacy_policy', 0 );
  696. return apply_filters( 'woocommerce_privacy_policy_page_id', 0 < $page_id ? absint( $page_id ) : 0 );
  697. }
  698. /**
  699. * See if the checkbox is enabled or not based on the existance of the terms page and checkbox text.
  700. *
  701. * @since 3.4.0
  702. * @return bool
  703. */
  704. function wc_terms_and_conditions_checkbox_enabled() {
  705. $page_id = wc_terms_and_conditions_page_id();
  706. $page = $page_id ? get_post( $page_id ) : false;
  707. return $page && wc_get_terms_and_conditions_checkbox_text();
  708. }
  709. /**
  710. * Get the terms and conditons checkbox text, if set.
  711. *
  712. * @since 3.4.0
  713. * @return string
  714. */
  715. function wc_get_terms_and_conditions_checkbox_text() {
  716. /* translators: %s terms and conditions page name and link */
  717. return trim( apply_filters( 'woocommerce_get_terms_and_conditions_checkbox_text', get_option( 'woocommerce_checkout_terms_and_conditions_checkbox_text', sprintf( __( 'I have read and agree to the website %s', 'woocommerce' ), '[terms]' ) ) ) );
  718. }
  719. /**
  720. * Get the privacy policy text, if set.
  721. *
  722. * @since 3.4.0
  723. * @param string $type Type of policy to load. Valid values include registration and checkout.
  724. * @return string
  725. */
  726. function wc_get_privacy_policy_text( $type = '' ) {
  727. $text = '';
  728. switch ( $type ) {
  729. case 'checkout':
  730. /* translators: %s privacy policy page name and link */
  731. $text = get_option( 'woocommerce_checkout_privacy_policy_text', sprintf( __( 'Your personal data will be used to process your order, support your experience throughout this website, and for other purposes described in our %s.', 'woocommerce' ), '[privacy_policy]' ) );
  732. break;
  733. case 'registration':
  734. /* translators: %s privacy policy page name and link */
  735. $text = get_option( 'woocommerce_registration_privacy_policy_text', sprintf( __( 'Your personal data will be used to support your experience throughout this website, to manage access to your account, and for other purposes described in our %s.', 'woocommerce' ), '[privacy_policy]' ) );
  736. break;
  737. }
  738. return trim( apply_filters( 'woocommerce_get_privacy_policy_text', $text, $type ) );
  739. }
  740. /**
  741. * Output t&c checkbox text.
  742. *
  743. * @since 3.4.0
  744. */
  745. function wc_terms_and_conditions_checkbox_text() {
  746. $text = wc_get_terms_and_conditions_checkbox_text();
  747. if ( ! $text ) {
  748. return;
  749. }
  750. echo wp_kses_post( wc_replace_policy_page_link_placeholders( $text ) );
  751. }
  752. /**
  753. * Output t&c page's content (if set). The page can be set from checkout settings.
  754. *
  755. * @since 3.4.0
  756. */
  757. function wc_terms_and_conditions_page_content() {
  758. $terms_page_id = wc_terms_and_conditions_page_id();
  759. if ( ! $terms_page_id ) {
  760. return;
  761. }
  762. $page = get_post( $terms_page_id );
  763. if ( $page && 'publish' === $page->post_status && $page->post_content && ! has_shortcode( $page->post_content, 'woocommerce_checkout' ) ) {
  764. echo '<div class="woocommerce-terms-and-conditions" style="display: none; max-height: 200px; overflow: auto;">' . wc_format_content( wp_kses_post( $page->post_content ) ) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  765. }
  766. }
  767. /**
  768. * Render privacy policy text on the checkout.
  769. *
  770. * @since 3.4.0
  771. */
  772. function wc_checkout_privacy_policy_text() {
  773. echo '<div class="woocommerce-privacy-policy-text">';
  774. wc_privacy_policy_text( 'checkout' );
  775. echo '</div>';
  776. }
  777. /**
  778. * Render privacy policy text on the register forms.
  779. *
  780. * @since 3.4.0
  781. */
  782. function wc_registration_privacy_policy_text() {
  783. echo '<div class="woocommerce-privacy-policy-text">';
  784. wc_privacy_policy_text( 'registration' );
  785. echo '</div>';
  786. }
  787. /**
  788. * Output privacy policy text. This is custom text which can be added via the customizer/privacy settings section.
  789. *
  790. * Loads the relevant policy for the current page unless a specific policy text is required.
  791. *
  792. * @since 3.4.0
  793. * @param string $type Type of policy to load. Valid values include registration and checkout.
  794. */
  795. function wc_privacy_policy_text( $type = 'checkout' ) {
  796. if ( ! wc_privacy_policy_page_id() ) {
  797. return;
  798. }
  799. echo wp_kses_post( wpautop( wc_replace_policy_page_link_placeholders( wc_get_privacy_policy_text( $type ) ) ) );
  800. }
  801. /**
  802. * Replaces placeholders with links to WooCommerce policy pages.
  803. *
  804. * @since 3.4.0
  805. * @param string $text Text to find/replace within.
  806. * @return string
  807. */
  808. function wc_replace_policy_page_link_placeholders( $text ) {
  809. $privacy_page_id = wc_privacy_policy_page_id();
  810. $terms_page_id = wc_terms_and_conditions_page_id();
  811. $privacy_link = $privacy_page_id ? '<a href="' . esc_url( get_permalink( $privacy_page_id ) ) . '" class="woocommerce-privacy-policy-link" target="_blank">' . __( 'privacy policy', 'woocommerce' ) . '</a>' : __( 'privacy policy', 'woocommerce' );
  812. $terms_link = $terms_page_id ? '<a href="' . esc_url( get_permalink( $terms_page_id ) ) . '" class="woocommerce-terms-and-conditions-link" target="_blank">' . __( 'terms and conditions', 'woocommerce' ) . '</a>' : __( 'terms and conditions', 'woocommerce' );
  813. $find_replace = array(
  814. '[terms]' => $terms_link,
  815. '[privacy_policy]' => $privacy_link,
  816. );
  817. return str_replace( array_keys( $find_replace ), array_values( $find_replace ), $text );
  818. }
  819. /**
  820. * Template pages
  821. */
  822. if ( ! function_exists( 'woocommerce_content' ) ) {
  823. /**
  824. * Output WooCommerce content.
  825. *
  826. * This function is only used in the optional 'woocommerce.php' template.
  827. * which people can add to their themes to add basic woocommerce support.
  828. * without hooks or modifying core templates.
  829. */
  830. function woocommerce_content() {
  831. if ( is_singular( 'product' ) ) {
  832. while ( have_posts() ) :
  833. the_post();
  834. wc_get_template_part( 'content', 'single-product' );
  835. endwhile;
  836. } else {
  837. ?>
  838. <?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>
  839. <h1 class="page-title"><?php woocommerce_page_title(); ?></h1>
  840. <?php endif; ?>
  841. <?php do_action( 'woocommerce_archive_description' ); ?>
  842. <?php if ( woocommerce_product_loop() ) : ?>
  843. <?php do_action( 'woocommerce_before_shop_loop' ); ?>
  844. <?php woocommerce_product_loop_start(); ?>
  845. <?php if ( wc_get_loop_prop( 'total' ) ) : ?>
  846. <?php while ( have_posts() ) : ?>
  847. <?php the_post(); ?>
  848. <?php wc_get_template_part( 'content', 'product' ); ?>
  849. <?php endwhile; ?>
  850. <?php endif; ?>
  851. <?php woocommerce_product_loop_end(); ?>
  852. <?php do_action( 'woocommerce_after_shop_loop' ); ?>
  853. <?php
  854. else :
  855. do_action( 'woocommerce_no_products_found' );
  856. endif;
  857. }
  858. }
  859. }
  860. /**
  861. * Global
  862. */
  863. if ( ! function_exists( 'woocommerce_output_content_wrapper' ) ) {
  864. /**
  865. * Output the start of the page wrapper.
  866. */
  867. function woocommerce_output_content_wrapper() {
  868. wc_get_template( 'global/wrapper-start.php' );
  869. }
  870. }
  871. if ( ! function_exists( 'woocommerce_output_content_wrapper_end' ) ) {
  872. /**
  873. * Output the end of the page wrapper.
  874. */
  875. function woocommerce_output_content_wrapper_end() {
  876. wc_get_template( 'global/wrapper-end.php' );
  877. }
  878. }
  879. if ( ! function_exists( 'woocommerce_get_sidebar' ) ) {
  880. /**
  881. * Get the shop sidebar template.
  882. */
  883. function woocommerce_get_sidebar() {
  884. wc_get_template( 'global/sidebar.php' );
  885. }
  886. }
  887. if ( ! function_exists( 'woocommerce_demo_store' ) ) {
  888. /**
  889. * Adds a demo store banner to the site if enabled.
  890. */
  891. function woocommerce_demo_store() {
  892. if ( ! is_store_notice_showing() ) {
  893. return;
  894. }
  895. $notice = get_option( 'woocommerce_demo_store_notice' );
  896. if ( empty( $notice ) ) {
  897. $notice = __( 'This is a demo store for testing purposes &mdash; no orders shall be fulfilled.', 'woocommerce' );
  898. }
  899. $notice_id = md5( $notice );
  900. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  901. echo apply_filters( 'woocommerce_demo_store', '<p class="woocommerce-store-notice demo_store" data-notice-id="' . esc_attr( $notice_id ) . '" style="display:none;">' . wp_kses_post( $notice ) . ' <a href="#" class="woocommerce-store-notice__dismiss-link">' . esc_html__( 'Dismiss', 'woocommerce' ) . '</a></p>', $notice );
  902. }
  903. }
  904. /**
  905. * Loop
  906. */
  907. if ( ! function_exists( 'woocommerce_page_title' ) ) {
  908. /**
  909. * Page Title function.
  910. *
  911. * @param bool $echo Should echo title.
  912. * @return string
  913. */
  914. function woocommerce_page_title( $echo = true ) {
  915. if ( is_search() ) {
  916. /* translators: %s: search query */
  917. $page_title = sprintf( __( 'Search results: &ldquo;%s&rdquo;', 'woocommerce' ), get_search_query() );
  918. if ( get_query_var( 'paged' ) ) {
  919. /* translators: %s: page number */
  920. $page_title .= sprintf( __( '&nbsp;&ndash; Page %s', 'woocommerce' ), get_query_var( 'paged' ) );
  921. }
  922. } elseif ( is_tax() ) {
  923. $page_title = single_term_title( '', false );
  924. } else {
  925. $shop_page_id = wc_get_page_id( 'shop' );
  926. $page_title = get_the_title( $shop_page_id );
  927. }
  928. $page_title = apply_filters( 'woocommerce_page_title', $page_title );
  929. if ( $echo ) {
  930. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  931. echo $page_title;
  932. } else {
  933. return $page_title;
  934. }
  935. }
  936. }
  937. if ( ! function_exists( 'woocommerce_product_loop_start' ) ) {
  938. /**
  939. * Output the start of a product loop. By default this is a UL.
  940. *
  941. * @param bool $echo Should echo?.
  942. * @return string
  943. */
  944. function woocommerce_product_loop_start( $echo = true ) {
  945. ob_start();
  946. wc_set_loop_prop( 'loop', 0 );
  947. wc_get_template( 'loop/loop-start.php' );
  948. $loop_start = apply_filters( 'woocommerce_product_loop_start', ob_get_clean() );
  949. if ( $echo ) {
  950. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  951. echo $loop_start;
  952. } else {
  953. return $loop_start;
  954. }
  955. }
  956. }
  957. if ( ! function_exists( 'woocommerce_product_loop_end' ) ) {
  958. /**
  959. * Output the end of a product loop. By default this is a UL.
  960. *
  961. * @param bool $echo Should echo?.
  962. * @return string
  963. */
  964. function woocommerce_product_loop_end( $echo = true ) {
  965. ob_start();
  966. wc_get_template( 'loop/loop-end.php' );
  967. $loop_end = apply_filters( 'woocommerce_product_loop_end', ob_get_clean() );
  968. if ( $echo ) {
  969. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  970. echo $loop_end;
  971. } else {
  972. return $loop_end;
  973. }
  974. }
  975. }
  976. if ( ! function_exists( 'woocommerce_template_loop_product_title' ) ) {
  977. /**
  978. * Show the product title in the product loop. By default this is an H2.
  979. */
  980. function woocommerce_template_loop_product_title() {
  981. echo '<h2 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</h2>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  982. }
  983. }
  984. if ( ! function_exists( 'woocommerce_template_loop_category_title' ) ) {
  985. /**
  986. * Show the subcategory title in the product loop.
  987. *
  988. * @param object $category Category object.
  989. */
  990. function woocommerce_template_loop_category_title( $category ) {
  991. ?>
  992. <h2 class="woocommerce-loop-category__title">
  993. <?php
  994. echo esc_html( $category->name );
  995. if ( $category->count > 0 ) {
  996. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  997. echo apply_filters( 'woocommerce_subcategory_count_html', ' <mark class="count">(' . esc_html( $category->count ) . ')</mark>', $category );
  998. }
  999. ?>
  1000. </h2>
  1001. <?php
  1002. }
  1003. }
  1004. if ( ! function_exists( 'woocommerce_template_loop_product_link_open' ) ) {
  1005. /**
  1006. * Insert the opening anchor tag for products in the loop.
  1007. */
  1008. function woocommerce_template_loop_product_link_open() {
  1009. global $product;
  1010. $link = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );
  1011. echo '<a href="' . esc_url( $link ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
  1012. }
  1013. }
  1014. if ( ! function_exists( 'woocommerce_template_loop_product_link_close' ) ) {
  1015. /**
  1016. * Insert the closing anchor tag for products in the loop.
  1017. */
  1018. function woocommerce_template_loop_product_link_close() {
  1019. echo '</a>';
  1020. }
  1021. }
  1022. if ( ! function_exists( 'woocommerce_template_loop_category_link_open' ) ) {
  1023. /**
  1024. * Insert the opening anchor tag for categories in the loop.
  1025. *
  1026. * @param int|object|string $category Category ID, Object or String.
  1027. */
  1028. function woocommerce_template_loop_category_link_open( $category ) {
  1029. echo '<a href="' . esc_url( get_term_link( $category, 'product_cat' ) ) . '">';
  1030. }
  1031. }
  1032. if ( ! function_exists( 'woocommerce_template_loop_category_link_close' ) ) {
  1033. /**
  1034. * Insert the closing anchor tag for categories in the loop.
  1035. */
  1036. function woocommerce_template_loop_category_link_close() {
  1037. echo '</a>';
  1038. }
  1039. }
  1040. if ( ! function_exists( 'woocommerce_taxonomy_archive_description' ) ) {
  1041. /**
  1042. * Show an archive description on taxonomy archives.
  1043. */
  1044. function woocommerce_taxonomy_archive_description() {
  1045. if ( is_product_taxonomy() && 0 === absint( get_query_var( 'paged' ) ) ) {
  1046. $term = get_queried_object();
  1047. if ( $term && ! empty( $term->description ) ) {
  1048. echo '<div class="term-description">' . wc_format_content( wp_kses_post( $term->description ) ) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  1049. }
  1050. }
  1051. }
  1052. }
  1053. if ( ! function_exists( 'woocommerce_product_archive_description' ) ) {
  1054. /**
  1055. * Show a shop page description on product archives.
  1056. */
  1057. function woocommerce_product_archive_description() {
  1058. // Don't display the description on search results page.
  1059. if ( is_search() ) {
  1060. return;
  1061. }
  1062. if ( is_post_type_archive( 'product' ) && in_array( absint( get_query_var( 'paged' ) ), array( 0, 1 ), true ) ) {
  1063. $shop_page = get_post( wc_get_page_id( 'shop' ) );
  1064. if ( $shop_page ) {
  1065. $allowed_html = wp_kses_allowed_html( 'post' );
  1066. // This is needed for the search product block to work.
  1067. $allowed_html = array_merge(
  1068. $allowed_html,
  1069. array(
  1070. 'form' => array(
  1071. 'action' => true,
  1072. 'accept' => true,
  1073. 'accept-charset' => true,
  1074. 'enctype' => true,
  1075. 'method' => true,
  1076. 'name' => true,
  1077. 'target' => true,
  1078. ),
  1079. 'input' => array(
  1080. 'type' => true,
  1081. 'id' => true,
  1082. 'class' => true,
  1083. 'placeholder' => true,
  1084. 'name' => true,
  1085. 'value' => true,
  1086. ),
  1087. 'button' => array(
  1088. 'type' => true,
  1089. 'class' => true,
  1090. 'label' => true,
  1091. ),
  1092. 'svg' => array(
  1093. 'hidden' => true,
  1094. 'role' => true,
  1095. 'focusable' => true,
  1096. 'xmlns' => true,
  1097. 'width' => true,
  1098. 'height' => true,
  1099. 'viewbox' => true,
  1100. ),
  1101. 'path' => array(
  1102. 'd' => true,
  1103. ),
  1104. )
  1105. );
  1106. $description = wc_format_content( wp_kses( $shop_page->post_content, $allowed_html ) );
  1107. if ( $description ) {
  1108. echo '<div class="page-description">' . $description . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  1109. }
  1110. }
  1111. }
  1112. }
  1113. }
  1114. if ( ! function_exists( 'woocommerce_template_loop_add_to_cart' ) ) {
  1115. /**
  1116. * Get the add to cart template for the loop.
  1117. *
  1118. * @param array $args Arguments.
  1119. */
  1120. function woocommerce_template_loop_add_to_cart( $args = array() ) {
  1121. global $product;
  1122. if ( $product ) {
  1123. $defaults = array(
  1124. 'quantity' => 1,
  1125. 'class' => implode(
  1126. ' ',
  1127. array_filter(
  1128. array(
  1129. 'button',
  1130. 'product_type_' . $product->get_type(),
  1131. $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
  1132. $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
  1133. )
  1134. )
  1135. ),
  1136. 'attributes' => array(
  1137. 'data-product_id' => $product->get_id(),
  1138. 'data-product_sku' => $product->get_sku(),
  1139. 'aria-label' => $product->add_to_cart_description(),
  1140. 'rel' => 'nofollow',
  1141. ),
  1142. );
  1143. $args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );
  1144. if ( isset( $args['attributes']['aria-label'] ) ) {
  1145. $args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] );
  1146. }
  1147. wc_get_template( 'loop/add-to-cart.php', $args );
  1148. }
  1149. }
  1150. }
  1151. if ( ! function_exists( 'woocommerce_template_loop_product_thumbnail' ) ) {
  1152. /**
  1153. * Get the product thumbnail for the loop.
  1154. */
  1155. function woocommerce_template_loop_product_thumbnail() {
  1156. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  1157. echo woocommerce_get_product_thumbnail();
  1158. }
  1159. }
  1160. if ( ! function_exists( 'woocommerce_template_loop_price' ) ) {
  1161. /**
  1162. * Get the product price for the loop.
  1163. */
  1164. function woocommerce_template_loop_price() {
  1165. wc_get_template( 'loop/price.php' );
  1166. }
  1167. }
  1168. if ( ! function_exists( 'woocommerce_template_loop_rating' ) ) {
  1169. /**
  1170. * Display the average rating in the loop.
  1171. */
  1172. function woocommerce_template_loop_rating() {
  1173. wc_get_template( 'loop/rating.php' );
  1174. }
  1175. }
  1176. if ( ! function_exists( 'woocommerce_show_product_loop_sale_flash' ) ) {
  1177. /**
  1178. * Get the sale flash for the loop.
  1179. */
  1180. function woocommerce_show_product_loop_sale_flash() {
  1181. wc_get_template( 'loop/sale-flash.php' );
  1182. }
  1183. }
  1184. if ( ! function_exists( 'woocommerce_get_product_thumbnail' ) ) {
  1185. /**
  1186. * Get the product thumbnail, or the placeholder if not set.
  1187. *
  1188. * @param string $size (default: 'woocommerce_thumbnail').
  1189. * @param int $deprecated1 Deprecated since WooCommerce 2.0 (default: 0).
  1190. * @param int $deprecated2 Deprecated since WooCommerce 2.0 (default: 0).
  1191. * @return string
  1192. */
  1193. function woocommerce_get_product_thumbnail( $size = 'woocommerce_thumbnail', $deprecated1 = 0, $deprecated2 = 0 ) {
  1194. global $product;
  1195. $image_size = apply_filters( 'single_product_archive_thumbnail_size', $size );
  1196. return $product ? $product->get_image( $image_size ) : '';
  1197. }
  1198. }
  1199. if ( ! function_exists( 'woocommerce_result_count' ) ) {
  1200. /**
  1201. * Output the result count text (Showing x - x of x results).
  1202. */
  1203. function woocommerce_result_count() {
  1204. if ( ! wc_get_loop_prop( 'is_paginated' ) || ! woocommerce_products_will_display() ) {
  1205. return;
  1206. }
  1207. $args = array(
  1208. 'total' => wc_get_loop_prop( 'total' ),
  1209. 'per_page' => wc_get_loop_prop( 'per_page' ),
  1210. 'current' => wc_get_loop_prop( 'current_page' ),
  1211. );
  1212. wc_get_template( 'loop/result-count.php', $args );
  1213. }
  1214. }
  1215. if ( ! function_exists( 'woocommerce_catalog_ordering' ) ) {
  1216. /**
  1217. * Output the product sorting options.
  1218. */
  1219. function woocommerce_catalog_ordering() {
  1220. if ( ! wc_get_loop_prop( 'is_paginated' ) || ! woocommerce_products_will_display() ) {
  1221. return;
  1222. }
  1223. $show_default_orderby = 'menu_order' === apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby', 'menu_order' ) );
  1224. $catalog_orderby_options = apply_filters(
  1225. 'woocommerce_catalog_orderby',
  1226. array(
  1227. 'menu_order' => __( 'Default sorting', 'woocommerce' ),
  1228. 'popularity' => __( 'Sort by popularity', 'woocommerce' ),
  1229. 'rating' => __( 'Sort by average rating', 'woocommerce' ),
  1230. 'date' => __( 'Sort by latest', 'woocommerce' ),
  1231. 'price' => __( 'Sort by price: low to high', 'woocommerce' ),
  1232. 'price-desc' => __( 'Sort by price: high to low', 'woocommerce' ),
  1233. )
  1234. );
  1235. $default_orderby = wc_get_loop_prop( 'is_search' ) ? 'relevance' : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby', '' ) );
  1236. // phpcs:disable WordPress.Security.NonceVerification.Recommended
  1237. $orderby = isset( $_GET['orderby'] ) ? wc_clean( wp_unslash( $_GET['orderby'] ) ) : $default_orderby;
  1238. // phpcs:enable WordPress.Security.NonceVerification.Recommended
  1239. if ( wc_get_loop_prop( 'is_search' ) ) {
  1240. $catalog_orderby_options = array_merge( array( 'relevance' => __( 'Relevance', 'woocommerce' ) ), $catalog_orderby_options );
  1241. unset( $catalog_orderby_options['menu_order'] );
  1242. }
  1243. if ( ! $show_default_orderby ) {
  1244. unset( $catalog_orderby_options['menu_order'] );
  1245. }
  1246. if ( ! wc_review_ratings_enabled() ) {
  1247. unset( $catalog_orderby_options['rating'] );
  1248. }
  1249. if ( ! array_key_exists( $orderby, $catalog_orderby_options ) ) {
  1250. $orderby = current( array_keys( $catalog_orderby_options ) );
  1251. }
  1252. wc_get_template(
  1253. 'loop/orderby.php',
  1254. array(
  1255. 'catalog_orderby_options' => $catalog_orderby_options,
  1256. 'orderby' => $orderby,
  1257. 'show_default_orderby' => $show_default_orderby,
  1258. )
  1259. );
  1260. }
  1261. }
  1262. if ( ! function_exists( 'woocommerce_pagination' ) ) {
  1263. /**
  1264. * Output the pagination.
  1265. */
  1266. function woocommerce_pagination() {
  1267. if ( ! wc_get_loop_prop( 'is_paginated' ) || ! woocommerce_products_will_display() ) {
  1268. return;
  1269. }
  1270. $args = array(
  1271. 'total' => wc_get_loop_prop( 'total_pages' ),
  1272. 'current' => wc_get_loop_prop( 'current_page' ),
  1273. 'base' => esc_url_raw( add_query_arg( 'product-page', '%#%', false ) ),
  1274. 'format' => '?product-page=%#%',
  1275. );
  1276. if ( ! wc_get_loop_prop( 'is_shortcode' ) ) {
  1277. $args['format'] = '';
  1278. $args['base'] = esc_url_raw( str_replace( 999999999, '%#%', remove_query_arg( 'add-to-cart', get_pagenum_link( 999999999, false ) ) ) );
  1279. }
  1280. wc_get_template( 'loop/pagination.php', $args );
  1281. }
  1282. }
  1283. /**
  1284. * Single Product
  1285. */
  1286. if ( ! function_exists( 'woocommerce_show_product_images' ) ) {
  1287. /**
  1288. * Output the product image before the single product summary.
  1289. */
  1290. function woocommerce_show_product_images() {
  1291. wc_get_template( 'single-product/product-image.php' );
  1292. }
  1293. }
  1294. if ( ! function_exists( 'woocommerce_show_product_thumbnails' ) ) {
  1295. /**
  1296. * Output the product thumbnails.
  1297. */
  1298. function woocommerce_show_product_thumbnails() {
  1299. wc_get_template( 'single-product/product-thumbnails.php' );
  1300. }
  1301. }
  1302. /**
  1303. * Get HTML for a gallery image.
  1304. *
  1305. * Hooks: woocommerce_gallery_thumbnail_size, woocommerce_gallery_image_size and woocommerce_gallery_full_size accept name based image sizes, or an array of width/height values.
  1306. *
  1307. * @since 3.3.2
  1308. * @param int $attachment_id Attachment ID.
  1309. * @param bool $main_image Is this the main image or a thumbnail?.
  1310. * @return string
  1311. */
  1312. function wc_get_gallery_image_html( $attachment_id, $main_image = false ) {
  1313. $flexslider = (bool) apply_filters( 'woocommerce_single_product_flexslider_enabled', get_theme_support( 'wc-product-gallery-slider' ) );
  1314. $gallery_thumbnail = wc_get_image_size( 'gallery_thumbnail' );
  1315. $thumbnail_size = apply_filters( 'woocommerce_gallery_thumbnail_size', array( $gallery_thumbnail['width'], $gallery_thumbnail['height'] ) );
  1316. $image_size = apply_filters( 'woocommerce_gallery_image_size', $flexslider || $main_image ? 'woocommerce_single' : $thumbnail_size );
  1317. $full_size = apply_filters( 'woocommerce_gallery_full_size', apply_filters( 'woocommerce_product_thumbnails_large_size', 'full' ) );
  1318. $thumbnail_src = wp_get_attachment_image_src( $attachment_id, $thumbnail_size );
  1319. $full_src = wp_get_attachment_image_src( $attachment_id, $full_size );
  1320. $alt_text = trim( wp_strip_all_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) );
  1321. $image = wp_get_attachment_image(
  1322. $attachment_id,
  1323. $image_size,
  1324. false,
  1325. apply_filters(
  1326. 'woocommerce_gallery_image_html_attachment_image_params',
  1327. array(
  1328. 'title' => _wp_specialchars( get_post_field( 'post_title', $attachment_id ), ENT_QUOTES, 'UTF-8', true ),
  1329. 'data-caption' => _wp_specialchars( get_post_field( 'post_excerpt', $attachment_id ), ENT_QUOTES, 'UTF-8', true ),
  1330. 'data-src' => esc_url( $full_src[0] ),
  1331. 'data-large_image' => esc_url( $full_src[0] ),
  1332. 'data-large_image_width' => esc_attr( $full_src[1] ),
  1333. 'data-large_image_height' => esc_attr( $full_src[2] ),
  1334. 'class' => esc_attr( $main_image ? 'wp-post-image' : '' ),
  1335. ),
  1336. $attachment_id,
  1337. $image_size,
  1338. $main_image
  1339. )
  1340. );
  1341. return '<div data-thumb="' . esc_url( $thumbnail_src[0] ) . '" data-thumb-alt="' . esc_attr( $alt_text ) . '" class="woocommerce-product-gallery__image"><a href="' . esc_url( $full_src[0] ) . '">' . $image . '</a></div>';
  1342. }
  1343. if ( ! function_exists( 'woocommerce_output_product_data_tabs' ) ) {
  1344. /**
  1345. * Output the product tabs.
  1346. */
  1347. function woocommerce_output_product_data_tabs() {
  1348. wc_get_template( 'single-product/tabs/tabs.php' );
  1349. }
  1350. }
  1351. if ( ! function_exists( 'woocommerce_template_single_title' ) ) {
  1352. /**
  1353. * Output the product title.
  1354. */
  1355. function woocommerce_template_single_title() {
  1356. wc_get_template( 'single-product/title.php' );
  1357. }
  1358. }
  1359. if ( ! function_exists( 'woocommerce_template_single_rating' ) ) {
  1360. /**
  1361. * Output the product rating.
  1362. */
  1363. function woocommerce_template_single_rating() {
  1364. if ( post_type_supports( 'product', 'comments' ) ) {
  1365. wc_get_template( 'single-product/rating.php' );
  1366. }
  1367. }
  1368. }
  1369. if ( ! function_exists( 'woocommerce_template_single_price' ) ) {
  1370. /**
  1371. * Output the product price.
  1372. */
  1373. function woocommerce_template_single_price() {
  1374. wc_get_template( 'single-product/price.php' );
  1375. }
  1376. }
  1377. if ( ! function_exists( 'woocommerce_template_single_excerpt' ) ) {
  1378. /**
  1379. * Output the product short description (excerpt).
  1380. */
  1381. function woocommerce_template_single_excerpt() {
  1382. wc_get_template( 'single-product/short-description.php' );
  1383. }
  1384. }
  1385. if ( ! function_exists( 'woocommerce_template_single_meta' ) ) {
  1386. /**
  1387. * Output the product meta.
  1388. */
  1389. function woocommerce_template_single_meta() {
  1390. wc_get_template( 'single-product/meta.php' );
  1391. }
  1392. }
  1393. if ( ! function_exists( 'woocommerce_template_single_sharing' ) ) {
  1394. /**
  1395. * Output the product sharing.
  1396. */
  1397. function woocommerce_template_single_sharing() {
  1398. wc_get_template( 'single-product/share.php' );
  1399. }
  1400. }
  1401. if ( ! function_exists( 'woocommerce_show_product_sale_flash' ) ) {
  1402. /**
  1403. * Output the product sale flash.
  1404. */
  1405. function woocommerce_show_product_sale_flash() {
  1406. wc_get_template( 'single-product/sale-flash.php' );
  1407. }
  1408. }
  1409. if ( ! function_exists( 'woocommerce_template_single_add_to_cart' ) ) {
  1410. /**
  1411. * Trigger the single product add to cart action.
  1412. */
  1413. function woocommerce_template_single_add_to_cart() {
  1414. global $product;
  1415. do_action( 'woocommerce_' . $product->get_type() . '_add_to_cart' );
  1416. }
  1417. }
  1418. if ( ! function_exists( 'woocommerce_simple_add_to_cart' ) ) {
  1419. /**
  1420. * Output the simple product add to cart area.
  1421. */
  1422. function woocommerce_simple_add_to_cart() {
  1423. wc_get_template( 'single-product/add-to-cart/simple.php' );
  1424. }
  1425. }
  1426. if ( ! function_exists( 'woocommerce_grouped_add_to_cart' ) ) {
  1427. /**
  1428. * Output the grouped product add to cart area.
  1429. */
  1430. function woocommerce_grouped_add_to_cart() {
  1431. global $product;
  1432. $products = array_filter( array_map( 'wc_get_product', $product->get_children() ), 'wc_products_array_filter_visible_grouped' );
  1433. if ( $products ) {
  1434. wc_get_template(
  1435. 'single-product/add-to-cart/grouped.php',
  1436. array(
  1437. 'grouped_product' => $product,
  1438. 'grouped_products' => $products,
  1439. 'quantites_required' => false,
  1440. )
  1441. );
  1442. }
  1443. }
  1444. }
  1445. if ( ! function_exists( 'woocommerce_variable_add_to_cart' ) ) {
  1446. /**
  1447. * Output the variable product add to cart area.
  1448. */
  1449. function woocommerce_variable_add_to_cart() {
  1450. global $product;
  1451. // Enqueue variation scripts.
  1452. wp_enqueue_script( 'wc-add-to-cart-variation' );
  1453. // Get Available variations?
  1454. $get_variations = count( $product->get_children() ) <= apply_filters( 'woocommerce_ajax_variation_threshold', 30, $product );
  1455. // Load the template.
  1456. wc_get_template(
  1457. 'single-product/add-to-cart/variable.php',
  1458. array(
  1459. 'available_variations' => $get_variations ? $product->get_available_variations() : false,
  1460. 'attributes' => $product->get_variation_attributes(),
  1461. 'selected_attributes' => $product->get_default_attributes(),
  1462. )
  1463. );
  1464. }
  1465. }
  1466. if ( ! function_exists( 'woocommerce_external_add_to_cart' ) ) {
  1467. /**
  1468. * Output the external product add to cart area.
  1469. */
  1470. function woocommerce_external_add_to_cart() {
  1471. global $product;
  1472. if ( ! $product->add_to_cart_url() ) {
  1473. return;
  1474. }
  1475. wc_get_template(
  1476. 'single-product/add-to-cart/external.php',
  1477. array(
  1478. 'product_url' => $product->add_to_cart_url(),
  1479. 'button_text' => $product->single_add_to_cart_text(),
  1480. )
  1481. );
  1482. }
  1483. }
  1484. if ( ! function_exists( 'woocommerce_quantity_input' ) ) {
  1485. /**
  1486. * Output the quantity input for add to cart forms.
  1487. *
  1488. * @param array $args Args for the input.
  1489. * @param WC_Product|null $product Product.
  1490. * @param boolean $echo Whether to return or echo|string.
  1491. *
  1492. * @return string
  1493. */
  1494. function woocommerce_quantity_input( $args = array(), $product = null, $echo = true ) {
  1495. if ( is_null( $product ) ) {
  1496. $product = $GLOBALS['product'];
  1497. }
  1498. $defaults = array(
  1499. 'input_id' => uniqid( 'quantity_' ),
  1500. 'input_name' => 'quantity',
  1501. 'input_value' => '1',
  1502. 'classes' => apply_filters( 'woocommerce_quantity_input_classes', array( 'input-text', 'qty', 'text' ), $product ),
  1503. 'max_value' => apply_filters( 'woocommerce_quantity_input_max', -1, $product ),
  1504. 'min_value' => apply_filters( 'woocommerce_quantity_input_min', 0, $product ),
  1505. 'step' => apply_filters( 'woocommerce_quantity_input_step', 1, $product ),
  1506. 'pattern' => apply_filters( 'woocommerce_quantity_input_pattern', has_filter( 'woocommerce_stock_amount', 'intval' ) ? '[0-9]*' : '' ),
  1507. 'inputmode' => apply_filters( 'woocommerce_quantity_input_inputmode', has_filter( 'woocommerce_stock_amount', 'intval' ) ? 'numeric' : '' ),
  1508. 'product_name' => $product ? $product->get_title() : '',
  1509. 'placeholder' => apply_filters( 'woocommerce_quantity_input_placeholder', '', $product ),
  1510. );
  1511. $args = apply_filters( 'woocommerce_quantity_input_args', wp_parse_args( $args, $defaults ), $product );
  1512. // Apply sanity to min/max args - min cannot be lower than 0.
  1513. $args['min_value'] = max( $args['min_value'], 0 );
  1514. $args['max_value'] = 0 < $args['max_value'] ? $args['max_value'] : '';
  1515. // Max cannot be lower than min if defined.
  1516. if ( '' !== $args['max_value'] && $args['max_value'] < $args['min_value'] ) {
  1517. $args['max_value'] = $args['min_value'];
  1518. }
  1519. ob_start();
  1520. wc_get_template( 'global/quantity-input.php', $args );
  1521. if ( $echo ) {
  1522. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  1523. echo ob_get_clean();
  1524. } else {
  1525. return ob_get_clean();
  1526. }
  1527. }
  1528. }
  1529. if ( ! function_exists( 'woocommerce_product_description_tab' ) ) {
  1530. /**
  1531. * Output the description tab content.
  1532. */
  1533. function woocommerce_product_description_tab() {
  1534. wc_get_template( 'single-product/tabs/description.php' );
  1535. }
  1536. }
  1537. if ( ! function_exists( 'woocommerce_product_additional_information_tab' ) ) {
  1538. /**
  1539. * Output the attributes tab content.
  1540. */
  1541. function woocommerce_product_additional_information_tab() {
  1542. wc_get_template( 'single-product/tabs/additional-information.php' );
  1543. }
  1544. }
  1545. if ( ! function_exists( 'woocommerce_default_product_tabs' ) ) {
  1546. /**
  1547. * Add default product tabs to product pages.
  1548. *
  1549. * @param array $tabs Array of tabs.
  1550. * @return array
  1551. */
  1552. function woocommerce_default_product_tabs( $tabs = array() ) {
  1553. global $product, $post;
  1554. // Description tab - shows product content.
  1555. if ( $post->post_content ) {
  1556. $tabs['description'] = array(
  1557. 'title' => __( 'Description', 'woocommerce' ),
  1558. 'priority' => 10,
  1559. 'callback' => 'woocommerce_product_description_tab',
  1560. );
  1561. }
  1562. // Additional information tab - shows attributes.
  1563. if ( $product && ( $product->has_attributes() || apply_filters( 'wc_product_enable_dimensions_display', $product->has_weight() || $product->has_dimensions() ) ) ) {
  1564. $tabs['additional_information'] = array(
  1565. 'title' => __( 'Additional information', 'woocommerce' ),
  1566. 'priority' => 20,
  1567. 'callback' => 'woocommerce_product_additional_information_tab',
  1568. );
  1569. }
  1570. // Reviews tab - shows comments.
  1571. if ( comments_open() ) {
  1572. $tabs['reviews'] = array(
  1573. /* translators: %s: reviews count */
  1574. 'title' => sprintf( __( 'Reviews (%d)', 'woocommerce' ), $product->get_review_count() ),
  1575. 'priority' => 30,
  1576. 'callback' => 'comments_template',
  1577. );
  1578. }
  1579. return $tabs;
  1580. }
  1581. }
  1582. if ( ! function_exists( 'woocommerce_sort_product_tabs' ) ) {
  1583. /**
  1584. * Sort tabs by priority.
  1585. *
  1586. * @param array $tabs Array of tabs.
  1587. * @return array
  1588. */
  1589. function woocommerce_sort_product_tabs( $tabs = array() ) {
  1590. // Make sure the $tabs parameter is an array.
  1591. if ( ! is_array( $tabs ) ) {
  1592. // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
  1593. trigger_error( 'Function woocommerce_sort_product_tabs() expects an array as the first parameter. Defaulting to empty array.' );
  1594. $tabs = array();
  1595. }
  1596. // Re-order tabs by priority.
  1597. if ( ! function_exists( '_sort_priority_callback' ) ) {
  1598. /**
  1599. * Sort Priority Callback Function
  1600. *
  1601. * @param array $a Comparison A.
  1602. * @param array $b Comparison B.
  1603. * @return bool
  1604. */
  1605. function _sort_priority_callback( $a, $b ) {
  1606. if ( ! isset( $a['priority'], $b['priority'] ) || $a['priority'] === $b['priority'] ) {
  1607. return 0;
  1608. }
  1609. return ( $a['priority'] < $b['priority'] ) ? -1 : 1;
  1610. }
  1611. }
  1612. uasort( $tabs, '_sort_priority_callback' );
  1613. return $tabs;
  1614. }
  1615. }
  1616. if ( ! function_exists( 'woocommerce_comments' ) ) {
  1617. /**
  1618. * Output the Review comments template.
  1619. *
  1620. * @param WP_Comment $comment Comment object.
  1621. * @param array $args Arguments.
  1622. * @param int $depth Depth.
  1623. */
  1624. function woocommerce_comments( $comment, $args, $depth ) {
  1625. // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
  1626. $GLOBALS['comment'] = $comment;
  1627. wc_get_template(
  1628. 'single-product/review.php',
  1629. array(
  1630. 'comment' => $comment,
  1631. 'args' => $args,
  1632. 'depth' => $depth,
  1633. )
  1634. );
  1635. }
  1636. }
  1637. if ( ! function_exists( 'woocommerce_review_display_gravatar' ) ) {
  1638. /**
  1639. * Display the review authors gravatar
  1640. *
  1641. * @param array $comment WP_Comment.
  1642. * @return void
  1643. */
  1644. function woocommerce_review_display_gravatar( $comment ) {
  1645. echo get_avatar( $comment, apply_filters( 'woocommerce_review_gravatar_size', '60' ), '' );
  1646. }
  1647. }
  1648. if ( ! function_exists( 'woocommerce_review_display_rating' ) ) {
  1649. /**
  1650. * Display the reviewers star rating
  1651. *
  1652. * @return void
  1653. */
  1654. function woocommerce_review_display_rating() {
  1655. if ( post_type_supports( 'product', 'comments' ) ) {
  1656. wc_get_template( 'single-product/review-rating.php' );
  1657. }
  1658. }
  1659. }
  1660. if ( ! function_exists( 'woocommerce_review_display_meta' ) ) {
  1661. /**
  1662. * Display the review authors meta (name, verified owner, review date)
  1663. *
  1664. * @return void
  1665. */
  1666. function woocommerce_review_display_meta() {
  1667. wc_get_template( 'single-product/review-meta.php' );
  1668. }
  1669. }
  1670. if ( ! function_exists( 'woocommerce_review_display_comment_text' ) ) {
  1671. /**
  1672. * Display the review content.
  1673. */
  1674. function woocommerce_review_display_comment_text() {
  1675. echo '<div class="description">';
  1676. comment_text();
  1677. echo '</div>';
  1678. }
  1679. }
  1680. if ( ! function_exists( 'woocommerce_output_related_products' ) ) {
  1681. /**
  1682. * Output the related products.
  1683. */
  1684. function woocommerce_output_related_products() {
  1685. $args = array(
  1686. 'posts_per_page' => 4,
  1687. 'columns' => 4,
  1688. 'orderby' => 'rand', // @codingStandardsIgnoreLine.
  1689. );
  1690. woocommerce_related_products( apply_filters( 'woocommerce_output_related_products_args', $args ) );
  1691. }
  1692. }
  1693. if ( ! function_exists( 'woocommerce_related_products' ) ) {
  1694. /**
  1695. * Output the related products.
  1696. *
  1697. * @param array $args Provided arguments.
  1698. */
  1699. function woocommerce_related_products( $args = array() ) {
  1700. global $product;
  1701. if ( ! $product ) {
  1702. return;
  1703. }
  1704. $defaults = array(
  1705. 'posts_per_page' => 2,
  1706. 'columns' => 2,
  1707. 'orderby' => 'rand', // @codingStandardsIgnoreLine.
  1708. 'order' => 'desc',
  1709. );
  1710. $args = wp_parse_args( $args, $defaults );
  1711. // Get visible related products then sort them at random.
  1712. $args['related_products'] = array_filter( array_map( 'wc_get_product', wc_get_related_products( $product->get_id(), $args['posts_per_page'], $product->get_upsell_ids() ) ), 'wc_products_array_filter_visible' );
  1713. // Handle orderby.
  1714. $args['related_products'] = wc_products_array_orderby( $args['related_products'], $args['orderby'], $args['order'] );
  1715. // Set global loop values.
  1716. wc_set_loop_prop( 'name', 'related' );
  1717. wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_related_products_columns', $args['columns'] ) );
  1718. wc_get_template( 'single-product/related.php', $args );
  1719. }
  1720. }
  1721. if ( ! function_exists( 'woocommerce_upsell_display' ) ) {
  1722. /**
  1723. * Output product up sells.
  1724. *
  1725. * @param int $limit (default: -1).
  1726. * @param int $columns (default: 4).
  1727. * @param string $orderby Supported values - rand, title, ID, date, modified, menu_order, price.
  1728. * @param string $order Sort direction.
  1729. */
  1730. function woocommerce_upsell_display( $limit = '-1', $columns = 4, $orderby = 'rand', $order = 'desc' ) {
  1731. global $product;
  1732. if ( ! $product ) {
  1733. return;
  1734. }
  1735. // Handle the legacy filter which controlled posts per page etc.
  1736. $args = apply_filters(
  1737. 'woocommerce_upsell_display_args',
  1738. array(
  1739. 'posts_per_page' => $limit,
  1740. 'orderby' => $orderby,
  1741. 'order' => $order,
  1742. 'columns' => $columns,
  1743. )
  1744. );
  1745. wc_set_loop_prop( 'name', 'up-sells' );
  1746. wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_upsells_columns', isset( $args['columns'] ) ? $args['columns'] : $columns ) );
  1747. $orderby = apply_filters( 'woocommerce_upsells_orderby', isset( $args['orderby'] ) ? $args['orderby'] : $orderby );
  1748. $order = apply_filters( 'woocommerce_upsells_order', isset( $args['order'] ) ? $args['order'] : $order );
  1749. $limit = apply_filters( 'woocommerce_upsells_total', isset( $args['posts_per_page'] ) ? $args['posts_per_page'] : $limit );
  1750. // Get visible upsells then sort them at random, then limit result set.
  1751. $upsells = wc_products_array_orderby( array_filter( array_map( 'wc_get_product', $product->get_upsell_ids() ), 'wc_products_array_filter_visible' ), $orderby, $order );
  1752. $upsells = $limit > 0 ? array_slice( $upsells, 0, $limit ) : $upsells;
  1753. wc_get_template(
  1754. 'single-product/up-sells.php',
  1755. array(
  1756. 'upsells' => $upsells,
  1757. // Not used now, but used in previous version of up-sells.php.
  1758. 'posts_per_page' => $limit,
  1759. 'orderby' => $orderby,
  1760. 'columns' => $columns,
  1761. )
  1762. );
  1763. }
  1764. }
  1765. /** Cart */
  1766. if ( ! function_exists( 'woocommerce_shipping_calculator' ) ) {
  1767. /**
  1768. * Output the cart shipping calculator.
  1769. *
  1770. * @param string $button_text Text for the shipping calculation toggle.
  1771. */
  1772. function woocommerce_shipping_calculator( $button_text = '' ) {
  1773. if ( 'no' === get_option( 'woocommerce_enable_shipping_calc' ) || ! WC()->cart->needs_shipping() ) {
  1774. return;
  1775. }
  1776. wp_enqueue_script( 'wc-country-select' );
  1777. wc_get_template(
  1778. 'cart/shipping-calculator.php',
  1779. array(
  1780. 'button_text' => $button_text,
  1781. )
  1782. );
  1783. }
  1784. }
  1785. if ( ! function_exists( 'woocommerce_cart_totals' ) ) {
  1786. /**
  1787. * Output the cart totals.
  1788. */
  1789. function woocommerce_cart_totals() {
  1790. if ( is_checkout() ) {
  1791. return;
  1792. }
  1793. wc_get_template( 'cart/cart-totals.php' );
  1794. }
  1795. }
  1796. if ( ! function_exists( 'woocommerce_cross_sell_display' ) ) {
  1797. /**
  1798. * Output the cart cross-sells.
  1799. *
  1800. * @param int $limit (default: 2).
  1801. * @param int $columns (default: 2).
  1802. * @param string $orderby (default: 'rand').
  1803. * @param string $order (default: 'desc').
  1804. */
  1805. function woocommerce_cross_sell_display( $limit = 2, $columns = 2, $orderby = 'rand', $order = 'desc' ) {
  1806. if ( is_checkout() ) {
  1807. return;
  1808. }
  1809. // Get visible cross sells then sort them at random.
  1810. $cross_sells = array_filter( array_map( 'wc_get_product', WC()->cart->get_cross_sells() ), 'wc_products_array_filter_visible' );
  1811. wc_set_loop_prop( 'name', 'cross-sells' );
  1812. wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_cross_sells_columns', $columns ) );
  1813. // Handle orderby and limit results.
  1814. $orderby = apply_filters( 'woocommerce_cross_sells_orderby', $orderby );
  1815. $order = apply_filters( 'woocommerce_cross_sells_order', $order );
  1816. $cross_sells = wc_products_array_orderby( $cross_sells, $orderby, $order );
  1817. $limit = apply_filters( 'woocommerce_cross_sells_total', $limit );
  1818. $cross_sells = $limit > 0 ? array_slice( $cross_sells, 0, $limit ) : $cross_sells;
  1819. wc_get_template(
  1820. 'cart/cross-sells.php',
  1821. array(
  1822. 'cross_sells' => $cross_sells,
  1823. // Not used now, but used in previous version of up-sells.php.
  1824. 'posts_per_page' => $limit,
  1825. 'orderby' => $orderby,
  1826. 'columns' => $columns,
  1827. )
  1828. );
  1829. }
  1830. }
  1831. if ( ! function_exists( 'woocommerce_button_proceed_to_checkout' ) ) {
  1832. /**
  1833. * Output the proceed to checkout button.
  1834. */
  1835. function woocommerce_button_proceed_to_checkout() {
  1836. wc_get_template( 'cart/proceed-to-checkout-button.php' );
  1837. }
  1838. }
  1839. if ( ! function_exists( 'woocommerce_widget_shopping_cart_button_view_cart' ) ) {
  1840. /**
  1841. * Output the view cart button.
  1842. */
  1843. function woocommerce_widget_shopping_cart_button_view_cart() {
  1844. echo '<a href="' . esc_url( wc_get_cart_url() ) . '" class="button wc-forward">' . esc_html__( 'View cart', 'woocommerce' ) . '</a>';
  1845. }
  1846. }
  1847. if ( ! function_exists( 'woocommerce_widget_shopping_cart_proceed_to_checkout' ) ) {
  1848. /**
  1849. * Output the proceed to checkout button.
  1850. */
  1851. function woocommerce_widget_shopping_cart_proceed_to_checkout() {
  1852. echo '<a href="' . esc_url( wc_get_checkout_url() ) . '" class="button checkout wc-forward">' . esc_html__( 'Checkout', 'woocommerce' ) . '</a>';
  1853. }
  1854. }
  1855. if ( ! function_exists( 'woocommerce_widget_shopping_cart_subtotal' ) ) {
  1856. /**
  1857. * Output to view cart subtotal.
  1858. *
  1859. * @since 3.7.0
  1860. */
  1861. function woocommerce_widget_shopping_cart_subtotal() {
  1862. echo '<strong>' . esc_html__( 'Subtotal:', 'woocommerce' ) . '</strong> ' . WC()->cart->get_cart_subtotal(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  1863. }
  1864. }
  1865. /** Mini-Cart */
  1866. if ( ! function_exists( 'woocommerce_mini_cart' ) ) {
  1867. /**
  1868. * Output the Mini-cart - used by cart widget.
  1869. *
  1870. * @param array $args Arguments.
  1871. */
  1872. function woocommerce_mini_cart( $args = array() ) {
  1873. $defaults = array(
  1874. 'list_class' => '',
  1875. );
  1876. $args = wp_parse_args( $args, $defaults );
  1877. wc_get_template( 'cart/mini-cart.php', $args );
  1878. }
  1879. }
  1880. /** Login */
  1881. if ( ! function_exists( 'woocommerce_login_form' ) ) {
  1882. /**
  1883. * Output the WooCommerce Login Form.
  1884. *
  1885. * @param array $args Arguments.
  1886. */
  1887. function woocommerce_login_form( $args = array() ) {
  1888. $defaults = array(
  1889. 'message' => '',
  1890. 'redirect' => '',
  1891. 'hidden' => false,
  1892. );
  1893. $args = wp_parse_args( $args, $defaults );
  1894. wc_get_template( 'global/form-login.php', $args );
  1895. }
  1896. }
  1897. if ( ! function_exists( 'woocommerce_checkout_login_form' ) ) {
  1898. /**
  1899. * Output the WooCommerce Checkout Login Form.
  1900. */
  1901. function woocommerce_checkout_login_form() {
  1902. wc_get_template(
  1903. 'checkout/form-login.php',
  1904. array(
  1905. 'checkout' => WC()->checkout(),
  1906. )
  1907. );
  1908. }
  1909. }
  1910. if ( ! function_exists( 'woocommerce_breadcrumb' ) ) {
  1911. /**
  1912. * Output the WooCommerce Breadcrumb.
  1913. *
  1914. * @param array $args Arguments.
  1915. */
  1916. function woocommerce_breadcrumb( $args = array() ) {
  1917. $args = wp_parse_args(
  1918. $args,
  1919. apply_filters(
  1920. 'woocommerce_breadcrumb_defaults',
  1921. array(
  1922. 'delimiter' => '&nbsp;&#47;&nbsp;',
  1923. 'wrap_before' => '<nav class="woocommerce-breadcrumb">',
  1924. 'wrap_after' => '</nav>',
  1925. 'before' => '',
  1926. 'after' => '',
  1927. 'home' => _x( 'Home', 'breadcrumb', 'woocommerce' ),
  1928. )
  1929. )
  1930. );
  1931. $breadcrumbs = new WC_Breadcrumb();
  1932. if ( ! empty( $args['home'] ) ) {
  1933. $breadcrumbs->add_crumb( $args['home'], apply_filters( 'woocommerce_breadcrumb_home_url', home_url() ) );
  1934. }
  1935. $args['breadcrumb'] = $breadcrumbs->generate();
  1936. /**
  1937. * WooCommerce Breadcrumb hook
  1938. *
  1939. * @hooked WC_Structured_Data::generate_breadcrumblist_data() - 10
  1940. */
  1941. do_action( 'woocommerce_breadcrumb', $breadcrumbs, $args );
  1942. wc_get_template( 'global/breadcrumb.php', $args );
  1943. }
  1944. }
  1945. if ( ! function_exists( 'woocommerce_order_review' ) ) {
  1946. /**
  1947. * Output the Order review table for the checkout.
  1948. *
  1949. * @param bool $deprecated Deprecated param.
  1950. */
  1951. function woocommerce_order_review( $deprecated = false ) {
  1952. wc_get_template(
  1953. 'checkout/review-order.php',
  1954. array(
  1955. 'checkout' => WC()->checkout(),
  1956. )
  1957. );
  1958. }
  1959. }
  1960. if ( ! function_exists( 'woocommerce_checkout_payment' ) ) {
  1961. /**
  1962. * Output the Payment Methods on the checkout.
  1963. */
  1964. function woocommerce_checkout_payment() {
  1965. if ( WC()->cart->needs_payment() ) {
  1966. $available_gateways = WC()->payment_gateways()->get_available_payment_gateways();
  1967. WC()->payment_gateways()->set_current_gateway( $available_gateways );
  1968. } else {
  1969. $available_gateways = array();
  1970. }
  1971. wc_get_template(
  1972. 'checkout/payment.php',
  1973. array(
  1974. 'checkout' => WC()->checkout(),
  1975. 'available_gateways' => $available_gateways,
  1976. 'order_button_text' => apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) ),
  1977. )
  1978. );
  1979. }
  1980. }
  1981. if ( ! function_exists( 'woocommerce_checkout_coupon_form' ) ) {
  1982. /**
  1983. * Output the Coupon form for the checkout.
  1984. */
  1985. function woocommerce_checkout_coupon_form() {
  1986. if ( is_user_logged_in() || WC()->checkout()->is_registration_enabled() || ! WC()->checkout()->is_registration_required() ) {
  1987. wc_get_template(
  1988. 'checkout/form-coupon.php',
  1989. array(
  1990. 'checkout' => WC()->checkout(),
  1991. )
  1992. );
  1993. }
  1994. }
  1995. }
  1996. if ( ! function_exists( 'woocommerce_products_will_display' ) ) {
  1997. /**
  1998. * Check if we will be showing products or not (and not sub-categories only).
  1999. *
  2000. * @return bool
  2001. */
  2002. function woocommerce_products_will_display() {
  2003. $display_type = woocommerce_get_loop_display_mode();
  2004. return 0 < wc_get_loop_prop( 'total', 0 ) && 'subcategories' !== $display_type;
  2005. }
  2006. }
  2007. if ( ! function_exists( 'woocommerce_get_loop_display_mode' ) ) {
  2008. /**
  2009. * See what is going to display in the loop.
  2010. *
  2011. * @since 3.3.0
  2012. * @return string Either products, subcategories, or both, based on current page.
  2013. */
  2014. function woocommerce_get_loop_display_mode() {
  2015. // Only return products when filtering things.
  2016. if ( wc_get_loop_prop( 'is_search' ) || wc_get_loop_prop( 'is_filtered' ) ) {
  2017. return 'products';
  2018. }
  2019. $parent_id = 0;
  2020. $display_type = '';
  2021. if ( is_shop() ) {
  2022. $display_type = get_option( 'woocommerce_shop_page_display', '' );
  2023. } elseif ( is_product_category() ) {
  2024. $parent_id = get_queried_object_id();
  2025. $display_type = get_term_meta( $parent_id, 'display_type', true );
  2026. $display_type = '' === $display_type ? get_option( 'woocommerce_category_archive_display', '' ) : $display_type;
  2027. }
  2028. if ( ( ! is_shop() || 'subcategories' !== $display_type ) && 1 < wc_get_loop_prop( 'current_page' ) ) {
  2029. return 'products';
  2030. }
  2031. // Ensure valid value.
  2032. if ( '' === $display_type || ! in_array( $display_type, array( 'products', 'subcategories', 'both' ), true ) ) {
  2033. $display_type = 'products';
  2034. }
  2035. // If we're showing categories, ensure we actually have something to show.
  2036. if ( in_array( $display_type, array( 'subcategories', 'both' ), true ) ) {
  2037. $subcategories = woocommerce_get_product_subcategories( $parent_id );
  2038. if ( empty( $subcategories ) ) {
  2039. $display_type = 'products';
  2040. }
  2041. }
  2042. return $display_type;
  2043. }
  2044. }
  2045. if ( ! function_exists( 'woocommerce_maybe_show_product_subcategories' ) ) {
  2046. /**
  2047. * Maybe display categories before, or instead of, a product loop.
  2048. *
  2049. * @since 3.3.0
  2050. * @param string $loop_html HTML.
  2051. * @return string
  2052. */
  2053. function woocommerce_maybe_show_product_subcategories( $loop_html = '' ) {
  2054. if ( wc_get_loop_prop( 'is_shortcode' ) && ! WC_Template_Loader::in_content_filter() ) {
  2055. return $loop_html;
  2056. }
  2057. $display_type = woocommerce_get_loop_display_mode();
  2058. // If displaying categories, append to the loop.
  2059. if ( 'subcategories' === $display_type || 'both' === $display_type ) {
  2060. ob_start();
  2061. woocommerce_output_product_categories(
  2062. array(
  2063. 'parent_id' => is_product_category() ? get_queried_object_id() : 0,
  2064. )
  2065. );
  2066. $loop_html .= ob_get_clean();
  2067. if ( 'subcategories' === $display_type ) {
  2068. wc_set_loop_prop( 'total', 0 );
  2069. // This removes pagination and products from display for themes not using wc_get_loop_prop in their product loops. @todo Remove in future major version.
  2070. global $wp_query;
  2071. if ( $wp_query->is_main_query() ) {
  2072. $wp_query->post_count = 0;
  2073. $wp_query->max_num_pages = 0;
  2074. }
  2075. }
  2076. }
  2077. return $loop_html;
  2078. }
  2079. }
  2080. if ( ! function_exists( 'woocommerce_product_subcategories' ) ) {
  2081. /**
  2082. * This is a legacy function which used to check if we needed to display subcats and then output them. It was called by templates.
  2083. *
  2084. * From 3.3 onwards this is all handled via hooks and the woocommerce_maybe_show_product_subcategories function.
  2085. *
  2086. * Since some templates have not updated compatibility, to avoid showing incorrect categories this function has been deprecated and will
  2087. * return nothing. Replace usage with woocommerce_output_product_categories to render the category list manually.
  2088. *
  2089. * This is a legacy function which also checks if things should display.
  2090. * Themes no longer need to call these functions. It's all done via hooks.
  2091. *
  2092. * @deprecated 3.3.1 @todo Add a notice in a future version.
  2093. * @param array $args Arguments.
  2094. * @return null|boolean
  2095. */
  2096. function woocommerce_product_subcategories( $args = array() ) {
  2097. $defaults = array(
  2098. 'before' => '',
  2099. 'after' => '',
  2100. 'force_display' => false,
  2101. );
  2102. $args = wp_parse_args( $args, $defaults );
  2103. if ( $args['force_display'] ) {
  2104. // We can still render if display is forced.
  2105. woocommerce_output_product_categories(
  2106. array(
  2107. 'before' => $args['before'],
  2108. 'after' => $args['after'],
  2109. 'parent_id' => is_product_category() ? get_queried_object_id() : 0,
  2110. )
  2111. );
  2112. return true;
  2113. } else {
  2114. // Output nothing. woocommerce_maybe_show_product_subcategories will handle the output of cats.
  2115. $display_type = woocommerce_get_loop_display_mode();
  2116. if ( 'subcategories' === $display_type ) {
  2117. // This removes pagination and products from display for themes not using wc_get_loop_prop in their product loops. @todo Remove in future major version.
  2118. global $wp_query;
  2119. if ( $wp_query->is_main_query() ) {
  2120. $wp_query->post_count = 0;
  2121. $wp_query->max_num_pages = 0;
  2122. }
  2123. }
  2124. return 'subcategories' === $display_type || 'both' === $display_type;
  2125. }
  2126. }
  2127. }
  2128. if ( ! function_exists( 'woocommerce_output_product_categories' ) ) {
  2129. /**
  2130. * Display product sub categories as thumbnails.
  2131. *
  2132. * This is a replacement for woocommerce_product_subcategories which also does some logic
  2133. * based on the loop. This function however just outputs when called.
  2134. *
  2135. * @since 3.3.1
  2136. * @param array $args Arguments.
  2137. * @return boolean
  2138. */
  2139. function woocommerce_output_product_categories( $args = array() ) {
  2140. $args = wp_parse_args(
  2141. $args,
  2142. array(
  2143. 'before' => apply_filters( 'woocommerce_before_output_product_categories', '' ),
  2144. 'after' => apply_filters( 'woocommerce_after_output_product_categories', '' ),
  2145. 'parent_id' => 0,
  2146. )
  2147. );
  2148. $product_categories = woocommerce_get_product_subcategories( $args['parent_id'] );
  2149. if ( ! $product_categories ) {
  2150. return false;
  2151. }
  2152. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  2153. echo $args['before'];
  2154. foreach ( $product_categories as $category ) {
  2155. wc_get_template(
  2156. 'content-product_cat.php',
  2157. array(
  2158. 'category' => $category,
  2159. )
  2160. );
  2161. }
  2162. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  2163. echo $args['after'];
  2164. return true;
  2165. }
  2166. }
  2167. if ( ! function_exists( 'woocommerce_get_product_subcategories' ) ) {
  2168. /**
  2169. * Get (and cache) product subcategories.
  2170. *
  2171. * @param int $parent_id Get subcategories of this ID.
  2172. * @return array
  2173. */
  2174. function woocommerce_get_product_subcategories( $parent_id = 0 ) {
  2175. $parent_id = absint( $parent_id );
  2176. $cache_key = apply_filters( 'woocommerce_get_product_subcategories_cache_key', 'product-category-hierarchy-' . $parent_id, $parent_id );
  2177. $product_categories = $cache_key ? wp_cache_get( $cache_key, 'product_cat' ) : false;
  2178. if ( false === $product_categories ) {
  2179. // NOTE: using child_of instead of parent - this is not ideal but due to a WP bug ( https://core.trac.wordpress.org/ticket/15626 ) pad_counts won't work.
  2180. $product_categories = get_categories(
  2181. apply_filters(
  2182. 'woocommerce_product_subcategories_args',
  2183. array(
  2184. 'parent' => $parent_id,
  2185. 'hide_empty' => 0,
  2186. 'hierarchical' => 1,
  2187. 'taxonomy' => 'product_cat',
  2188. 'pad_counts' => 1,
  2189. )
  2190. )
  2191. );
  2192. if ( $cache_key ) {
  2193. wp_cache_set( $cache_key, $product_categories, 'product_cat' );
  2194. }
  2195. }
  2196. if ( apply_filters( 'woocommerce_product_subcategories_hide_empty', true ) ) {
  2197. $product_categories = wp_list_filter( $product_categories, array( 'count' => 0 ), 'NOT' );
  2198. }
  2199. return $product_categories;
  2200. }
  2201. }
  2202. if ( ! function_exists( 'woocommerce_subcategory_thumbnail' ) ) {
  2203. /**
  2204. * Show subcategory thumbnails.
  2205. *
  2206. * @param mixed $category Category.
  2207. */
  2208. function woocommerce_subcategory_thumbnail( $category ) {
  2209. $small_thumbnail_size = apply_filters( 'subcategory_archive_thumbnail_size', 'woocommerce_thumbnail' );
  2210. $dimensions = wc_get_image_size( $small_thumbnail_size );
  2211. $thumbnail_id = get_term_meta( $category->term_id, 'thumbnail_id', true );
  2212. if ( $thumbnail_id ) {
  2213. $image = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
  2214. $image = $image[0];
  2215. $image_srcset = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $thumbnail_id, $small_thumbnail_size ) : false;
  2216. $image_sizes = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $thumbnail_id, $small_thumbnail_size ) : false;
  2217. } else {
  2218. $image = wc_placeholder_img_src();
  2219. $image_srcset = false;
  2220. $image_sizes = false;
  2221. }
  2222. if ( $image ) {
  2223. // Prevent esc_url from breaking spaces in urls for image embeds.
  2224. // Ref: https://core.trac.wordpress.org/ticket/23605.
  2225. $image = str_replace( ' ', '%20', $image );
  2226. // Add responsive image markup if available.
  2227. if ( $image_srcset && $image_sizes ) {
  2228. echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $category->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" srcset="' . esc_attr( $image_srcset ) . '" sizes="' . esc_attr( $image_sizes ) . '" />';
  2229. } else {
  2230. echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $category->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" />';
  2231. }
  2232. }
  2233. }
  2234. }
  2235. if ( ! function_exists( 'woocommerce_order_details_table' ) ) {
  2236. /**
  2237. * Displays order details in a table.
  2238. *
  2239. * @param mixed $order_id Order ID.
  2240. */
  2241. function woocommerce_order_details_table( $order_id ) {
  2242. if ( ! $order_id ) {
  2243. return;
  2244. }
  2245. wc_get_template(
  2246. 'order/order-details.php',
  2247. array(
  2248. 'order_id' => $order_id,
  2249. )
  2250. );
  2251. }
  2252. }
  2253. if ( ! function_exists( 'woocommerce_order_downloads_table' ) ) {
  2254. /**
  2255. * Displays order downloads in a table.
  2256. *
  2257. * @since 3.2.0
  2258. * @param array $downloads Downloads.
  2259. */
  2260. function woocommerce_order_downloads_table( $downloads ) {
  2261. if ( ! $downloads ) {
  2262. return;
  2263. }
  2264. wc_get_template(
  2265. 'order/order-downloads.php',
  2266. array(
  2267. 'downloads' => $downloads,
  2268. )
  2269. );
  2270. }
  2271. }
  2272. if ( ! function_exists( 'woocommerce_order_again_button' ) ) {
  2273. /**
  2274. * Display an 'order again' button on the view order page.
  2275. *
  2276. * @param object $order Order.
  2277. */
  2278. function woocommerce_order_again_button( $order ) {
  2279. if ( ! $order || ! $order->has_status( apply_filters( 'woocommerce_valid_order_statuses_for_order_again', array( 'completed' ) ) ) || ! is_user_logged_in() ) {
  2280. return;
  2281. }
  2282. wc_get_template(
  2283. 'order/order-again.php',
  2284. array(
  2285. 'order' => $order,
  2286. 'order_again_url' => wp_nonce_url( add_query_arg( 'order_again', $order->get_id(), wc_get_cart_url() ), 'woocommerce-order_again' ),
  2287. )
  2288. );
  2289. }
  2290. }
  2291. /** Forms */
  2292. if ( ! function_exists( 'woocommerce_form_field' ) ) {
  2293. /**
  2294. * Outputs a checkout/address form field.
  2295. *
  2296. * @param string $key Key.
  2297. * @param mixed $args Arguments.
  2298. * @param string $value (default: null).
  2299. * @return string
  2300. */
  2301. function woocommerce_form_field( $key, $args, $value = null ) {
  2302. $defaults = array(
  2303. 'type' => 'text',
  2304. 'label' => '',
  2305. 'description' => '',
  2306. 'placeholder' => '',
  2307. 'maxlength' => false,
  2308. 'required' => false,
  2309. 'autocomplete' => false,
  2310. 'id' => $key,
  2311. 'class' => array(),
  2312. 'label_class' => array(),
  2313. 'input_class' => array(),
  2314. 'return' => false,
  2315. 'options' => array(),
  2316. 'custom_attributes' => array(),
  2317. 'validate' => array(),
  2318. 'default' => '',
  2319. 'autofocus' => '',
  2320. 'priority' => '',
  2321. );
  2322. $args = wp_parse_args( $args, $defaults );
  2323. $args = apply_filters( 'woocommerce_form_field_args', $args, $key, $value );
  2324. if ( $args['required'] ) {
  2325. $args['class'][] = 'validate-required';
  2326. $required = '&nbsp;<abbr class="required" title="' . esc_attr__( 'required', 'woocommerce' ) . '">*</abbr>';
  2327. } else {
  2328. $required = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
  2329. }
  2330. if ( is_string( $args['label_class'] ) ) {
  2331. $args['label_class'] = array( $args['label_class'] );
  2332. }
  2333. if ( is_null( $value ) ) {
  2334. $value = $args['default'];
  2335. }
  2336. // Custom attribute handling.
  2337. $custom_attributes = array();
  2338. $args['custom_attributes'] = array_filter( (array) $args['custom_attributes'], 'strlen' );
  2339. if ( $args['maxlength'] ) {
  2340. $args['custom_attributes']['maxlength'] = absint( $args['maxlength'] );
  2341. }
  2342. if ( ! empty( $args['autocomplete'] ) ) {
  2343. $args['custom_attributes']['autocomplete'] = $args['autocomplete'];
  2344. }
  2345. if ( true === $args['autofocus'] ) {
  2346. $args['custom_attributes']['autofocus'] = 'autofocus';
  2347. }
  2348. if ( $args['description'] ) {
  2349. $args['custom_attributes']['aria-describedby'] = $args['id'] . '-description';
  2350. }
  2351. if ( ! empty( $args['custom_attributes'] ) && is_array( $args['custom_attributes'] ) ) {
  2352. foreach ( $args['custom_attributes'] as $attribute => $attribute_value ) {
  2353. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
  2354. }
  2355. }
  2356. if ( ! empty( $args['validate'] ) ) {
  2357. foreach ( $args['validate'] as $validate ) {
  2358. $args['class'][] = 'validate-' . $validate;
  2359. }
  2360. }
  2361. $field = '';
  2362. $label_id = $args['id'];
  2363. $sort = $args['priority'] ? $args['priority'] : '';
  2364. $field_container = '<p class="form-row %1$s" id="%2$s" data-priority="' . esc_attr( $sort ) . '">%3$s</p>';
  2365. switch ( $args['type'] ) {
  2366. case 'country':
  2367. $countries = 'shipping_country' === $key ? WC()->countries->get_shipping_countries() : WC()->countries->get_allowed_countries();
  2368. if ( 1 === count( $countries ) ) {
  2369. $field .= '<strong>' . current( array_values( $countries ) ) . '</strong>';
  2370. $field .= '<input type="hidden" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" value="' . current( array_keys( $countries ) ) . '" ' . implode( ' ', $custom_attributes ) . ' class="country_to_state" readonly="readonly" />';
  2371. } else {
  2372. $data_label = ! empty( $args['label'] ) ? 'data-label="' . esc_attr( $args['label'] ) . '"' : '';
  2373. $field = '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" class="country_to_state country_select ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" ' . implode( ' ', $custom_attributes ) . ' data-placeholder="' . esc_attr( $args['placeholder'] ? $args['placeholder'] : esc_attr__( 'Select a country / region&hellip;', 'woocommerce' ) ) . '" ' . $data_label . '><option value="">' . esc_html__( 'Select a country / region&hellip;', 'woocommerce' ) . '</option>';
  2374. foreach ( $countries as $ckey => $cvalue ) {
  2375. $field .= '<option value="' . esc_attr( $ckey ) . '" ' . selected( $value, $ckey, false ) . '>' . esc_html( $cvalue ) . '</option>';
  2376. }
  2377. $field .= '</select>';
  2378. $field .= '<noscript><button type="submit" name="woocommerce_checkout_update_totals" value="' . esc_attr__( 'Update country / region', 'woocommerce' ) . '">' . esc_html__( 'Update country / region', 'woocommerce' ) . '</button></noscript>';
  2379. }
  2380. break;
  2381. case 'state':
  2382. /* Get country this state field is representing */
  2383. $for_country = isset( $args['country'] ) ? $args['country'] : WC()->checkout->get_value( 'billing_state' === $key ? 'billing_country' : 'shipping_country' );
  2384. $states = WC()->countries->get_states( $for_country );
  2385. if ( is_array( $states ) && empty( $states ) ) {
  2386. $field_container = '<p class="form-row %1$s" id="%2$s" style="display: none">%3$s</p>';
  2387. $field .= '<input type="hidden" class="hidden" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" value="" ' . implode( ' ', $custom_attributes ) . ' placeholder="' . esc_attr( $args['placeholder'] ) . '" readonly="readonly" data-input-classes="' . esc_attr( implode( ' ', $args['input_class'] ) ) . '"/>';
  2388. } elseif ( ! is_null( $for_country ) && is_array( $states ) ) {
  2389. $data_label = ! empty( $args['label'] ) ? 'data-label="' . esc_attr( $args['label'] ) . '"' : '';
  2390. $field .= '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" class="state_select ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" ' . implode( ' ', $custom_attributes ) . ' data-placeholder="' . esc_attr( $args['placeholder'] ? $args['placeholder'] : esc_html__( 'Select an option&hellip;', 'woocommerce' ) ) . '" data-input-classes="' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" ' . $data_label . '>
  2391. <option value="">' . esc_html__( 'Select an option&hellip;', 'woocommerce' ) . '</option>';
  2392. foreach ( $states as $ckey => $cvalue ) {
  2393. $field .= '<option value="' . esc_attr( $ckey ) . '" ' . selected( $value, $ckey, false ) . '>' . esc_html( $cvalue ) . '</option>';
  2394. }
  2395. $field .= '</select>';
  2396. } else {
  2397. $field .= '<input type="text" class="input-text ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" ' . implode( ' ', $custom_attributes ) . ' data-input-classes="' . esc_attr( implode( ' ', $args['input_class'] ) ) . '"/>';
  2398. }
  2399. break;
  2400. case 'textarea':
  2401. $field .= '<textarea name="' . esc_attr( $key ) . '" class="input-text ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" id="' . esc_attr( $args['id'] ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" ' . ( empty( $args['custom_attributes']['rows'] ) ? ' rows="2"' : '' ) . ( empty( $args['custom_attributes']['cols'] ) ? ' cols="5"' : '' ) . implode( ' ', $custom_attributes ) . '>' . esc_textarea( $value ) . '</textarea>';
  2402. break;
  2403. case 'checkbox':
  2404. $field = '<label class="checkbox ' . implode( ' ', $args['label_class'] ) . '" ' . implode( ' ', $custom_attributes ) . '>
  2405. <input type="' . esc_attr( $args['type'] ) . '" class="input-checkbox ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" value="1" ' . checked( $value, 1, false ) . ' /> ' . $args['label'] . $required . '</label>';
  2406. break;
  2407. case 'text':
  2408. case 'password':
  2409. case 'datetime':
  2410. case 'datetime-local':
  2411. case 'date':
  2412. case 'month':
  2413. case 'time':
  2414. case 'week':
  2415. case 'number':
  2416. case 'email':
  2417. case 'url':
  2418. case 'tel':
  2419. $field .= '<input type="' . esc_attr( $args['type'] ) . '" class="input-text ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" value="' . esc_attr( $value ) . '" ' . implode( ' ', $custom_attributes ) . ' />';
  2420. break;
  2421. case 'hidden':
  2422. $field .= '<input type="' . esc_attr( $args['type'] ) . '" class="input-hidden ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" value="' . esc_attr( $value ) . '" ' . implode( ' ', $custom_attributes ) . ' />';
  2423. break;
  2424. case 'select':
  2425. $field = '';
  2426. $options = '';
  2427. if ( ! empty( $args['options'] ) ) {
  2428. foreach ( $args['options'] as $option_key => $option_text ) {
  2429. if ( '' === $option_key ) {
  2430. // If we have a blank option, select2 needs a placeholder.
  2431. if ( empty( $args['placeholder'] ) ) {
  2432. $args['placeholder'] = $option_text ? $option_text : __( 'Choose an option', 'woocommerce' );
  2433. }
  2434. $custom_attributes[] = 'data-allow_clear="true"';
  2435. }
  2436. $options .= '<option value="' . esc_attr( $option_key ) . '" ' . selected( $value, $option_key, false ) . '>' . esc_html( $option_text ) . '</option>';
  2437. }
  2438. $field .= '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" class="select ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" ' . implode( ' ', $custom_attributes ) . ' data-placeholder="' . esc_attr( $args['placeholder'] ) . '">
  2439. ' . $options . '
  2440. </select>';
  2441. }
  2442. break;
  2443. case 'radio':
  2444. $label_id .= '_' . current( array_keys( $args['options'] ) );
  2445. if ( ! empty( $args['options'] ) ) {
  2446. foreach ( $args['options'] as $option_key => $option_text ) {
  2447. $field .= '<input type="radio" class="input-radio ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" value="' . esc_attr( $option_key ) . '" name="' . esc_attr( $key ) . '" ' . implode( ' ', $custom_attributes ) . ' id="' . esc_attr( $args['id'] ) . '_' . esc_attr( $option_key ) . '"' . checked( $value, $option_key, false ) . ' />';
  2448. $field .= '<label for="' . esc_attr( $args['id'] ) . '_' . esc_attr( $option_key ) . '" class="radio ' . implode( ' ', $args['label_class'] ) . '">' . esc_html( $option_text ) . '</label>';
  2449. }
  2450. }
  2451. break;
  2452. }
  2453. if ( ! empty( $field ) ) {
  2454. $field_html = '';
  2455. if ( $args['label'] && 'checkbox' !== $args['type'] ) {
  2456. $field_html .= '<label for="' . esc_attr( $label_id ) . '" class="' . esc_attr( implode( ' ', $args['label_class'] ) ) . '">' . wp_kses_post( $args['label'] ) . $required . '</label>';
  2457. }
  2458. $field_html .= '<span class="woocommerce-input-wrapper">' . $field;
  2459. if ( $args['description'] ) {
  2460. $field_html .= '<span class="description" id="' . esc_attr( $args['id'] ) . '-description" aria-hidden="true">' . wp_kses_post( $args['description'] ) . '</span>';
  2461. }
  2462. $field_html .= '</span>';
  2463. $container_class = esc_attr( implode( ' ', $args['class'] ) );
  2464. $container_id = esc_attr( $args['id'] ) . '_field';
  2465. $field = sprintf( $field_container, $container_class, $container_id, $field_html );
  2466. }
  2467. /**
  2468. * Filter by type.
  2469. */
  2470. $field = apply_filters( 'woocommerce_form_field_' . $args['type'], $field, $key, $args, $value );
  2471. /**
  2472. * General filter on form fields.
  2473. *
  2474. * @since 3.4.0
  2475. */
  2476. $field = apply_filters( 'woocommerce_form_field', $field, $key, $args, $value );
  2477. if ( $args['return'] ) {
  2478. return $field;
  2479. } else {
  2480. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  2481. echo $field;
  2482. }
  2483. }
  2484. }
  2485. if ( ! function_exists( 'get_product_search_form' ) ) {
  2486. /**
  2487. * Display product search form.
  2488. *
  2489. * Will first attempt to locate the product-searchform.php file in either the child or.
  2490. * the parent, then load it. If it doesn't exist, then the default search form.
  2491. * will be displayed.
  2492. *
  2493. * The default searchform uses html5.
  2494. *
  2495. * @param bool $echo (default: true).
  2496. * @return string
  2497. */
  2498. function get_product_search_form( $echo = true ) {
  2499. global $product_search_form_index;
  2500. ob_start();
  2501. if ( empty( $product_search_form_index ) ) {
  2502. $product_search_form_index = 0;
  2503. }
  2504. do_action( 'pre_get_product_search_form' );
  2505. wc_get_template(
  2506. 'product-searchform.php',
  2507. array(
  2508. 'index' => $product_search_form_index++,
  2509. )
  2510. );
  2511. $form = apply_filters( 'get_product_search_form', ob_get_clean() );
  2512. if ( ! $echo ) {
  2513. return $form;
  2514. }
  2515. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  2516. echo $form;
  2517. }
  2518. }
  2519. if ( ! function_exists( 'woocommerce_output_auth_header' ) ) {
  2520. /**
  2521. * Output the Auth header.
  2522. */
  2523. function woocommerce_output_auth_header() {
  2524. wc_get_template( 'auth/header.php' );
  2525. }
  2526. }
  2527. if ( ! function_exists( 'woocommerce_output_auth_footer' ) ) {
  2528. /**
  2529. * Output the Auth footer.
  2530. */
  2531. function woocommerce_output_auth_footer() {
  2532. wc_get_template( 'auth/footer.php' );
  2533. }
  2534. }
  2535. if ( ! function_exists( 'woocommerce_single_variation' ) ) {
  2536. /**
  2537. * Output placeholders for the single variation.
  2538. */
  2539. function woocommerce_single_variation() {
  2540. echo '<div class="woocommerce-variation single_variation"></div>';
  2541. }
  2542. }
  2543. if ( ! function_exists( 'woocommerce_single_variation_add_to_cart_button' ) ) {
  2544. /**
  2545. * Output the add to cart button for variations.
  2546. */
  2547. function woocommerce_single_variation_add_to_cart_button() {
  2548. wc_get_template( 'single-product/add-to-cart/variation-add-to-cart-button.php' );
  2549. }
  2550. }
  2551. if ( ! function_exists( 'wc_dropdown_variation_attribute_options' ) ) {
  2552. /**
  2553. * Output a list of variation attributes for use in the cart forms.
  2554. *
  2555. * @param array $args Arguments.
  2556. * @since 2.4.0
  2557. */
  2558. function wc_dropdown_variation_attribute_options( $args = array() ) {
  2559. $args = wp_parse_args(
  2560. apply_filters( 'woocommerce_dropdown_variation_attribute_options_args', $args ),
  2561. array(
  2562. 'options' => false,
  2563. 'attribute' => false,
  2564. 'product' => false,
  2565. 'selected' => false,
  2566. 'name' => '',
  2567. 'id' => '',
  2568. 'class' => '',
  2569. 'show_option_none' => __( 'Choose an option', 'woocommerce' ),
  2570. )
  2571. );
  2572. // Get selected value.
  2573. if ( false === $args['selected'] && $args['attribute'] && $args['product'] instanceof WC_Product ) {
  2574. $selected_key = 'attribute_' . sanitize_title( $args['attribute'] );
  2575. // phpcs:disable WordPress.Security.NonceVerification.Recommended
  2576. $args['selected'] = isset( $_REQUEST[ $selected_key ] ) ? wc_clean( wp_unslash( $_REQUEST[ $selected_key ] ) ) : $args['product']->get_variation_default_attribute( $args['attribute'] );
  2577. // phpcs:enable WordPress.Security.NonceVerification.Recommended
  2578. }
  2579. $options = $args['options'];
  2580. $product = $args['product'];
  2581. $attribute = $args['attribute'];
  2582. $name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
  2583. $id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
  2584. $class = $args['class'];
  2585. $show_option_none = (bool) $args['show_option_none'];
  2586. $show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' ); // We'll do our best to hide the placeholder, but we'll need to show something when resetting options.
  2587. if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
  2588. $attributes = $product->get_variation_attributes();
  2589. $options = $attributes[ $attribute ];
  2590. }
  2591. $html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
  2592. $html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';
  2593. if ( ! empty( $options ) ) {
  2594. if ( $product && taxonomy_exists( $attribute ) ) {
  2595. // Get terms if this is a taxonomy - ordered. We need the names too.
  2596. $terms = wc_get_product_terms(
  2597. $product->get_id(),
  2598. $attribute,
  2599. array(
  2600. 'fields' => 'all',
  2601. )
  2602. );
  2603. foreach ( $terms as $term ) {
  2604. if ( in_array( $term->slug, $options, true ) ) {
  2605. $html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name, $term, $attribute, $product ) ) . '</option>';
  2606. }
  2607. }
  2608. } else {
  2609. foreach ( $options as $option ) {
  2610. // This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
  2611. $selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
  2612. $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option, null, $attribute, $product ) ) . '</option>';
  2613. }
  2614. }
  2615. }
  2616. $html .= '</select>';
  2617. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  2618. echo apply_filters( 'woocommerce_dropdown_variation_attribute_options_html', $html, $args );
  2619. }
  2620. }
  2621. if ( ! function_exists( 'woocommerce_account_content' ) ) {
  2622. /**
  2623. * My Account content output.
  2624. */
  2625. function woocommerce_account_content() {
  2626. global $wp;
  2627. if ( ! empty( $wp->query_vars ) ) {
  2628. foreach ( $wp->query_vars as $key => $value ) {
  2629. // Ignore pagename param.
  2630. if ( 'pagename' === $key ) {
  2631. continue;
  2632. }
  2633. if ( has_action( 'woocommerce_account_' . $key . '_endpoint' ) ) {
  2634. do_action( 'woocommerce_account_' . $key . '_endpoint', $value );
  2635. return;
  2636. }
  2637. }
  2638. }
  2639. // No endpoint found? Default to dashboard.
  2640. wc_get_template(
  2641. 'myaccount/dashboard.php',
  2642. array(
  2643. 'current_user' => get_user_by( 'id', get_current_user_id() ),
  2644. )
  2645. );
  2646. }
  2647. }
  2648. if ( ! function_exists( 'woocommerce_account_navigation' ) ) {
  2649. /**
  2650. * My Account navigation template.
  2651. */
  2652. function woocommerce_account_navigation() {
  2653. wc_get_template( 'myaccount/navigation.php' );
  2654. }
  2655. }
  2656. if ( ! function_exists( 'woocommerce_account_orders' ) ) {
  2657. /**
  2658. * My Account > Orders template.
  2659. *
  2660. * @param int $current_page Current page number.
  2661. */
  2662. function woocommerce_account_orders( $current_page ) {
  2663. $current_page = empty( $current_page ) ? 1 : absint( $current_page );
  2664. $customer_orders = wc_get_orders(
  2665. apply_filters(
  2666. 'woocommerce_my_account_my_orders_query',
  2667. array(
  2668. 'customer' => get_current_user_id(),
  2669. 'page' => $current_page,
  2670. 'paginate' => true,
  2671. )
  2672. )
  2673. );
  2674. wc_get_template(
  2675. 'myaccount/orders.php',
  2676. array(
  2677. 'current_page' => absint( $current_page ),
  2678. 'customer_orders' => $customer_orders,
  2679. 'has_orders' => 0 < $customer_orders->total,
  2680. )
  2681. );
  2682. }
  2683. }
  2684. if ( ! function_exists( 'woocommerce_account_view_order' ) ) {
  2685. /**
  2686. * My Account > View order template.
  2687. *
  2688. * @param int $order_id Order ID.
  2689. */
  2690. function woocommerce_account_view_order( $order_id ) {
  2691. WC_Shortcode_My_Account::view_order( absint( $order_id ) );
  2692. }
  2693. }
  2694. if ( ! function_exists( 'woocommerce_account_downloads' ) ) {
  2695. /**
  2696. * My Account > Downloads template.
  2697. */
  2698. function woocommerce_account_downloads() {
  2699. wc_get_template( 'myaccount/downloads.php' );
  2700. }
  2701. }
  2702. if ( ! function_exists( 'woocommerce_account_edit_address' ) ) {
  2703. /**
  2704. * My Account > Edit address template.
  2705. *
  2706. * @param string $type Address type.
  2707. */
  2708. function woocommerce_account_edit_address( $type ) {
  2709. $type = wc_edit_address_i18n( sanitize_title( $type ), true );
  2710. WC_Shortcode_My_Account::edit_address( $type );
  2711. }
  2712. }
  2713. if ( ! function_exists( 'woocommerce_account_payment_methods' ) ) {
  2714. /**
  2715. * My Account > Downloads template.
  2716. */
  2717. function woocommerce_account_payment_methods() {
  2718. wc_get_template( 'myaccount/payment-methods.php' );
  2719. }
  2720. }
  2721. if ( ! function_exists( 'woocommerce_account_add_payment_method' ) ) {
  2722. /**
  2723. * My Account > Add payment method template.
  2724. */
  2725. function woocommerce_account_add_payment_method() {
  2726. WC_Shortcode_My_Account::add_payment_method();
  2727. }
  2728. }
  2729. if ( ! function_exists( 'woocommerce_account_edit_account' ) ) {
  2730. /**
  2731. * My Account > Edit account template.
  2732. */
  2733. function woocommerce_account_edit_account() {
  2734. WC_Shortcode_My_Account::edit_account();
  2735. }
  2736. }
  2737. if ( ! function_exists( 'wc_no_products_found' ) ) {
  2738. /**
  2739. * Handles the loop when no products were found/no product exist.
  2740. */
  2741. function wc_no_products_found() {
  2742. wc_get_template( 'loop/no-products-found.php' );
  2743. }
  2744. }
  2745. if ( ! function_exists( 'wc_get_email_order_items' ) ) {
  2746. /**
  2747. * Get HTML for the order items to be shown in emails.
  2748. *
  2749. * @param WC_Order $order Order object.
  2750. * @param array $args Arguments.
  2751. *
  2752. * @since 3.0.0
  2753. * @return string
  2754. */
  2755. function wc_get_email_order_items( $order, $args = array() ) {
  2756. ob_start();
  2757. $defaults = array(
  2758. 'show_sku' => false,
  2759. 'show_image' => false,
  2760. 'image_size' => array( 32, 32 ),
  2761. 'plain_text' => false,
  2762. 'sent_to_admin' => false,
  2763. );
  2764. $args = wp_parse_args( $args, $defaults );
  2765. $template = $args['plain_text'] ? 'emails/plain/email-order-items.php' : 'emails/email-order-items.php';
  2766. wc_get_template(
  2767. $template,
  2768. apply_filters(
  2769. 'woocommerce_email_order_items_args',
  2770. array(
  2771. 'order' => $order,
  2772. 'items' => $order->get_items(),
  2773. 'show_download_links' => $order->is_download_permitted() && ! $args['sent_to_admin'],
  2774. 'show_sku' => $args['show_sku'],
  2775. 'show_purchase_note' => $order->is_paid() && ! $args['sent_to_admin'],
  2776. 'show_image' => $args['show_image'],
  2777. 'image_size' => $args['image_size'],
  2778. 'plain_text' => $args['plain_text'],
  2779. 'sent_to_admin' => $args['sent_to_admin'],
  2780. )
  2781. )
  2782. );
  2783. return apply_filters( 'woocommerce_email_order_items_table', ob_get_clean(), $order );
  2784. }
  2785. }
  2786. if ( ! function_exists( 'wc_display_item_meta' ) ) {
  2787. /**
  2788. * Display item meta data.
  2789. *
  2790. * @since 3.0.0
  2791. * @param WC_Order_Item $item Order Item.
  2792. * @param array $args Arguments.
  2793. * @return string|void
  2794. */
  2795. function wc_display_item_meta( $item, $args = array() ) {
  2796. $strings = array();
  2797. $html = '';
  2798. $args = wp_parse_args(
  2799. $args,
  2800. array(
  2801. 'before' => '<ul class="wc-item-meta"><li>',
  2802. 'after' => '</li></ul>',
  2803. 'separator' => '</li><li>',
  2804. 'echo' => true,
  2805. 'autop' => false,
  2806. 'label_before' => '<strong class="wc-item-meta-label">',
  2807. 'label_after' => ':</strong> ',
  2808. )
  2809. );
  2810. foreach ( $item->get_formatted_meta_data() as $meta_id => $meta ) {
  2811. $value = $args['autop'] ? wp_kses_post( $meta->display_value ) : wp_kses_post( make_clickable( trim( $meta->display_value ) ) );
  2812. $strings[] = $args['label_before'] . wp_kses_post( $meta->display_key ) . $args['label_after'] . $value;
  2813. }
  2814. if ( $strings ) {
  2815. $html = $args['before'] . implode( $args['separator'], $strings ) . $args['after'];
  2816. }
  2817. $html = apply_filters( 'woocommerce_display_item_meta', $html, $item, $args );
  2818. if ( $args['echo'] ) {
  2819. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  2820. echo $html;
  2821. } else {
  2822. return $html;
  2823. }
  2824. }
  2825. }
  2826. if ( ! function_exists( 'wc_display_item_downloads' ) ) {
  2827. /**
  2828. * Display item download links.
  2829. *
  2830. * @since 3.0.0
  2831. * @param WC_Order_Item $item Order Item.
  2832. * @param array $args Arguments.
  2833. * @return string|void
  2834. */
  2835. function wc_display_item_downloads( $item, $args = array() ) {
  2836. $strings = array();
  2837. $html = '';
  2838. $args = wp_parse_args(
  2839. $args,
  2840. array(
  2841. 'before' => '<ul class ="wc-item-downloads"><li>',
  2842. 'after' => '</li></ul>',
  2843. 'separator' => '</li><li>',
  2844. 'echo' => true,
  2845. 'show_url' => false,
  2846. )
  2847. );
  2848. $downloads = is_object( $item ) && $item->is_type( 'line_item' ) ? $item->get_item_downloads() : array();
  2849. if ( $downloads ) {
  2850. $i = 0;
  2851. foreach ( $downloads as $file ) {
  2852. $i ++;
  2853. if ( $args['show_url'] ) {
  2854. $strings[] = '<strong class="wc-item-download-label">' . esc_html( $file['name'] ) . ':</strong> ' . esc_html( $file['download_url'] );
  2855. } else {
  2856. /* translators: %d: downloads count */
  2857. $prefix = count( $downloads ) > 1 ? sprintf( __( 'Download %d', 'woocommerce' ), $i ) : __( 'Download', 'woocommerce' );
  2858. $strings[] = '<strong class="wc-item-download-label">' . $prefix . ':</strong> <a href="' . esc_url( $file['download_url'] ) . '" target="_blank">' . esc_html( $file['name'] ) . '</a>';
  2859. }
  2860. }
  2861. }
  2862. if ( $strings ) {
  2863. $html = $args['before'] . implode( $args['separator'], $strings ) . $args['after'];
  2864. }
  2865. $html = apply_filters( 'woocommerce_display_item_downloads', $html, $item, $args );
  2866. if ( $args['echo'] ) {
  2867. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  2868. echo $html;
  2869. } else {
  2870. return $html;
  2871. }
  2872. }
  2873. }
  2874. if ( ! function_exists( 'woocommerce_photoswipe' ) ) {
  2875. /**
  2876. * Get the shop sidebar template.
  2877. */
  2878. function woocommerce_photoswipe() {
  2879. if ( current_theme_supports( 'wc-product-gallery-lightbox' ) ) {
  2880. wc_get_template( 'single-product/photoswipe.php' );
  2881. }
  2882. }
  2883. }
  2884. /**
  2885. * Outputs a list of product attributes for a product.
  2886. *
  2887. * @since 3.0.0
  2888. * @param WC_Product $product Product Object.
  2889. */
  2890. function wc_display_product_attributes( $product ) {
  2891. $product_attributes = array();
  2892. // Display weight and dimensions before attribute list.
  2893. $display_dimensions = apply_filters( 'wc_product_enable_dimensions_display', $product->has_weight() || $product->has_dimensions() );
  2894. if ( $display_dimensions && $product->has_weight() ) {
  2895. $product_attributes['weight'] = array(
  2896. 'label' => __( 'Weight', 'woocommerce' ),
  2897. 'value' => wc_format_weight( $product->get_weight() ),
  2898. );
  2899. }
  2900. if ( $display_dimensions && $product->has_dimensions() ) {
  2901. $product_attributes['dimensions'] = array(
  2902. 'label' => __( 'Dimensions', 'woocommerce' ),
  2903. 'value' => wc_format_dimensions( $product->get_dimensions( false ) ),
  2904. );
  2905. }
  2906. // Add product attributes to list.
  2907. $attributes = array_filter( $product->get_attributes(), 'wc_attributes_array_filter_visible' );
  2908. foreach ( $attributes as $attribute ) {
  2909. $values = array();
  2910. if ( $attribute->is_taxonomy() ) {
  2911. $attribute_taxonomy = $attribute->get_taxonomy_object();
  2912. $attribute_values = wc_get_product_terms( $product->get_id(), $attribute->get_name(), array( 'fields' => 'all' ) );
  2913. foreach ( $attribute_values as $attribute_value ) {
  2914. $value_name = esc_html( $attribute_value->name );
  2915. if ( $attribute_taxonomy->attribute_public ) {
  2916. $values[] = '<a href="' . esc_url( get_term_link( $attribute_value->term_id, $attribute->get_name() ) ) . '" rel="tag">' . $value_name . '</a>';
  2917. } else {
  2918. $values[] = $value_name;
  2919. }
  2920. }
  2921. } else {
  2922. $values = $attribute->get_options();
  2923. foreach ( $values as &$value ) {
  2924. $value = make_clickable( esc_html( $value ) );
  2925. }
  2926. }
  2927. $product_attributes[ 'attribute_' . sanitize_title_with_dashes( $attribute->get_name() ) ] = array(
  2928. 'label' => wc_attribute_label( $attribute->get_name() ),
  2929. 'value' => apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values ),
  2930. );
  2931. }
  2932. /**
  2933. * Hook: woocommerce_display_product_attributes.
  2934. *
  2935. * @since 3.6.0.
  2936. * @param array $product_attributes Array of atributes to display; label, value.
  2937. * @param WC_Product $product Showing attributes for this product.
  2938. */
  2939. $product_attributes = apply_filters( 'woocommerce_display_product_attributes', $product_attributes, $product );
  2940. wc_get_template(
  2941. 'single-product/product-attributes.php',
  2942. array(
  2943. 'product_attributes' => $product_attributes,
  2944. // Legacy params.
  2945. 'product' => $product,
  2946. 'attributes' => $attributes,
  2947. 'display_dimensions' => $display_dimensions,
  2948. )
  2949. );
  2950. }
  2951. /**
  2952. * Get HTML to show product stock.
  2953. *
  2954. * @since 3.0.0
  2955. * @param WC_Product $product Product Object.
  2956. * @return string
  2957. */
  2958. function wc_get_stock_html( $product ) {
  2959. $html = '';
  2960. $availability = $product->get_availability();
  2961. if ( ! empty( $availability['availability'] ) ) {
  2962. ob_start();
  2963. wc_get_template(
  2964. 'single-product/stock.php',
  2965. array(
  2966. 'product' => $product,
  2967. 'class' => $availability['class'],
  2968. 'availability' => $availability['availability'],
  2969. )
  2970. );
  2971. $html = ob_get_clean();
  2972. }
  2973. if ( has_filter( 'woocommerce_stock_html' ) ) {
  2974. wc_deprecated_function( 'The woocommerce_stock_html filter', '', 'woocommerce_get_stock_html' );
  2975. $html = apply_filters( 'woocommerce_stock_html', $html, $availability['availability'], $product );
  2976. }
  2977. return apply_filters( 'woocommerce_get_stock_html', $html, $product );
  2978. }
  2979. /**
  2980. * Get HTML for ratings.
  2981. *
  2982. * @since 3.0.0
  2983. * @param float $rating Rating being shown.
  2984. * @param int $count Total number of ratings.
  2985. * @return string
  2986. */
  2987. function wc_get_rating_html( $rating, $count = 0 ) {
  2988. $html = '';
  2989. if ( 0 < $rating ) {
  2990. /* translators: %s: rating */
  2991. $label = sprintf( __( 'Rated %s out of 5', 'woocommerce' ), $rating );
  2992. $html = '<div class="star-rating" role="img" aria-label="' . esc_attr( $label ) . '">' . wc_get_star_rating_html( $rating, $count ) . '</div>';
  2993. }
  2994. return apply_filters( 'woocommerce_product_get_rating_html', $html, $rating, $count );
  2995. }
  2996. /**
  2997. * Get HTML for star rating.
  2998. *
  2999. * @since 3.1.0
  3000. * @param float $rating Rating being shown.
  3001. * @param int $count Total number of ratings.
  3002. * @return string
  3003. */
  3004. function wc_get_star_rating_html( $rating, $count = 0 ) {
  3005. $html = '<span style="width:' . ( ( $rating / 5 ) * 100 ) . '%">';
  3006. if ( 0 < $count ) {
  3007. /* translators: 1: rating 2: rating count */
  3008. $html .= sprintf( _n( 'Rated %1$s out of 5 based on %2$s customer rating', 'Rated %1$s out of 5 based on %2$s customer ratings', $count, 'woocommerce' ), '<strong class="rating">' . esc_html( $rating ) . '</strong>', '<span class="rating">' . esc_html( $count ) . '</span>' );
  3009. } else {
  3010. /* translators: %s: rating */
  3011. $html .= sprintf( esc_html__( 'Rated %s out of 5', 'woocommerce' ), '<strong class="rating">' . esc_html( $rating ) . '</strong>' );
  3012. }
  3013. $html .= '</span>';
  3014. return apply_filters( 'woocommerce_get_star_rating_html', $html, $rating, $count );
  3015. }
  3016. /**
  3017. * Returns a 'from' prefix if you want to show where prices start at.
  3018. *
  3019. * @since 3.0.0
  3020. * @return string
  3021. */
  3022. function wc_get_price_html_from_text() {
  3023. return apply_filters( 'woocommerce_get_price_html_from_text', '<span class="from">' . _x( 'From:', 'min_price', 'woocommerce' ) . ' </span>' );
  3024. }
  3025. /**
  3026. * Get logout endpoint.
  3027. *
  3028. * @since 2.6.9
  3029. *
  3030. * @param string $redirect Redirect URL.
  3031. *
  3032. * @return string
  3033. */
  3034. function wc_logout_url( $redirect = '' ) {
  3035. $redirect = $redirect ? $redirect : apply_filters( 'woocommerce_logout_default_redirect_url', wc_get_page_permalink( 'myaccount' ) );
  3036. if ( get_option( 'woocommerce_logout_endpoint' ) ) {
  3037. return wp_nonce_url( wc_get_endpoint_url( 'customer-logout', '', $redirect ), 'customer-logout' );
  3038. }
  3039. return wp_logout_url( $redirect );
  3040. }
  3041. /**
  3042. * Show notice if cart is empty.
  3043. *
  3044. * @since 3.1.0
  3045. */
  3046. function wc_empty_cart_message() {
  3047. echo '<p class="cart-empty woocommerce-info">' . wp_kses_post( apply_filters( 'wc_empty_cart_message', __( 'Your cart is currently empty.', 'woocommerce' ) ) ) . '</p>';
  3048. }
  3049. /**
  3050. * Disable search engines indexing core, dynamic, cart/checkout pages.
  3051. *
  3052. * @todo Deprecated this function after dropping support for WP 5.6.
  3053. * @since 3.2.0
  3054. */
  3055. function wc_page_noindex() {
  3056. // wp_no_robots is deprecated since WP 5.7.
  3057. if ( function_exists( 'wp_robots_no_robots' ) ) {
  3058. return;
  3059. }
  3060. if ( is_page( wc_get_page_id( 'cart' ) ) || is_page( wc_get_page_id( 'checkout' ) ) || is_page( wc_get_page_id( 'myaccount' ) ) ) {
  3061. wp_no_robots();
  3062. }
  3063. }
  3064. add_action( 'wp_head', 'wc_page_noindex' );
  3065. /**
  3066. * Disable search engines indexing core, dynamic, cart/checkout pages.
  3067. * Uses "wp_robots" filter introduced in WP 5.7.
  3068. *
  3069. * @since 5.0.0
  3070. * @param array $robots Associative array of robots directives.
  3071. * @return array Filtered robots directives.
  3072. */
  3073. function wc_page_no_robots( $robots ) {
  3074. if ( is_page( wc_get_page_id( 'cart' ) ) || is_page( wc_get_page_id( 'checkout' ) ) || is_page( wc_get_page_id( 'myaccount' ) ) ) {
  3075. return wp_robots_no_robots( $robots );
  3076. }
  3077. return $robots;
  3078. }
  3079. add_filter( 'wp_robots', 'wc_page_no_robots' );
  3080. /**
  3081. * Get a slug identifying the current theme.
  3082. *
  3083. * @since 3.3.0
  3084. * @return string
  3085. */
  3086. function wc_get_theme_slug_for_templates() {
  3087. return apply_filters( 'woocommerce_theme_slug_for_templates', get_option( 'template' ) );
  3088. }
  3089. /**
  3090. * Gets and formats a list of cart item data + variations for display on the frontend.
  3091. *
  3092. * @since 3.3.0
  3093. * @param array $cart_item Cart item object.
  3094. * @param bool $flat Should the data be returned flat or in a list.
  3095. * @return string
  3096. */
  3097. function wc_get_formatted_cart_item_data( $cart_item, $flat = false ) {
  3098. $item_data = array();
  3099. // Variation values are shown only if they are not found in the title as of 3.0.
  3100. // This is because variation titles display the attributes.
  3101. if ( $cart_item['data']->is_type( 'variation' ) && is_array( $cart_item['variation'] ) ) {
  3102. foreach ( $cart_item['variation'] as $name => $value ) {
  3103. $taxonomy = wc_attribute_taxonomy_name( str_replace( 'attribute_pa_', '', urldecode( $name ) ) );
  3104. if ( taxonomy_exists( $taxonomy ) ) {
  3105. // If this is a term slug, get the term's nice name.
  3106. $term = get_term_by( 'slug', $value, $taxonomy );
  3107. if ( ! is_wp_error( $term ) && $term && $term->name ) {
  3108. $value = $term->name;
  3109. }
  3110. $label = wc_attribute_label( $taxonomy );
  3111. } else {
  3112. // If this is a custom option slug, get the options name.
  3113. $value = apply_filters( 'woocommerce_variation_option_name', $value, null, $taxonomy, $cart_item['data'] );
  3114. $label = wc_attribute_label( str_replace( 'attribute_', '', $name ), $cart_item['data'] );
  3115. }
  3116. // Check the nicename against the title.
  3117. if ( '' === $value || wc_is_attribute_in_product_name( $value, $cart_item['data']->get_name() ) ) {
  3118. continue;
  3119. }
  3120. $item_data[] = array(
  3121. 'key' => $label,
  3122. 'value' => $value,
  3123. );
  3124. }
  3125. }
  3126. // Filter item data to allow 3rd parties to add more to the array.
  3127. $item_data = apply_filters( 'woocommerce_get_item_data', $item_data, $cart_item );
  3128. // Format item data ready to display.
  3129. foreach ( $item_data as $key => $data ) {
  3130. // Set hidden to true to not display meta on cart.
  3131. if ( ! empty( $data['hidden'] ) ) {
  3132. unset( $item_data[ $key ] );
  3133. continue;
  3134. }
  3135. $item_data[ $key ]['key'] = ! empty( $data['key'] ) ? $data['key'] : $data['name'];
  3136. $item_data[ $key ]['display'] = ! empty( $data['display'] ) ? $data['display'] : $data['value'];
  3137. }
  3138. // Output flat or in list format.
  3139. if ( count( $item_data ) > 0 ) {
  3140. ob_start();
  3141. if ( $flat ) {
  3142. foreach ( $item_data as $data ) {
  3143. echo esc_html( $data['key'] ) . ': ' . wp_kses_post( $data['display'] ) . "\n";
  3144. }
  3145. } else {
  3146. wc_get_template( 'cart/cart-item-data.php', array( 'item_data' => $item_data ) );
  3147. }
  3148. return ob_get_clean();
  3149. }
  3150. return '';
  3151. }
  3152. /**
  3153. * Gets the url to remove an item from the cart.
  3154. *
  3155. * @since 3.3.0
  3156. * @param string $cart_item_key contains the id of the cart item.
  3157. * @return string url to page
  3158. */
  3159. function wc_get_cart_remove_url( $cart_item_key ) {
  3160. $cart_page_url = wc_get_cart_url();
  3161. return apply_filters( 'woocommerce_get_remove_url', $cart_page_url ? wp_nonce_url( add_query_arg( 'remove_item', $cart_item_key, $cart_page_url ), 'woocommerce-cart' ) : '' );
  3162. }
  3163. /**
  3164. * Gets the url to re-add an item into the cart.
  3165. *
  3166. * @since 3.3.0
  3167. * @param string $cart_item_key Cart item key to undo.
  3168. * @return string url to page
  3169. */
  3170. function wc_get_cart_undo_url( $cart_item_key ) {
  3171. $cart_page_url = wc_get_cart_url();
  3172. $query_args = array(
  3173. 'undo_item' => $cart_item_key,
  3174. );
  3175. return apply_filters( 'woocommerce_get_undo_url', $cart_page_url ? wp_nonce_url( add_query_arg( $query_args, $cart_page_url ), 'woocommerce-cart' ) : '', $cart_item_key );
  3176. }
  3177. /**
  3178. * Outputs all queued notices on WC pages.
  3179. *
  3180. * @since 3.5.0
  3181. */
  3182. function woocommerce_output_all_notices() {
  3183. echo '<div class="woocommerce-notices-wrapper">';
  3184. wc_print_notices();
  3185. echo '</div>';
  3186. }
  3187. /**
  3188. * Products RSS Feed.
  3189. *
  3190. * @deprecated 2.6
  3191. */
  3192. function wc_products_rss_feed() {
  3193. wc_deprecated_function( 'wc_products_rss_feed', '2.6' );
  3194. }
  3195. if ( ! function_exists( 'woocommerce_reset_loop' ) ) {
  3196. /**
  3197. * Reset the loop's index and columns when we're done outputting a product loop.
  3198. *
  3199. * @deprecated 3.3
  3200. */
  3201. function woocommerce_reset_loop() {
  3202. wc_reset_loop();
  3203. }
  3204. }
  3205. if ( ! function_exists( 'woocommerce_product_reviews_tab' ) ) {
  3206. /**
  3207. * Output the reviews tab content.
  3208. *
  3209. * @deprecated 2.4.0 Unused.
  3210. */
  3211. function woocommerce_product_reviews_tab() {
  3212. wc_deprecated_function( 'woocommerce_product_reviews_tab', '2.4' );
  3213. }
  3214. }
  3215. /**
  3216. * Display pay buttons HTML.
  3217. *
  3218. * @since 3.9.0
  3219. */
  3220. function wc_get_pay_buttons() {
  3221. $supported_gateways = array();
  3222. $available_gateways = WC()->payment_gateways()->get_available_payment_gateways();
  3223. foreach ( $available_gateways as $gateway ) {
  3224. if ( $gateway->supports( 'pay_button' ) ) {
  3225. $supported_gateways[] = $gateway->get_pay_button_id();
  3226. }
  3227. }
  3228. if ( ! $supported_gateways ) {
  3229. return;
  3230. }
  3231. echo '<div class="woocommerce-pay-buttons">';
  3232. foreach ( $supported_gateways as $pay_button_id ) {
  3233. echo sprintf( '<div class="woocommerce-pay-button__%1$s %1$s" id="%1$s"></div>', esc_attr( $pay_button_id ) );
  3234. }
  3235. echo '</div>';
  3236. }
  3237. // phpcs:enable Generic.Commenting.Todo.TaskFound