wc-product-functions.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. <?php
  2. /**
  3. * WooCommerce Product Functions
  4. *
  5. * Functions for product specific things.
  6. *
  7. * @package WooCommerce/Functions
  8. * @version 3.0.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * Standard way of retrieving products based on certain parameters.
  13. *
  14. * This function should be used for product retrieval so that we have a data agnostic
  15. * way to get a list of products.
  16. *
  17. * Args and usage: https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
  18. *
  19. * @since 3.0.0
  20. * @param array $args Array of args (above).
  21. * @return array|stdClass Number of pages and an array of product objects if
  22. * paginate is true, or just an array of values.
  23. */
  24. function wc_get_products( $args ) {
  25. // Handle some BW compatibility arg names where wp_query args differ in naming.
  26. $map_legacy = array(
  27. 'numberposts' => 'limit',
  28. 'post_status' => 'status',
  29. 'post_parent' => 'parent',
  30. 'posts_per_page' => 'limit',
  31. 'paged' => 'page',
  32. );
  33. foreach ( $map_legacy as $from => $to ) {
  34. if ( isset( $args[ $from ] ) ) {
  35. $args[ $to ] = $args[ $from ];
  36. }
  37. }
  38. $query = new WC_Product_Query( $args );
  39. return $query->get_products();
  40. }
  41. /**
  42. * Main function for returning products, uses the WC_Product_Factory class.
  43. *
  44. * @since 2.2.0
  45. *
  46. * @param mixed $the_product Post object or post ID of the product.
  47. * @param array $deprecated Previously used to pass arguments to the factory, e.g. to force a type.
  48. * @return WC_Product|null|false
  49. */
  50. function wc_get_product( $the_product = false, $deprecated = array() ) {
  51. if ( ! did_action( 'woocommerce_init' ) ) {
  52. /* translators: 1: wc_get_product 2: woocommerce_init */
  53. wc_doing_it_wrong( __FUNCTION__, sprintf( __( '%1$s should not be called before the %2$s action.', 'woocommerce' ), 'wc_get_product', 'woocommerce_init' ), '2.5' );
  54. return false;
  55. }
  56. if ( ! empty( $deprecated ) ) {
  57. wc_deprecated_argument( 'args', '3.0', 'Passing args to wc_get_product is deprecated. If you need to force a type, construct the product class directly.' );
  58. }
  59. return WC()->product_factory->get_product( $the_product, $deprecated );
  60. }
  61. /**
  62. * Returns whether or not SKUS are enabled.
  63. *
  64. * @return bool
  65. */
  66. function wc_product_sku_enabled() {
  67. return apply_filters( 'wc_product_sku_enabled', true );
  68. }
  69. /**
  70. * Returns whether or not product weights are enabled.
  71. *
  72. * @return bool
  73. */
  74. function wc_product_weight_enabled() {
  75. return apply_filters( 'wc_product_weight_enabled', true );
  76. }
  77. /**
  78. * Returns whether or not product dimensions (HxWxD) are enabled.
  79. *
  80. * @return bool
  81. */
  82. function wc_product_dimensions_enabled() {
  83. return apply_filters( 'wc_product_dimensions_enabled', true );
  84. }
  85. /**
  86. * Clear all transients cache for product data.
  87. *
  88. * @param int $post_id (default: 0).
  89. */
  90. function wc_delete_product_transients( $post_id = 0 ) {
  91. // Core transients.
  92. $transients_to_clear = array(
  93. 'wc_products_onsale',
  94. 'wc_featured_products',
  95. 'wc_outofstock_count',
  96. 'wc_low_stock_count',
  97. 'wc_count_comments',
  98. );
  99. // Transient names that include an ID.
  100. $post_transient_names = array(
  101. 'wc_product_children_',
  102. 'wc_var_prices_',
  103. 'wc_related_',
  104. 'wc_child_has_weight_',
  105. 'wc_child_has_dimensions_',
  106. );
  107. if ( $post_id > 0 ) {
  108. foreach ( $post_transient_names as $transient ) {
  109. $transients_to_clear[] = $transient . $post_id;
  110. }
  111. // Does this product have a parent?
  112. $product = wc_get_product( $post_id );
  113. if ( $product ) {
  114. if ( $product->get_parent_id() > 0 ) {
  115. wc_delete_product_transients( $product->get_parent_id() );
  116. }
  117. if ( 'variable' === $product->get_type() ) {
  118. wp_cache_delete(
  119. WC_Cache_Helper::get_cache_prefix( 'products' ) . 'product_variation_attributes_' . $product->get_id(),
  120. 'products'
  121. );
  122. }
  123. }
  124. }
  125. // Delete transients.
  126. foreach ( $transients_to_clear as $transient ) {
  127. delete_transient( $transient );
  128. }
  129. // Increments the transient version to invalidate cache.
  130. WC_Cache_Helper::get_transient_version( 'product', true );
  131. do_action( 'woocommerce_delete_product_transients', $post_id );
  132. }
  133. /**
  134. * Function that returns an array containing the IDs of the products that are on sale.
  135. *
  136. * @since 2.0
  137. * @return array
  138. */
  139. function wc_get_product_ids_on_sale() {
  140. // Load from cache.
  141. $product_ids_on_sale = get_transient( 'wc_products_onsale' );
  142. // Valid cache found.
  143. if ( false !== $product_ids_on_sale ) {
  144. return $product_ids_on_sale;
  145. }
  146. $data_store = WC_Data_Store::load( 'product' );
  147. $on_sale_products = $data_store->get_on_sale_products();
  148. $product_ids_on_sale = wp_parse_id_list( array_merge( wp_list_pluck( $on_sale_products, 'id' ), array_diff( wp_list_pluck( $on_sale_products, 'parent_id' ), array( 0 ) ) ) );
  149. set_transient( 'wc_products_onsale', $product_ids_on_sale, DAY_IN_SECONDS * 30 );
  150. return $product_ids_on_sale;
  151. }
  152. /**
  153. * Function that returns an array containing the IDs of the featured products.
  154. *
  155. * @since 2.1
  156. * @return array
  157. */
  158. function wc_get_featured_product_ids() {
  159. // Load from cache.
  160. $featured_product_ids = get_transient( 'wc_featured_products' );
  161. // Valid cache found.
  162. if ( false !== $featured_product_ids ) {
  163. return $featured_product_ids;
  164. }
  165. $data_store = WC_Data_Store::load( 'product' );
  166. $featured = $data_store->get_featured_product_ids();
  167. $product_ids = array_keys( $featured );
  168. $parent_ids = array_values( array_filter( $featured ) );
  169. $featured_product_ids = array_unique( array_merge( $product_ids, $parent_ids ) );
  170. set_transient( 'wc_featured_products', $featured_product_ids, DAY_IN_SECONDS * 30 );
  171. return $featured_product_ids;
  172. }
  173. /**
  174. * Filter to allow product_cat in the permalinks for products.
  175. *
  176. * @param string $permalink The existing permalink URL.
  177. * @param WP_Post $post WP_Post object.
  178. * @return string
  179. */
  180. function wc_product_post_type_link( $permalink, $post ) {
  181. // Abort if post is not a product.
  182. if ( 'product' !== $post->post_type ) {
  183. return $permalink;
  184. }
  185. // Abort early if the placeholder rewrite tag isn't in the generated URL.
  186. if ( false === strpos( $permalink, '%' ) ) {
  187. return $permalink;
  188. }
  189. // Get the custom taxonomy terms in use by this post.
  190. $terms = get_the_terms( $post->ID, 'product_cat' );
  191. if ( ! empty( $terms ) ) {
  192. if ( function_exists( 'wp_list_sort' ) ) {
  193. $terms = wp_list_sort( $terms, 'term_id', 'ASC' );
  194. } else {
  195. usort( $terms, '_usort_terms_by_ID' );
  196. }
  197. $category_object = apply_filters( 'wc_product_post_type_link_product_cat', $terms[0], $terms, $post );
  198. $category_object = get_term( $category_object, 'product_cat' );
  199. $product_cat = $category_object->slug;
  200. if ( $category_object->parent ) {
  201. $ancestors = get_ancestors( $category_object->term_id, 'product_cat' );
  202. foreach ( $ancestors as $ancestor ) {
  203. $ancestor_object = get_term( $ancestor, 'product_cat' );
  204. $product_cat = $ancestor_object->slug . '/' . $product_cat;
  205. }
  206. }
  207. } else {
  208. // If no terms are assigned to this post, use a string instead (can't leave the placeholder there).
  209. $product_cat = _x( 'uncategorized', 'slug', 'woocommerce' );
  210. }
  211. $find = array(
  212. '%year%',
  213. '%monthnum%',
  214. '%day%',
  215. '%hour%',
  216. '%minute%',
  217. '%second%',
  218. '%post_id%',
  219. '%category%',
  220. '%product_cat%',
  221. );
  222. $replace = array(
  223. date_i18n( 'Y', strtotime( $post->post_date ) ),
  224. date_i18n( 'm', strtotime( $post->post_date ) ),
  225. date_i18n( 'd', strtotime( $post->post_date ) ),
  226. date_i18n( 'H', strtotime( $post->post_date ) ),
  227. date_i18n( 'i', strtotime( $post->post_date ) ),
  228. date_i18n( 's', strtotime( $post->post_date ) ),
  229. $post->ID,
  230. $product_cat,
  231. $product_cat,
  232. );
  233. $permalink = str_replace( $find, $replace, $permalink );
  234. return $permalink;
  235. }
  236. add_filter( 'post_type_link', 'wc_product_post_type_link', 10, 2 );
  237. /**
  238. * Get the placeholder image URL for products etc.
  239. *
  240. * @access public
  241. * @return string
  242. */
  243. function wc_placeholder_img_src() {
  244. return apply_filters( 'woocommerce_placeholder_img_src', WC()->plugin_url() . '/assets/images/placeholder.png' );
  245. }
  246. /**
  247. * Get the placeholder image.
  248. *
  249. * @access public
  250. *
  251. * @param string $size Image size.
  252. *
  253. * @return string
  254. */
  255. function wc_placeholder_img( $size = 'woocommerce_thumbnail' ) {
  256. $dimensions = wc_get_image_size( $size );
  257. return apply_filters( 'woocommerce_placeholder_img', '<img src="' . wc_placeholder_img_src() . '" alt="' . esc_attr__( 'Placeholder', 'woocommerce' ) . '" width="' . esc_attr( $dimensions['width'] ) . '" class="woocommerce-placeholder wp-post-image" height="' . esc_attr( $dimensions['height'] ) . '" />', $size, $dimensions );
  258. }
  259. /**
  260. * Variation Formatting.
  261. *
  262. * Gets a formatted version of variation data or item meta.
  263. *
  264. * @param array|WC_Product_Variation $variation Variation object.
  265. * @param bool $flat Should this be a flat list or HTML list? (default: false).
  266. * @param bool $include_names include attribute names/labels in the list.
  267. * @param bool $skip_attributes_in_name Do not list attributes already part of the variation name.
  268. * @return string
  269. */
  270. function wc_get_formatted_variation( $variation, $flat = false, $include_names = true, $skip_attributes_in_name = false ) {
  271. $return = '';
  272. if ( is_a( $variation, 'WC_Product_Variation' ) ) {
  273. $variation_attributes = $variation->get_attributes();
  274. $product = $variation;
  275. $variation_name = $variation->get_name();
  276. } else {
  277. $product = false;
  278. $variation_name = '';
  279. // Remove attribute_ prefix from names.
  280. $variation_attributes = array();
  281. if ( is_array( $variation ) ) {
  282. foreach ( $variation as $key => $value ) {
  283. $variation_attributes[ str_replace( 'attribute_', '', $key ) ] = $value;
  284. }
  285. }
  286. }
  287. $list_type = $include_names ? 'dl' : 'ul';
  288. if ( is_array( $variation_attributes ) ) {
  289. if ( ! $flat ) {
  290. $return = '<' . $list_type . ' class="variation">';
  291. }
  292. $variation_list = array();
  293. foreach ( $variation_attributes as $name => $value ) {
  294. // If this is a term slug, get the term's nice name.
  295. if ( taxonomy_exists( $name ) ) {
  296. $term = get_term_by( 'slug', $value, $name );
  297. if ( ! is_wp_error( $term ) && ! empty( $term->name ) ) {
  298. $value = $term->name;
  299. }
  300. }
  301. // Do not list attributes already part of the variation name.
  302. if ( '' === $value || ( $skip_attributes_in_name && wc_is_attribute_in_product_name( $value, $variation_name ) ) ) {
  303. continue;
  304. }
  305. if ( $include_names ) {
  306. if ( $flat ) {
  307. $variation_list[] = wc_attribute_label( $name, $product ) . ': ' . rawurldecode( $value );
  308. } else {
  309. $variation_list[] = '<dt>' . wc_attribute_label( $name, $product ) . ':</dt><dd>' . rawurldecode( $value ) . '</dd>';
  310. }
  311. } else {
  312. if ( $flat ) {
  313. $variation_list[] = rawurldecode( $value );
  314. } else {
  315. $variation_list[] = '<li>' . rawurldecode( $value ) . '</li>';
  316. }
  317. }
  318. }
  319. if ( $flat ) {
  320. $return .= implode( ', ', $variation_list );
  321. } else {
  322. $return .= implode( '', $variation_list );
  323. }
  324. if ( ! $flat ) {
  325. $return .= '</' . $list_type . '>';
  326. }
  327. }
  328. return $return;
  329. }
  330. /**
  331. * Function which handles the start and end of scheduled sales via cron.
  332. */
  333. function wc_scheduled_sales() {
  334. $data_store = WC_Data_Store::load( 'product' );
  335. // Sales which are due to start.
  336. $product_ids = $data_store->get_starting_sales();
  337. if ( $product_ids ) {
  338. do_action( 'wc_before_products_starting_sales', $product_ids );
  339. foreach ( $product_ids as $product_id ) {
  340. $product = wc_get_product( $product_id );
  341. if ( $product ) {
  342. $sale_price = $product->get_sale_price();
  343. if ( $sale_price ) {
  344. $product->set_price( $sale_price );
  345. $product->set_date_on_sale_from( '' );
  346. } else {
  347. $product->set_date_on_sale_to( '' );
  348. $product->set_date_on_sale_from( '' );
  349. }
  350. $product->save();
  351. }
  352. }
  353. do_action( 'wc_after_products_starting_sales', $product_ids );
  354. delete_transient( 'wc_products_onsale' );
  355. }
  356. // Sales which are due to end.
  357. $product_ids = $data_store->get_ending_sales();
  358. if ( $product_ids ) {
  359. do_action( 'wc_before_products_ending_sales', $product_ids );
  360. foreach ( $product_ids as $product_id ) {
  361. $product = wc_get_product( $product_id );
  362. if ( $product ) {
  363. $regular_price = $product->get_regular_price();
  364. $product->set_price( $regular_price );
  365. $product->set_sale_price( '' );
  366. $product->set_date_on_sale_to( '' );
  367. $product->set_date_on_sale_from( '' );
  368. $product->save();
  369. }
  370. }
  371. do_action( 'wc_after_products_ending_sales', $product_ids );
  372. WC_Cache_Helper::get_transient_version( 'product', true );
  373. delete_transient( 'wc_products_onsale' );
  374. }
  375. }
  376. add_action( 'woocommerce_scheduled_sales', 'wc_scheduled_sales' );
  377. /**
  378. * Get attachment image attributes.
  379. *
  380. * @access public
  381. * @param array $attr Image attributes.
  382. * @return array
  383. */
  384. function wc_get_attachment_image_attributes( $attr ) {
  385. if ( strstr( $attr['src'][0], 'woocommerce_uploads/' ) ) {
  386. $attr['src'][0] = wc_placeholder_img_src();
  387. }
  388. return $attr;
  389. }
  390. add_filter( 'wp_get_attachment_image_attributes', 'wc_get_attachment_image_attributes' );
  391. /**
  392. * Prepare attachment for JavaScript.
  393. *
  394. * @access public
  395. * @param array $response JS version of a attachment post object.
  396. * @return array
  397. */
  398. function wc_prepare_attachment_for_js( $response ) {
  399. if ( isset( $response['url'] ) && strstr( $response['url'], 'woocommerce_uploads/' ) ) {
  400. $response['full']['url'] = wc_placeholder_img_src();
  401. if ( isset( $response['sizes'] ) ) {
  402. foreach ( $response['sizes'] as $size => $value ) {
  403. $response['sizes'][ $size ]['url'] = wc_placeholder_img_src();
  404. }
  405. }
  406. }
  407. return $response;
  408. }
  409. add_filter( 'wp_prepare_attachment_for_js', 'wc_prepare_attachment_for_js' );
  410. /**
  411. * Track product views.
  412. */
  413. function wc_track_product_view() {
  414. if ( ! is_singular( 'product' ) || ! is_active_widget( false, false, 'woocommerce_recently_viewed_products', true ) ) {
  415. return;
  416. }
  417. global $post;
  418. if ( empty( $_COOKIE['woocommerce_recently_viewed'] ) ) { // @codingStandardsIgnoreLine.
  419. $viewed_products = array();
  420. } else {
  421. $viewed_products = wp_parse_id_list( (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) ); // @codingStandardsIgnoreLine.
  422. }
  423. // Unset if already in viewed products list.
  424. $keys = array_flip( $viewed_products );
  425. if ( isset( $keys[ $post->ID ] ) ) {
  426. unset( $viewed_products[ $keys[ $post->ID ] ] );
  427. }
  428. $viewed_products[] = $post->ID;
  429. if ( count( $viewed_products ) > 15 ) {
  430. array_shift( $viewed_products );
  431. }
  432. // Store for session only.
  433. wc_setcookie( 'woocommerce_recently_viewed', implode( '|', $viewed_products ) );
  434. }
  435. add_action( 'template_redirect', 'wc_track_product_view', 20 );
  436. /**
  437. * Get product types.
  438. *
  439. * @since 2.2
  440. * @return array
  441. */
  442. function wc_get_product_types() {
  443. return (array) apply_filters( 'product_type_selector', array(
  444. 'simple' => __( 'Simple product', 'woocommerce' ),
  445. 'grouped' => __( 'Grouped product', 'woocommerce' ),
  446. 'external' => __( 'External/Affiliate product', 'woocommerce' ),
  447. 'variable' => __( 'Variable product', 'woocommerce' ),
  448. ) );
  449. }
  450. /**
  451. * Check if product sku is unique.
  452. *
  453. * @since 2.2
  454. * @param int $product_id Product ID.
  455. * @param string $sku Product SKU.
  456. * @return bool
  457. */
  458. function wc_product_has_unique_sku( $product_id, $sku ) {
  459. $data_store = WC_Data_Store::load( 'product' );
  460. $sku_found = $data_store->is_existing_sku( $product_id, $sku );
  461. if ( apply_filters( 'wc_product_has_unique_sku', $sku_found, $product_id, $sku ) ) {
  462. return false;
  463. }
  464. return true;
  465. }
  466. /**
  467. * Force a unique SKU.
  468. *
  469. * @since 3.0.0
  470. * @param integer $product_id Product ID.
  471. */
  472. function wc_product_force_unique_sku( $product_id ) {
  473. $product = wc_get_product( $product_id );
  474. $current_sku = $product ? $product->get_sku( 'edit' ) : '';
  475. if ( $current_sku ) {
  476. try {
  477. $new_sku = wc_product_generate_unique_sku( $product_id, $current_sku );
  478. if ( $current_sku !== $new_sku ) {
  479. $product->set_sku( $new_sku );
  480. $product->save();
  481. }
  482. } catch ( Exception $e ) {} // @codingStandardsIgnoreLine.
  483. }
  484. }
  485. /**
  486. * Recursively appends a suffix until a unique SKU is found.
  487. *
  488. * @since 3.0.0
  489. * @param integer $product_id Product ID.
  490. * @param string $sku Product SKU.
  491. * @param integer $index An optional index that can be added to the product SKU.
  492. * @return string
  493. */
  494. function wc_product_generate_unique_sku( $product_id, $sku, $index = 0 ) {
  495. $generated_sku = 0 < $index ? $sku . '-' . $index : $sku;
  496. if ( ! wc_product_has_unique_sku( $product_id, $generated_sku ) ) {
  497. $generated_sku = wc_product_generate_unique_sku( $product_id, $sku, ( $index + 1 ) );
  498. }
  499. return $generated_sku;
  500. }
  501. /**
  502. * Get product ID by SKU.
  503. *
  504. * @since 2.3.0
  505. * @param string $sku Product SKU.
  506. * @return int
  507. */
  508. function wc_get_product_id_by_sku( $sku ) {
  509. $data_store = WC_Data_Store::load( 'product' );
  510. return $data_store->get_product_id_by_sku( $sku );
  511. }
  512. /**
  513. * Get attibutes/data for an individual variation from the database and maintain it's integrity.
  514. *
  515. * @since 2.4.0
  516. * @param int $variation_id Variation ID.
  517. * @return array
  518. */
  519. function wc_get_product_variation_attributes( $variation_id ) {
  520. // Build variation data from meta.
  521. $all_meta = get_post_meta( $variation_id );
  522. $parent_id = wp_get_post_parent_id( $variation_id );
  523. $parent_attributes = array_filter( (array) get_post_meta( $parent_id, '_product_attributes', true ) );
  524. $found_parent_attributes = array();
  525. $variation_attributes = array();
  526. // Compare to parent variable product attributes and ensure they match.
  527. foreach ( $parent_attributes as $attribute_name => $options ) {
  528. if ( ! empty( $options['is_variation'] ) ) {
  529. $attribute = 'attribute_' . sanitize_title( $attribute_name );
  530. $found_parent_attributes[] = $attribute;
  531. if ( ! array_key_exists( $attribute, $variation_attributes ) ) {
  532. $variation_attributes[ $attribute ] = ''; // Add it - 'any' will be asumed.
  533. }
  534. }
  535. }
  536. // Get the variation attributes from meta.
  537. foreach ( $all_meta as $name => $value ) {
  538. // Only look at valid attribute meta, and also compare variation level attributes and remove any which do not exist at parent level.
  539. if ( 0 !== strpos( $name, 'attribute_' ) || ! in_array( $name, $found_parent_attributes, true ) ) {
  540. unset( $variation_attributes[ $name ] );
  541. continue;
  542. }
  543. /**
  544. * Pre 2.4 handling where 'slugs' were saved instead of the full text attribute.
  545. * Attempt to get full version of the text attribute from the parent.
  546. */
  547. if ( sanitize_title( $value[0] ) === $value[0] && version_compare( get_post_meta( $parent_id, '_product_version', true ), '2.4.0', '<' ) ) {
  548. foreach ( $parent_attributes as $attribute ) {
  549. if ( 'attribute_' . sanitize_title( $attribute['name'] ) !== $name ) {
  550. continue;
  551. }
  552. $text_attributes = wc_get_text_attributes( $attribute['value'] );
  553. foreach ( $text_attributes as $text_attribute ) {
  554. if ( sanitize_title( $text_attribute ) === $value[0] ) {
  555. $value[0] = $text_attribute;
  556. break;
  557. }
  558. }
  559. }
  560. }
  561. $variation_attributes[ $name ] = $value[0];
  562. }
  563. return $variation_attributes;
  564. }
  565. /**
  566. * Get all product cats for a product by ID, including hierarchy
  567. *
  568. * @since 2.5.0
  569. * @param int $product_id Product ID.
  570. * @return array
  571. */
  572. function wc_get_product_cat_ids( $product_id ) {
  573. $product_cats = wc_get_product_term_ids( $product_id, 'product_cat' );
  574. foreach ( $product_cats as $product_cat ) {
  575. $product_cats = array_merge( $product_cats, get_ancestors( $product_cat, 'product_cat' ) );
  576. }
  577. return $product_cats;
  578. }
  579. /**
  580. * Gets data about an attachment, such as alt text and captions.
  581. *
  582. * @since 2.6.0
  583. *
  584. * @param int|null $attachment_id Attachment ID.
  585. * @param WC_Product|bool $product WC_Product object.
  586. *
  587. * @return array
  588. */
  589. function wc_get_product_attachment_props( $attachment_id = null, $product = false ) {
  590. $props = array(
  591. 'title' => '',
  592. 'caption' => '',
  593. 'url' => '',
  594. 'alt' => '',
  595. 'src' => '',
  596. 'srcset' => false,
  597. 'sizes' => false,
  598. );
  599. $attachment = get_post( $attachment_id );
  600. if ( $attachment ) {
  601. $props['title'] = wp_strip_all_tags( $attachment->post_title );
  602. $props['caption'] = wp_strip_all_tags( $attachment->post_excerpt );
  603. $props['url'] = wp_get_attachment_url( $attachment_id );
  604. // Alt text.
  605. $alt_text = array( wp_strip_all_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ), $props['caption'], wp_strip_all_tags( $attachment->post_title ) );
  606. if ( $product && $product instanceof WC_Product ) {
  607. $alt_text[] = wp_strip_all_tags( get_the_title( $product->get_id() ) );
  608. }
  609. $alt_text = array_filter( $alt_text );
  610. $props['alt'] = isset( $alt_text[0] ) ? $alt_text[0] : '';
  611. // Large version.
  612. $src = wp_get_attachment_image_src( $attachment_id, 'full' );
  613. $props['full_src'] = $src[0];
  614. $props['full_src_w'] = $src[1];
  615. $props['full_src_h'] = $src[2];
  616. // Gallery thumbnail.
  617. $gallery_thumbnail = wc_get_image_size( 'gallery_thumbnail' );
  618. $gallery_thumbnail_size = apply_filters( 'woocommerce_gallery_thumbnail_size', array( $gallery_thumbnail['width'], $gallery_thumbnail['height'] ) );
  619. $src = wp_get_attachment_image_src( $attachment_id, $gallery_thumbnail_size );
  620. $props['gallery_thumbnail_src'] = $src[0];
  621. $props['gallery_thumbnail_src_w'] = $src[1];
  622. $props['gallery_thumbnail_src_h'] = $src[2];
  623. // Thumbnail version.
  624. $src = wp_get_attachment_image_src( $attachment_id, 'woocommerce_thumbnail' );
  625. $props['thumb_src'] = $src[0];
  626. $props['thumb_src_w'] = $src[1];
  627. $props['thumb_src_h'] = $src[2];
  628. // Image source.
  629. $src = wp_get_attachment_image_src( $attachment_id, 'woocommerce_single' );
  630. $props['src'] = $src[0];
  631. $props['src_w'] = $src[1];
  632. $props['src_h'] = $src[2];
  633. $props['srcset'] = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $attachment_id, 'woocommerce_single' ) : false;
  634. $props['sizes'] = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $attachment_id, 'woocommerce_single' ) : false;
  635. }
  636. return $props;
  637. }
  638. /**
  639. * Get product visibility options.
  640. *
  641. * @since 3.0.0
  642. * @return array
  643. */
  644. function wc_get_product_visibility_options() {
  645. return apply_filters( 'woocommerce_product_visibility_options', array(
  646. 'visible' => __( 'Shop and search results', 'woocommerce' ),
  647. 'catalog' => __( 'Shop only', 'woocommerce' ),
  648. 'search' => __( 'Search results only', 'woocommerce' ),
  649. 'hidden' => __( 'Hidden', 'woocommerce' ),
  650. ) );
  651. }
  652. /**
  653. * Get min/max price meta query args.
  654. *
  655. * @since 3.0.0
  656. * @param array $args Min price and max price arguments.
  657. * @return array
  658. */
  659. function wc_get_min_max_price_meta_query( $args ) {
  660. $min = isset( $args['min_price'] ) ? floatval( $args['min_price'] ) : 0;
  661. $max = isset( $args['max_price'] ) ? floatval( $args['max_price'] ) : 9999999999;
  662. /**
  663. * Adjust if the store taxes are not displayed how they are stored.
  664. * Kicks in when prices excluding tax are displayed including tax.
  665. */
  666. if ( wc_tax_enabled() && 'incl' === get_option( 'woocommerce_tax_display_shop' ) && ! wc_prices_include_tax() ) {
  667. $tax_classes = array_merge( array( '' ), WC_Tax::get_tax_classes() );
  668. $class_min = $min;
  669. $class_max = $max;
  670. foreach ( $tax_classes as $tax_class ) {
  671. $tax_rates = WC_Tax::get_rates( $tax_class );
  672. if ( $tax_rates ) {
  673. $class_min = $min + WC_Tax::get_tax_total( WC_Tax::calc_exclusive_tax( $min, $tax_rates ) );
  674. $class_max = $max - WC_Tax::get_tax_total( WC_Tax::calc_exclusive_tax( $max, $tax_rates ) );
  675. }
  676. }
  677. $min = $class_min;
  678. $max = $class_max;
  679. }
  680. return array(
  681. 'key' => '_price',
  682. 'value' => array( $min, $max ),
  683. 'compare' => 'BETWEEN',
  684. 'type' => 'DECIMAL(10,' . wc_get_price_decimals() . ')',
  685. );
  686. }
  687. /**
  688. * Get product tax class options.
  689. *
  690. * @since 3.0.0
  691. * @return array
  692. */
  693. function wc_get_product_tax_class_options() {
  694. $tax_classes = WC_Tax::get_tax_classes();
  695. $tax_class_options = array();
  696. $tax_class_options[''] = __( 'Standard', 'woocommerce' );
  697. if ( ! empty( $tax_classes ) ) {
  698. foreach ( $tax_classes as $class ) {
  699. $tax_class_options[ sanitize_title( $class ) ] = $class;
  700. }
  701. }
  702. return $tax_class_options;
  703. }
  704. /**
  705. * Get stock status options.
  706. *
  707. * @since 3.0.0
  708. * @return array
  709. */
  710. function wc_get_product_stock_status_options() {
  711. return array(
  712. 'instock' => __( 'In stock', 'woocommerce' ),
  713. 'outofstock' => __( 'Out of stock', 'woocommerce' ),
  714. 'onbackorder' => __( 'On backorder', 'woocommerce' ),
  715. );
  716. }
  717. /**
  718. * Get backorder options.
  719. *
  720. * @since 3.0.0
  721. * @return array
  722. */
  723. function wc_get_product_backorder_options() {
  724. return array(
  725. 'no' => __( 'Do not allow', 'woocommerce' ),
  726. 'notify' => __( 'Allow, but notify customer', 'woocommerce' ),
  727. 'yes' => __( 'Allow', 'woocommerce' ),
  728. );
  729. }
  730. /**
  731. * Get related products based on product category and tags.
  732. *
  733. * @since 3.0.0
  734. * @param int $product_id Product ID.
  735. * @param int $limit Limit of results.
  736. * @param array $exclude_ids Exclude IDs from the results.
  737. * @return array
  738. */
  739. function wc_get_related_products( $product_id, $limit = 5, $exclude_ids = array() ) {
  740. $product_id = absint( $product_id );
  741. $limit = $limit >= -1 ? $limit : 5;
  742. $exclude_ids = array_merge( array( 0, $product_id ), $exclude_ids );
  743. $transient_name = 'wc_related_' . $product_id;
  744. $query_args = http_build_query( array(
  745. 'limit' => $limit,
  746. 'exclude_ids' => $exclude_ids,
  747. ) );
  748. $transient = get_transient( $transient_name );
  749. $related_posts = $transient && isset( $transient[ $query_args ] ) ? $transient[ $query_args ] : false;
  750. // We want to query related posts if they are not cached, or we don't have enough.
  751. if ( false === $related_posts || count( $related_posts ) < $limit ) {
  752. $cats_array = apply_filters( 'woocommerce_product_related_posts_relate_by_category', true, $product_id ) ? apply_filters( 'woocommerce_get_related_product_cat_terms', wc_get_product_term_ids( $product_id, 'product_cat' ), $product_id ) : array();
  753. $tags_array = apply_filters( 'woocommerce_product_related_posts_relate_by_tag', true, $product_id ) ? apply_filters( 'woocommerce_get_related_product_tag_terms', wc_get_product_term_ids( $product_id, 'product_tag' ), $product_id ) : array();
  754. // Don't bother if none are set, unless woocommerce_product_related_posts_force_display is set to true in which case all products are related.
  755. if ( empty( $cats_array ) && empty( $tags_array ) && ! apply_filters( 'woocommerce_product_related_posts_force_display', false, $product_id ) ) {
  756. $related_posts = array();
  757. } else {
  758. $data_store = WC_Data_Store::load( 'product' );
  759. $related_posts = $data_store->get_related_products( $cats_array, $tags_array, $exclude_ids, $limit + 10, $product_id );
  760. }
  761. if ( $transient ) {
  762. $transient[ $query_args ] = $related_posts;
  763. } else {
  764. $transient = array( $query_args => $related_posts );
  765. }
  766. set_transient( $transient_name, $transient, DAY_IN_SECONDS );
  767. }
  768. $related_posts = apply_filters( 'woocommerce_related_products', $related_posts, $product_id, array(
  769. 'limit' => $limit,
  770. 'excluded_ids' => $exclude_ids,
  771. ) );
  772. shuffle( $related_posts );
  773. return array_slice( $related_posts, 0, $limit );
  774. }
  775. /**
  776. * Retrieves product term ids for a taxonomy.
  777. *
  778. * @since 3.0.0
  779. * @param int $product_id Product ID.
  780. * @param string $taxonomy Taxonomy slug.
  781. * @return array
  782. */
  783. function wc_get_product_term_ids( $product_id, $taxonomy ) {
  784. $terms = get_the_terms( $product_id, $taxonomy );
  785. return ( empty( $terms ) || is_wp_error( $terms ) ) ? array() : wp_list_pluck( $terms, 'term_id' );
  786. }
  787. /**
  788. * For a given product, and optionally price/qty, work out the price with tax included, based on store settings.
  789. *
  790. * @since 3.0.0
  791. * @param WC_Product $product WC_Product object.
  792. * @param array $args Optional arguments to pass product quantity and price.
  793. * @return float
  794. */
  795. function wc_get_price_including_tax( $product, $args = array() ) {
  796. $args = wp_parse_args( $args, array(
  797. 'qty' => '',
  798. 'price' => '',
  799. ) );
  800. $price = '' !== $args['price'] ? max( 0.0, (float) $args['price'] ) : $product->get_price();
  801. $qty = '' !== $args['qty'] ? max( 0.0, (float) $args['qty'] ) : 1;
  802. if ( '' === $price ) {
  803. return '';
  804. } elseif ( empty( $qty ) ) {
  805. return 0.0;
  806. }
  807. $line_price = $price * $qty;
  808. $return_price = $line_price;
  809. if ( $product->is_taxable() ) {
  810. if ( ! wc_prices_include_tax() ) {
  811. $tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
  812. $taxes = WC_Tax::calc_tax( $line_price, $tax_rates, false );
  813. $tax_amount = WC_Tax::get_tax_total( $taxes );
  814. $return_price = round( $line_price + $tax_amount, wc_get_price_decimals() );
  815. } else {
  816. $tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
  817. $base_tax_rates = WC_Tax::get_base_tax_rates( $product->get_tax_class( 'unfiltered' ) );
  818. /**
  819. * If the customer is excempt from VAT, remove the taxes here.
  820. * Either remove the base or the user taxes depending on woocommerce_adjust_non_base_location_prices setting.
  821. */
  822. if ( ! empty( WC()->customer ) && WC()->customer->get_is_vat_exempt() ) { // @codingStandardsIgnoreLine.
  823. $remove_taxes = apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ? WC_Tax::calc_tax( $line_price, $base_tax_rates, true ) : WC_Tax::calc_tax( $line_price, $tax_rates, true );
  824. $remove_tax = array_sum( $remove_taxes );
  825. $return_price = round( $line_price - $remove_tax, wc_get_price_decimals() );
  826. /**
  827. * The woocommerce_adjust_non_base_location_prices filter can stop base taxes being taken off when dealing with out of base locations.
  828. * e.g. If a product costs 10 including tax, all users will pay 10 regardless of location and taxes.
  829. * This feature is experimental @since 2.4.7 and may change in the future. Use at your risk.
  830. */
  831. } elseif ( $tax_rates !== $base_tax_rates && apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ) {
  832. $base_taxes = WC_Tax::calc_tax( $line_price, $base_tax_rates, true );
  833. $modded_taxes = WC_Tax::calc_tax( $line_price - array_sum( $base_taxes ), $tax_rates, false );
  834. $return_price = round( $line_price - array_sum( $base_taxes ) + wc_round_tax_total( array_sum( $modded_taxes ), wc_get_price_decimals() ), wc_get_price_decimals() );
  835. }
  836. }
  837. }
  838. return apply_filters( 'woocommerce_get_price_including_tax', $return_price, $qty, $product );
  839. }
  840. /**
  841. * For a given product, and optionally price/qty, work out the price with tax excluded, based on store settings.
  842. *
  843. * @since 3.0.0
  844. * @param WC_Product $product WC_Product object.
  845. * @param array $args Optional arguments to pass product quantity and price.
  846. * @return float
  847. */
  848. function wc_get_price_excluding_tax( $product, $args = array() ) {
  849. $args = wp_parse_args( $args, array(
  850. 'qty' => '',
  851. 'price' => '',
  852. ) );
  853. $price = '' !== $args['price'] ? max( 0.0, (float) $args['price'] ) : $product->get_price();
  854. $qty = '' !== $args['qty'] ? max( 0.0, (float) $args['qty'] ) : 1;
  855. if ( '' === $price ) {
  856. return '';
  857. } elseif ( empty( $qty ) ) {
  858. return 0.0;
  859. }
  860. $line_price = $price * $qty;
  861. if ( $product->is_taxable() && wc_prices_include_tax() ) {
  862. $tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
  863. $base_tax_rates = WC_Tax::get_base_tax_rates( $product->get_tax_class( 'unfiltered' ) );
  864. $remove_taxes = apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ? WC_Tax::calc_tax( $line_price, $base_tax_rates, true ) : WC_Tax::calc_tax( $line_price, $tax_rates, true );
  865. $return_price = $line_price - array_sum( $remove_taxes );
  866. } else {
  867. $return_price = $line_price;
  868. }
  869. return apply_filters( 'woocommerce_get_price_excluding_tax', $return_price, $qty, $product );
  870. }
  871. /**
  872. * Returns the price including or excluding tax, based on the 'woocommerce_tax_display_shop' setting.
  873. *
  874. * @since 3.0.0
  875. * @param WC_Product $product WC_Product object.
  876. * @param array $args Optional arguments to pass product quantity and price.
  877. * @return float
  878. */
  879. function wc_get_price_to_display( $product, $args = array() ) {
  880. $args = wp_parse_args( $args, array(
  881. 'qty' => 1,
  882. 'price' => $product->get_price(),
  883. ) );
  884. $price = $args['price'];
  885. $qty = $args['qty'];
  886. return 'incl' === get_option( 'woocommerce_tax_display_shop' ) ?
  887. wc_get_price_including_tax( $product, array(
  888. 'qty' => $qty,
  889. 'price' => $price,
  890. ) ) :
  891. wc_get_price_excluding_tax( $product, array(
  892. 'qty' => $qty,
  893. 'price' => $price,
  894. ) );
  895. }
  896. /**
  897. * Returns the product categories in a list.
  898. *
  899. * @param int $product_id Product ID.
  900. * @param string $sep (default: ', ').
  901. * @param string $before (default: '').
  902. * @param string $after (default: '').
  903. * @return string
  904. */
  905. function wc_get_product_category_list( $product_id, $sep = ', ', $before = '', $after = '' ) {
  906. return get_the_term_list( $product_id, 'product_cat', $before, $sep, $after );
  907. }
  908. /**
  909. * Returns the product tags in a list.
  910. *
  911. * @param int $product_id Product ID.
  912. * @param string $sep (default: ', ').
  913. * @param string $before (default: '').
  914. * @param string $after (default: '').
  915. * @return string
  916. */
  917. function wc_get_product_tag_list( $product_id, $sep = ', ', $before = '', $after = '' ) {
  918. return get_the_term_list( $product_id, 'product_tag', $before, $sep, $after );
  919. }
  920. /**
  921. * Callback for array filter to get visible only.
  922. *
  923. * @since 3.0.0
  924. * @param WC_Product $product WC_Product object.
  925. * @return bool
  926. */
  927. function wc_products_array_filter_visible( $product ) {
  928. return $product && is_a( $product, 'WC_Product' ) && $product->is_visible();
  929. }
  930. /**
  931. * Callback for array filter to get visible grouped products only.
  932. *
  933. * @since 3.1.0
  934. * @param WC_Product $product WC_Product object.
  935. * @return bool
  936. */
  937. function wc_products_array_filter_visible_grouped( $product ) {
  938. return $product && is_a( $product, 'WC_Product' ) && ( 'publish' === $product->get_status() || current_user_can( 'edit_product', $product->get_id() ) );
  939. }
  940. /**
  941. * Callback for array filter to get products the user can edit only.
  942. *
  943. * @since 3.0.0
  944. * @param WC_Product $product WC_Product object.
  945. * @return bool
  946. */
  947. function wc_products_array_filter_editable( $product ) {
  948. return $product && is_a( $product, 'WC_Product' ) && current_user_can( 'edit_product', $product->get_id() );
  949. }
  950. /**
  951. * Callback for array filter to get products the user can view only.
  952. *
  953. * @since 3.4.0
  954. * @param WC_Product $product WC_Product object.
  955. * @return bool
  956. */
  957. function wc_products_array_filter_readable( $product ) {
  958. return $product && is_a( $product, 'WC_Product' ) && current_user_can( 'read_product', $product->get_id() );
  959. }
  960. /**
  961. * Sort an array of products by a value.
  962. *
  963. * @since 3.0.0
  964. *
  965. * @param array $products List of products to be ordered.
  966. * @param string $orderby Optional order criteria.
  967. * @param string $order Ascending or descending order.
  968. *
  969. * @return array
  970. */
  971. function wc_products_array_orderby( $products, $orderby = 'date', $order = 'desc' ) {
  972. $orderby = strtolower( $orderby );
  973. $order = strtolower( $order );
  974. switch ( $orderby ) {
  975. case 'title':
  976. case 'id':
  977. case 'date':
  978. case 'modified':
  979. case 'menu_order':
  980. case 'price':
  981. usort( $products, 'wc_products_array_orderby_' . $orderby );
  982. break;
  983. default:
  984. shuffle( $products );
  985. break;
  986. }
  987. if ( 'desc' === $order ) {
  988. $products = array_reverse( $products );
  989. }
  990. return $products;
  991. }
  992. /**
  993. * Sort by title.
  994. *
  995. * @since 3.0.0
  996. * @param WC_Product $a First WC_Product object.
  997. * @param WC_Product $b Second WC_Product object.
  998. * @return int
  999. */
  1000. function wc_products_array_orderby_title( $a, $b ) {
  1001. return strcasecmp( $a->get_name(), $b->get_name() );
  1002. }
  1003. /**
  1004. * Sort by id.
  1005. *
  1006. * @since 3.0.0
  1007. * @param WC_Product $a First WC_Product object.
  1008. * @param WC_Product $b Second WC_Product object.
  1009. * @return int
  1010. */
  1011. function wc_products_array_orderby_id( $a, $b ) {
  1012. if ( $a->get_id() === $b->get_id() ) {
  1013. return 0;
  1014. }
  1015. return ( $a->get_id() < $b->get_id() ) ? -1 : 1;
  1016. }
  1017. /**
  1018. * Sort by date.
  1019. *
  1020. * @since 3.0.0
  1021. * @param WC_Product $a First WC_Product object.
  1022. * @param WC_Product $b Second WC_Product object.
  1023. * @return int
  1024. */
  1025. function wc_products_array_orderby_date( $a, $b ) {
  1026. if ( $a->get_date_created() === $b->get_date_created() ) {
  1027. return 0;
  1028. }
  1029. return ( $a->get_date_created() < $b->get_date_created() ) ? -1 : 1;
  1030. }
  1031. /**
  1032. * Sort by modified.
  1033. *
  1034. * @since 3.0.0
  1035. * @param WC_Product $a First WC_Product object.
  1036. * @param WC_Product $b Second WC_Product object.
  1037. * @return int
  1038. */
  1039. function wc_products_array_orderby_modified( $a, $b ) {
  1040. if ( $a->get_date_modified() === $b->get_date_modified() ) {
  1041. return 0;
  1042. }
  1043. return ( $a->get_date_modified() < $b->get_date_modified() ) ? -1 : 1;
  1044. }
  1045. /**
  1046. * Sort by menu order.
  1047. *
  1048. * @since 3.0.0
  1049. * @param WC_Product $a First WC_Product object.
  1050. * @param WC_Product $b Second WC_Product object.
  1051. * @return int
  1052. */
  1053. function wc_products_array_orderby_menu_order( $a, $b ) {
  1054. if ( $a->get_menu_order() === $b->get_menu_order() ) {
  1055. return 0;
  1056. }
  1057. return ( $a->get_menu_order() < $b->get_menu_order() ) ? -1 : 1;
  1058. }
  1059. /**
  1060. * Sort by price low to high.
  1061. *
  1062. * @since 3.0.0
  1063. * @param WC_Product $a First WC_Product object.
  1064. * @param WC_Product $b Second WC_Product object.
  1065. * @return int
  1066. */
  1067. function wc_products_array_orderby_price( $a, $b ) {
  1068. if ( $a->get_price() === $b->get_price() ) {
  1069. return 0;
  1070. }
  1071. return ( $a->get_price() < $b->get_price() ) ? -1 : 1;
  1072. }
  1073. /**
  1074. * Queue a product for syncing at the end of the request.
  1075. *
  1076. * @param int $product_id Product ID.
  1077. */
  1078. function wc_deferred_product_sync( $product_id ) {
  1079. global $wc_deferred_product_sync;
  1080. if ( empty( $wc_deferred_product_sync ) ) {
  1081. $wc_deferred_product_sync = array();
  1082. }
  1083. $wc_deferred_product_sync[] = $product_id;
  1084. }