Guest User

class-wc-pb-product-prices.php

a guest
Oct 9th, 2020
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.91 KB | None | 0 0
  1. <?php
  2. /**
  3. * WC_PB_Product_Prices class
  4. *
  5. * @author SomewhereWarm <[email protected]>
  6. * @package WooCommerce Product Bundles
  7. * @since 5.0.0
  8. */
  9.  
  10. // Exit if accessed directly.
  11. if ( ! defined( 'ABSPATH' ) ) {
  12. exit;
  13. }
  14.  
  15. /**
  16. * Price functions and hooks.
  17. *
  18. * @class WC_PB_Product_Prices
  19. * @version 6.3.3
  20. */
  21. class WC_PB_Product_Prices {
  22.  
  23. /**
  24. * Bundled items whose prices are currently being filtered -- all states.
  25. *
  26. * @var WC_Bundled_Item
  27. */
  28. private static $bundled_item_pre;
  29.  
  30. /**
  31. * Bundled item whose prices are currently being filtered.
  32. *
  33. * @var WC_Bundled_Item
  34. */
  35. public static $bundled_item;
  36.  
  37. /**
  38. * Initialize.
  39. */
  40. public static function init() {
  41.  
  42. // Always-on price filters used in cart context.
  43. if ( 'filters' === self::get_bundled_cart_item_discount_method() ) {
  44.  
  45. add_filter( 'woocommerce_product_get_price', array( __CLASS__, 'filter_get_price_cart' ), 99, 2 );
  46. add_filter( 'woocommerce_product_get_sale_price', array( __CLASS__, 'filter_get_sale_price_cart' ), 99, 2 );
  47. add_filter( 'woocommerce_product_get_regular_price', array( __CLASS__, 'filter_get_regular_price_cart' ), 99, 2 );
  48.  
  49. add_filter( 'woocommerce_product_variation_get_price', array( __CLASS__, 'filter_get_price_cart' ), 99, 2 );
  50. add_filter( 'woocommerce_product_variation_get_sale_price', array( __CLASS__, 'filter_get_sale_price_cart' ), 99, 2 );
  51. add_filter( 'woocommerce_product_variation_get_regular_price', array( __CLASS__, 'filter_get_regular_price_cart' ), 99, 2 );
  52. }
  53. }
  54.  
  55. /*
  56. |--------------------------------------------------------------------------
  57. | Class methods.
  58. |--------------------------------------------------------------------------
  59. */
  60.  
  61. /**
  62. * A non-strict way to tell if a product's prices are being altered due to the presence of a parent "bundle".
  63. *
  64. * @since 6.0.0
  65. *
  66. * @param WC_Product $product
  67. * @param string $context
  68. * @return boolean
  69. */
  70. public static function is_bundled_pricing_context( $product, $context = 'any' ) {
  71.  
  72. if ( in_array( $context, array( 'any', 'catalog' ) ) ) {
  73. return self::$bundled_item && in_array( self::$bundled_item->get_product_id(), array( $product->get_id(), $product->get_parent_id() ) );
  74. } elseif ( in_array( $context, array( 'any', 'cart' ) ) ) {
  75. return isset( $product->bundled_cart_item );
  76. }
  77. }
  78.  
  79. /**
  80. * Method to use for calculating cart item discounts. Values: 'filters' | 'props'
  81. *
  82. * @since 6.0.0
  83. *
  84. * @return string $method
  85. */
  86. public static function get_bundled_cart_item_discount_method() {
  87. /**
  88. * 'woocommerce_bundled_cart_item_discount_method' filter.
  89. *
  90. * @since 6.0.0
  91. *
  92. * @param string $method Method to use for calculating cart item discounts. Values: 'filters' | 'props'.
  93. */
  94. $discount_method = apply_filters( 'woocommerce_bundled_cart_item_discount_method', 'filters' );
  95. return in_array( $discount_method, array( 'filters', 'props' ) ) ? $discount_method : 'filters';
  96. }
  97.  
  98. /**
  99. * Returns the incl/excl tax coefficients for calculating prices incl/excl tax on the client side.
  100. *
  101. * @since 5.7.6
  102. *
  103. * @param WC_Product $product
  104. * @return array
  105. */
  106. public static function get_tax_ratios( $product ) {
  107.  
  108. WC_PB_Product_Prices::extend_price_display_precision();
  109.  
  110. $ref_price = 1000.0;
  111. $ref_price_incl = wc_get_price_including_tax( $product, array( 'qty' => 1, 'price' => $ref_price ) );
  112. $ref_price_excl = wc_get_price_excluding_tax( $product, array( 'qty' => 1, 'price' => $ref_price ) );
  113.  
  114. WC_PB_Product_Prices::reset_price_display_precision();
  115.  
  116. return array(
  117. 'incl' => $ref_price_incl / $ref_price,
  118. 'excl' => $ref_price_excl / $ref_price
  119. );
  120. }
  121.  
  122. /**
  123. * Filters the 'woocommerce_price_num_decimals' option to use the internal WC rounding precision.
  124. */
  125. public static function extend_price_display_precision() {
  126. add_filter( 'option_woocommerce_price_num_decimals', array( 'WC_PB_Core_Compatibility', 'wc_get_rounding_precision' ) );
  127. }
  128.  
  129. /**
  130. * Reset applied filters to the 'woocommerce_price_num_decimals' option.
  131. */
  132. public static function reset_price_display_precision() {
  133. remove_filter( 'option_woocommerce_price_num_decimals', array( 'WC_PB_Core_Compatibility', 'wc_get_rounding_precision' ) );
  134. }
  135.  
  136. /**
  137. * Calculates product prices.
  138. *
  139. * @since 5.5.0
  140. *
  141. * @param WC_Product $product
  142. * @param array $args
  143. * @return mixed
  144. */
  145. public static function get_product_price( $product, $args ) {
  146.  
  147. $defaults = array(
  148. 'price' => '',
  149. 'qty' => 1,
  150. 'calc' => ''
  151. );
  152.  
  153. $args = wp_parse_args( $args, $defaults );
  154. $price = $args[ 'price' ];
  155. $qty = $args[ 'qty' ];
  156. $calc = $args[ 'calc' ];
  157.  
  158. if ( $price ) {
  159.  
  160. if ( 'display' === $calc ) {
  161. $calc = 'excl' === get_option( 'woocommerce_tax_display_shop' ) ? 'excl_tax' : 'incl_tax';
  162. }
  163.  
  164. if ( 'incl_tax' === $calc ) {
  165. $price = wc_get_price_including_tax( $product, array( 'qty' => $qty, 'price' => $price ) );
  166. } elseif ( 'excl_tax' === $calc ) {
  167. $price = wc_get_price_excluding_tax( $product, array( 'qty' => $qty, 'price' => $price ) );
  168. } else {
  169. $price = $price * $qty;
  170. }
  171. }
  172.  
  173. return $price;
  174. }
  175.  
  176. /**
  177. * Discounted bundled item price precision. Defaults to the price display precision, a.k.a. wc_get_price_decimals.
  178. *
  179. * @since 5.7.8
  180. *
  181. * @return int
  182. */
  183. public static function get_discounted_price_precision() {
  184. return apply_filters( 'woocommerce_bundled_item_discounted_price_precision', wc_get_price_decimals() );
  185. }
  186.  
  187. /**
  188. * Discounted price getter.
  189. *
  190. * @param mixed $price
  191. * @param mixed $discount
  192. * @return mixed
  193. */
  194. public static function get_discounted_price( $price, $discount ) {
  195.  
  196. $discounted_price = $price;
  197.  
  198. if ( ! empty( $price ) && ! empty( $discount ) ) {
  199. $discounted_price = round( ( double ) $price * ( 100 - $discount ) / 100, self::get_discounted_price_precision() );
  200. }
  201.  
  202. /**
  203. * Discounted price getter FIX.
  204. *
  205. * @param mixed $price
  206. * @param mixed $discount
  207. * @return mixed
  208. */
  209. if (class_exists('WOOCS')) {
  210. global $WOOCS;
  211. if ($WOOCS->current_currency != $WOOCS->default_currency) {
  212. $currencies = $WOOCS->get_currencies();
  213. if ($WOOCS->is_multiple_allowed) {
  214. $discounted_price = $discounted_price / $currencies[$WOOCS->current_currency]['rate'];
  215. }
  216. }
  217. }
  218. /**
  219. * Discounted price getter FIX END.
  220. *
  221. * @param mixed $price
  222. * @param mixed $discount
  223. * @return mixed
  224. */
  225. return $discounted_price;
  226. }
  227.  
  228. /**
  229. * Returns the recurring price component of a subscription product.
  230. *
  231. * @param WC_Product $product
  232. * @return string
  233. */
  234. public static function get_recurring_price_html_component( $product ) {
  235.  
  236. $sync_date = $product->get_meta( '_subscription_payment_sync_date', true );
  237.  
  238. $product->update_meta_data( '_subscription_payment_sync_date', 0 );
  239.  
  240. $sub_price_html = WC_Subscriptions_Product::get_price_string( $product, array( 'price' => '%s', 'sign_up_fee' => false ) );
  241.  
  242. $product->update_meta_data( '_subscription_payment_sync_date', $sync_date );
  243.  
  244. return $sub_price_html;
  245. }
  246.  
  247. /**
  248. * Add price filters to modify child product prices depending on the bundled item pricing setup.
  249. *
  250. * @param WC_Bundled_Item $bundled_item
  251. */
  252. public static function add_price_filters( $bundled_item ) {
  253.  
  254. $add_filters = false;
  255.  
  256. if ( empty( self::$bundled_item_pre ) ) {
  257. self::$bundled_item_pre = array();
  258. $add_filters = true;
  259. }
  260.  
  261. self::$bundled_item_pre[] = $bundled_item;
  262. self::$bundled_item = $bundled_item;
  263.  
  264. if ( $add_filters ) {
  265.  
  266. add_filter( 'woocommerce_product_get_price', array( __CLASS__, 'filter_get_price' ), 15, 2 );
  267. add_filter( 'woocommerce_product_get_sale_price', array( __CLASS__, 'filter_get_sale_price' ), 15, 2 );
  268. add_filter( 'woocommerce_product_get_regular_price', array( __CLASS__, 'filter_get_regular_price' ), 15, 2 );
  269. add_filter( 'woocommerce_product_variation_get_price', array( __CLASS__, 'filter_get_price' ), 15, 2 );
  270. add_filter( 'woocommerce_product_variation_get_sale_price', array( __CLASS__, 'filter_get_sale_price' ), 15, 2 );
  271. add_filter( 'woocommerce_product_variation_get_regular_price', array( __CLASS__, 'filter_get_regular_price' ), 15, 2 );
  272.  
  273. add_filter( 'woocommerce_get_price_html', array( __CLASS__, 'filter_get_price_html' ), 10, 2 );
  274. add_filter( 'woocommerce_get_children', array( __CLASS__, 'filter_children' ), 10, 2 );
  275. add_filter( 'woocommerce_variation_prices', array( __CLASS__, 'filter_get_variation_prices' ), 15, 2 );
  276. add_filter( 'woocommerce_show_variation_price', array( __CLASS__, 'filter_show_variation_price' ), 10, 3 );
  277. add_filter( 'woocommerce_get_variation_prices_hash', array( __CLASS__, 'filter_variation_prices_hash' ), 10, 2 );
  278.  
  279. /**
  280. * 'woocommerce_bundled_product_price_filters_added' hook.
  281. *
  282. * @param WC_Bundled_Item $bundled_item
  283. */
  284. do_action( 'woocommerce_bundled_product_price_filters_added', $bundled_item );
  285. }
  286. }
  287.  
  288. /**
  289. * Remove price filters after modifying child product prices depending on the bundled item pricing setup.
  290. */
  291. public static function remove_price_filters() {
  292.  
  293. $bundled_item = self::$bundled_item;
  294.  
  295. array_pop( self::$bundled_item_pre );
  296.  
  297. self::$bundled_item = ! empty( self::$bundled_item_pre ) && is_array( self::$bundled_item_pre ) ? end( self::$bundled_item_pre ) : null;
  298.  
  299. if ( $bundled_item && empty( self::$bundled_item ) ) {
  300.  
  301. remove_filter( 'woocommerce_product_get_price', array( __CLASS__, 'filter_get_price' ), 15, 2 );
  302. remove_filter( 'woocommerce_product_get_sale_price', array( __CLASS__, 'filter_get_sale_price' ), 15, 2 );
  303. remove_filter( 'woocommerce_product_get_regular_price', array( __CLASS__, 'filter_get_regular_price' ), 15, 2 );
  304. remove_filter( 'woocommerce_product_variation_get_price', array( __CLASS__, 'filter_get_price' ), 15, 2 );
  305. remove_filter( 'woocommerce_product_variation_get_sale_price', array( __CLASS__, 'filter_get_sale_price' ), 15, 2 );
  306. remove_filter( 'woocommerce_product_variation_get_regular_price', array( __CLASS__, 'filter_get_regular_price' ), 15, 2 );
  307.  
  308. remove_filter( 'woocommerce_get_price_html', array( __CLASS__, 'filter_get_price_html' ), 10, 2 );
  309. remove_filter( 'woocommerce_get_children', array( __CLASS__, 'filter_children' ), 10, 2 );
  310. remove_filter( 'woocommerce_variation_prices', array( __CLASS__, 'filter_get_variation_prices' ), 15, 2 );
  311. remove_filter( 'woocommerce_show_variation_price', array( __CLASS__, 'filter_show_variation_price' ), 10, 3 );
  312. remove_filter( 'woocommerce_get_variation_prices_hash', array( __CLASS__, 'filter_variation_prices_hash' ), 10, 2 );
  313.  
  314. /**
  315. * 'woocommerce_bundled_product_price_filters_removed' hook.
  316. *
  317. * @param WC_Bundled_Item $bundled_item
  318. */
  319. do_action( 'woocommerce_bundled_product_price_filters_removed', $bundled_item );
  320. }
  321. }
  322.  
  323. /*
  324. |--------------------------------------------------------------------------
  325. | Callbacks.
  326. |--------------------------------------------------------------------------
  327. */
  328.  
  329. /**
  330. * Filter variation prices hash to load different prices for variable products with variation filters and/or discounts.
  331. *
  332. * @param array $hash
  333. * @param WC_Product_Variable $product
  334. * @return array
  335. */
  336. public static function filter_variation_prices_hash( $hash, $product ) {
  337.  
  338. $bundled_item = self::$bundled_item;
  339.  
  340. if ( $bundled_item ) {
  341.  
  342. $discount = $bundled_item->get_discount();
  343. $has_filtered_variations = $product->is_type( 'variable' ) && $bundled_item->has_filtered_variations();
  344.  
  345. if ( $has_filtered_variations || ! empty( $discount ) ) {
  346. $hash[] = $bundled_item->data->get_id();
  347. }
  348. }
  349.  
  350. return $hash;
  351. }
  352.  
  353. /**
  354. * Filter variable product children to exclude filtered out variations.
  355. *
  356. * @param array $children
  357. * @param WC_Product_Variable $product
  358. * @return array
  359. */
  360. public static function filter_children( $children, $product ) {
  361.  
  362. $bundled_item = self::$bundled_item;
  363.  
  364. if ( $bundled_item ) {
  365.  
  366. if ( $bundled_item->has_filtered_variations() ) {
  367.  
  368. $filtered_children = array();
  369.  
  370. foreach ( $children as $variation_id ) {
  371. // Remove if filtered.
  372. if ( in_array( $variation_id, $bundled_item->get_filtered_variations() ) ) {
  373. $filtered_children[] = $variation_id;
  374. }
  375. }
  376.  
  377. $children = $filtered_children;
  378. }
  379. }
  380.  
  381. return $children;
  382. }
  383.  
  384. /**
  385. * Filter get_variation_prices() calls for bundled products to include discounts.
  386. *
  387. * @param array $prices_array
  388. * @param WC_Product_Variable $product
  389. * @return array
  390. */
  391. public static function filter_get_variation_prices( $prices_array, $product ) {
  392.  
  393. $bundled_item = self::$bundled_item;
  394.  
  395. if ( $bundled_item ) {
  396.  
  397. $prices = array();
  398. $regular_prices = array();
  399. $sale_prices = array();
  400.  
  401. $discount = $bundled_item->get_discount();
  402. $priced_per_product = $bundled_item->is_priced_individually();
  403.  
  404. // Filter regular prices.
  405. foreach ( $prices_array[ 'regular_price' ] as $variation_id => $regular_price ) {
  406. if ( $priced_per_product ) {
  407. $regular_prices[ $variation_id ] = $regular_price === '' ? $prices_array[ 'price' ][ $variation_id ] : $regular_price;
  408. } else {
  409. $regular_prices[ $variation_id ] = 0;
  410. }
  411. }
  412.  
  413. // Filter prices.
  414. foreach ( $prices_array[ 'price' ] as $variation_id => $price ) {
  415. if ( $priced_per_product ) {
  416. if ( false === $bundled_item->is_discount_allowed_on_sale_price() ) {
  417. $regular_price = $regular_prices[ $variation_id ];
  418. } else {
  419. $regular_price = $price;
  420. }
  421. $price = empty( $discount ) ? $price : round( ( double ) $regular_price * ( 100 - $discount ) / 100, self::get_discounted_price_precision() );
  422. $prices[ $variation_id ] = apply_filters( 'woocommerce_bundled_variation_price', $price, $variation_id, $discount, $bundled_item );
  423. } else {
  424. $prices[ $variation_id ] = 0;
  425. }
  426. }
  427.  
  428. // Filter sale prices.
  429. foreach ( $prices_array[ 'sale_price' ] as $variation_id => $sale_price ) {
  430. if ( $priced_per_product ) {
  431. $sale_prices[ $variation_id ] = empty( $discount ) ? $sale_price : $prices[ $variation_id ];
  432. } else {
  433. $sale_prices[ $variation_id ] = 0;
  434. }
  435. }
  436.  
  437. if ( $priced_per_product && ! empty( $discount ) && false === $bundled_item->is_discount_allowed_on_sale_price() ) {
  438. asort( $regular_prices );
  439. asort( $prices );
  440. asort( $sale_prices );
  441. }
  442.  
  443. $prices_array = array(
  444. 'price' => $prices,
  445. 'regular_price' => $regular_prices,
  446. 'sale_price' => $sale_prices
  447. );
  448. }
  449.  
  450. return $prices_array;
  451. }
  452.  
  453. /**
  454. * Filter condition that allows WC to calculate variation price_html.
  455. *
  456. * @param boolean $show
  457. * @param WC_Product_Variable $product
  458. * @param WC_Product_Variation $variation
  459. * @return boolean
  460. */
  461. public static function filter_show_variation_price( $show, $product, $variation ) {
  462.  
  463. $bundled_item = self::$bundled_item;
  464.  
  465. if ( $bundled_item ) {
  466.  
  467. $prices_equal = ! $show;
  468. $show = false;
  469.  
  470. if ( $bundled_item->is_priced_individually() && $bundled_item->is_price_visible( 'product' ) ) {
  471. $show = true;
  472. // If the product is optional and all prices are equal, then the prices is already displayed in "Add for $XXX".
  473. if ( $bundled_item->is_optional() && $prices_equal ) {
  474. $show = false;
  475. }
  476. }
  477. }
  478.  
  479. return $show;
  480. }
  481.  
  482. /**
  483. * Filter get_price() calls for bundled products to include discounts.
  484. *
  485. * @param double $price
  486. * @param WC_Product $product
  487. * @param string $context
  488. * @return double
  489. */
  490. public static function filter_get_price( $price, $product, $context = '' ) {
  491.  
  492. $bundled_item = false;
  493.  
  494. if ( self::$bundled_item ) {
  495. $bundled_item = self::$bundled_item;
  496. } elseif ( isset( $product->bundled_cart_item ) ) {
  497. $bundled_item = $product->bundled_cart_item;
  498. }
  499.  
  500. if ( $bundled_item && ( $bundled_item instanceof WC_Bundled_Item ) ) {
  501.  
  502. if ( $price === '' ) {
  503. return $price;
  504. }
  505.  
  506. if ( ! $bundled_item->is_priced_individually() ) {
  507. return 0;
  508. }
  509.  
  510. if ( $discount = $bundled_item->get_discount( $context ) ) {
  511.  
  512. $offset_price = ! empty( $product->bundled_price_offset ) ? $product->bundled_price_offset : false;
  513. $offset_price_pct = ! empty( $product->bundled_price_offset_pct ) && is_array( $product->bundled_price_offset_pct ) ? $product->bundled_price_offset_pct : false;
  514.  
  515. if ( false === $bundled_item->is_discount_allowed_on_sale_price() ) {
  516. $regular_price = $product->get_regular_price();
  517. } else {
  518. $regular_price = $price;
  519. }
  520.  
  521. $price = self::get_discounted_price( $regular_price, $discount );
  522.  
  523. // Add-on % prices.
  524. if ( $offset_price_pct ) {
  525.  
  526. if ( ! $offset_price ) {
  527. $offset_price = 0.0;
  528. }
  529.  
  530. foreach ( $offset_price_pct as $price_pct ) {
  531. $offset_price += $price * $price_pct / 100;
  532. }
  533. }
  534.  
  535. // Add-on prices.
  536. if ( $offset_price ) {
  537. $price += $offset_price;
  538. }
  539. }
  540.  
  541. $product->bundled_item_price = $price;
  542.  
  543. /** Documented in 'WC_Bundled_Item::get_raw_price()'. */
  544. $price = apply_filters( 'woocommerce_bundled_item_price', $price, $product, $discount, $bundled_item );
  545. }
  546.  
  547. return $price;
  548. }
  549.  
  550. /**
  551. * Filter get_regular_price() calls for bundled products to include discounts.
  552. *
  553. * @param double $price
  554. * @param WC_Product $product
  555. * @return double
  556. */
  557. public static function filter_get_regular_price( $regular_price, $product ) {
  558.  
  559. $bundled_item = false;
  560.  
  561. if ( self::$bundled_item ) {
  562. $bundled_item = self::$bundled_item;
  563. } elseif ( isset( $product->bundled_cart_item ) ) {
  564. $bundled_item = $product->bundled_cart_item;
  565. }
  566.  
  567. if ( $bundled_item && ( $bundled_item instanceof WC_Bundled_Item ) ) {
  568.  
  569. if ( ! $bundled_item->is_priced_individually() ) {
  570. return 0;
  571. }
  572. }
  573.  
  574. return $regular_price;
  575. }
  576.  
  577. /**
  578. * Filter get_sale_price() calls for bundled products to include discounts.
  579. *
  580. * @param double $price
  581. * @param WC_Product $product
  582. * @param string $context
  583. * @return double
  584. */
  585. public static function filter_get_sale_price( $sale_price, $product, $context = '' ) {
  586.  
  587. $bundled_item = false;
  588.  
  589. if ( self::$bundled_item ) {
  590. $bundled_item = self::$bundled_item;
  591. } elseif ( isset( $product->bundled_cart_item ) ) {
  592. $bundled_item = $product->bundled_cart_item;
  593. }
  594.  
  595. if ( $bundled_item && ( $bundled_item instanceof WC_Bundled_Item ) ) {
  596.  
  597. if ( ! $bundled_item->is_priced_individually() ) {
  598. return 0;
  599. }
  600.  
  601. if ( $discount = $bundled_item->get_discount( $context ) ) {
  602.  
  603. $offset_price = ! empty( $product->bundled_price_offset ) ? $product->bundled_price_offset : false;
  604. $offset_price_pct = ! empty( $product->bundled_price_offset_pct ) && is_array( $product->bundled_price_offset_pct ) ? $product->bundled_price_offset_pct : false;
  605.  
  606. if ( '' === $sale_price || false === $bundled_item->is_discount_allowed_on_sale_price() ) {
  607. $regular_price = $product->get_regular_price();
  608. } else {
  609. $regular_price = $sale_price;
  610. }
  611.  
  612. $sale_price = self::get_discounted_price( $regular_price, $discount );
  613.  
  614. // Add-on % prices.
  615. if ( $offset_price_pct ) {
  616.  
  617. if ( ! $offset_price ) {
  618. $offset_price = 0.0;
  619. }
  620.  
  621. foreach ( $offset_price_pct as $price_pct ) {
  622. $offset_price += $sale_price * $price_pct / 100;
  623. }
  624. }
  625.  
  626. // Add-on prices.
  627. if ( $offset_price ) {
  628. $sale_price += $offset_price;
  629. }
  630. }
  631.  
  632. /** Documented in 'WC_Bundled_Item::get_raw_price()'. */
  633. $sale_price = apply_filters( 'woocommerce_bundled_item_price', $sale_price, $product, $discount, $bundled_item );
  634. }
  635.  
  636. return $sale_price;
  637. }
  638.  
  639. /**
  640. * Filter get_price() calls for bundled cart items to include discounts.
  641. *
  642. * @since 6.0.0
  643. *
  644. * @param double $price
  645. * @param WC_Product $product
  646. * @return double
  647. */
  648. public static function filter_get_price_cart( $price, $product ) {
  649. return self::is_bundled_pricing_context( $product, 'cart' ) ? self::filter_get_price( $price, $product, 'cart' ) : $price;
  650. }
  651.  
  652. /**
  653. * Filter get_sale_price() calls for bundled cart items to include discounts.
  654. *
  655. * @since 6.0.0
  656. *
  657. * @param double $price
  658. * @param WC_Product $product
  659. * @return double
  660. */
  661. public static function filter_get_sale_price_cart( $price, $product ) {
  662. return self::is_bundled_pricing_context( $product, 'cart' ) ? self::filter_get_sale_price( $price, $product, 'cart' ) : $price;
  663. }
  664.  
  665. /**
  666. * Filter get_regular_price() calls for bundled cart items.
  667. *
  668. * @since 6.1.4
  669. *
  670. * @param double $price
  671. * @param WC_Product $product
  672. * @return double
  673. */
  674. public static function filter_get_regular_price_cart( $price, $product ) {
  675. return self::is_bundled_pricing_context( $product, 'cart' ) ? self::filter_get_regular_price( $price, $product, 'cart' ) : $price;
  676. }
  677.  
  678. /**
  679. * Filter the html price string of bundled items to show the correct price with discount and tax - needs to be hidden when the bundled item is priced individually.
  680. *
  681. * @param string $price_html
  682. * @param WC_Product $product
  683. * @return string
  684. */
  685. public static function filter_get_price_html( $price_html, $product ) {
  686.  
  687. $bundled_item = self::$bundled_item;
  688.  
  689. if ( $bundled_item ) {
  690.  
  691. if ( ! $bundled_item->is_priced_individually() ) {
  692. return '';
  693. }
  694.  
  695. if ( ! $bundled_item->is_price_visible( 'product' ) ) {
  696. return '';
  697. }
  698.  
  699. $quantity = $bundled_item->get_quantity( 'max', array( 'bound_by_stock' => true ) );
  700.  
  701. /**
  702. * 'woocommerce_bundled_item_price_html' filter.
  703. *
  704. * @param string $price_html
  705. * @param WC_Bundled_Item $bundled_item
  706. */
  707. $price_html = apply_filters( 'woocommerce_bundled_item_price_html', '' === $quantity || $quantity > 1 ? sprintf( __( '%1$s <span class="bundled_item_price_quantity">each</span>', 'woocommerce-product-bundles' ), $price_html, $quantity ) : $price_html, $price_html, $bundled_item );
  708. }
  709.  
  710. return $price_html;
  711. }
  712.  
  713. /*
  714. |--------------------------------------------------------------------------
  715. | Deprecated methods.
  716. |--------------------------------------------------------------------------
  717. */
  718.  
  719. /**
  720. * Calculates bundled product prices incl. or excl. tax depending on the 'woocommerce_tax_display_shop' setting.
  721. *
  722. * @deprecated 5.5.0
  723. */
  724. public static function get_product_display_price( $product, $price, $qty = 1 ) {
  725. _deprecated_function( __METHOD__ . '()', '5.5.0', 'WC_PB_Product_Prices::get_product_price()' );
  726. return self::get_product_price( $product, array(
  727. 'price' => $price,
  728. 'qty' => $qty,
  729. 'calc' => 'display'
  730. ) );
  731. }
  732. }
  733.  
  734. WC_PB_Product_Prices::init();
  735.  
Advertisement
Add Comment
Please, Sign In to add comment