wc-template-functions.php 101 KB

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