PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/packages/woocommerce-blocks/assets/js/base/components/cart-checkout/order-summary/order-summary-item.tsx

https://gitlab.com/remyvianne/krowkaramel
TypeScript | 215 lines | 189 code | 14 blank | 12 comment | 13 complexity | 2c4a21b26dbb5691b00f9f30cf690b82 MD5 | raw file
  1. /**
  2. * External dependencies
  3. */
  4. import classnames from 'classnames';
  5. import { sprintf, _n } from '@wordpress/i18n';
  6. import Label from '@woocommerce/base-components/label';
  7. import ProductPrice from '@woocommerce/base-components/product-price';
  8. import ProductName from '@woocommerce/base-components/product-name';
  9. import {
  10. getCurrencyFromPriceResponse,
  11. formatPrice,
  12. } from '@woocommerce/price-format';
  13. import {
  14. __experimentalApplyCheckoutFilter,
  15. mustContain,
  16. } from '@woocommerce/blocks-checkout';
  17. import Dinero from 'dinero.js';
  18. import { getSetting } from '@woocommerce/settings';
  19. import { useMemo } from '@wordpress/element';
  20. import { useStoreCart } from '@woocommerce/base-context/hooks';
  21. import { CartItem, isString } from '@woocommerce/types';
  22. /**
  23. * Internal dependencies
  24. */
  25. import ProductBackorderBadge from '../product-backorder-badge';
  26. import ProductImage from '../product-image';
  27. import ProductLowStockBadge from '../product-low-stock-badge';
  28. import ProductMetadata from '../product-metadata';
  29. const productPriceValidation = ( value: string ): true | never =>
  30. mustContain( value, '<price/>' );
  31. interface OrderSummaryProps {
  32. cartItem: CartItem;
  33. }
  34. const OrderSummaryItem = ( { cartItem }: OrderSummaryProps ): JSX.Element => {
  35. const {
  36. images,
  37. low_stock_remaining: lowStockRemaining,
  38. show_backorder_badge: showBackorderBadge,
  39. name: initialName,
  40. permalink,
  41. prices,
  42. quantity,
  43. short_description: shortDescription,
  44. description: fullDescription,
  45. item_data: itemData,
  46. variation,
  47. totals,
  48. extensions,
  49. } = cartItem;
  50. // Prepare props to pass to the __experimentalApplyCheckoutFilter filter.
  51. // We need to pluck out receiveCart.
  52. // eslint-disable-next-line no-unused-vars
  53. const { receiveCart, ...cart } = useStoreCart();
  54. const arg = useMemo(
  55. () => ( {
  56. context: 'summary',
  57. cartItem,
  58. cart,
  59. } ),
  60. [ cartItem, cart ]
  61. );
  62. const priceCurrency = getCurrencyFromPriceResponse( prices );
  63. const name = __experimentalApplyCheckoutFilter( {
  64. filterName: 'itemName',
  65. defaultValue: initialName,
  66. extensions,
  67. arg,
  68. } );
  69. const regularPriceSingle = Dinero( {
  70. amount: parseInt( prices.raw_prices.regular_price, 10 ),
  71. precision: isString( prices.raw_prices.precision )
  72. ? parseInt( prices.raw_prices.precision, 10 )
  73. : prices.raw_prices.precision,
  74. } )
  75. .convertPrecision( priceCurrency.minorUnit )
  76. .getAmount();
  77. const priceSingle = Dinero( {
  78. amount: parseInt( prices.raw_prices.price, 10 ),
  79. precision: isString( prices.raw_prices.precision )
  80. ? parseInt( prices.raw_prices.precision, 10 )
  81. : prices.raw_prices.precision,
  82. } )
  83. .convertPrecision( priceCurrency.minorUnit )
  84. .getAmount();
  85. const totalsCurrency = getCurrencyFromPriceResponse( totals );
  86. let lineSubtotal = parseInt( totals.line_subtotal, 10 );
  87. if ( getSetting( 'displayCartPricesIncludingTax', false ) ) {
  88. lineSubtotal += parseInt( totals.line_subtotal_tax, 10 );
  89. }
  90. const subtotalPrice = Dinero( {
  91. amount: lineSubtotal,
  92. precision: totalsCurrency.minorUnit,
  93. } ).getAmount();
  94. const subtotalPriceFormat = __experimentalApplyCheckoutFilter( {
  95. filterName: 'subtotalPriceFormat',
  96. defaultValue: '<price/>',
  97. extensions,
  98. arg,
  99. validation: productPriceValidation,
  100. } );
  101. // Allow extensions to filter how the price is displayed. Ie: prepending or appending some values.
  102. const productPriceFormat = __experimentalApplyCheckoutFilter( {
  103. filterName: 'cartItemPrice',
  104. defaultValue: '<price/>',
  105. extensions,
  106. arg,
  107. validation: productPriceValidation,
  108. } );
  109. const cartItemClassNameFilter = __experimentalApplyCheckoutFilter( {
  110. filterName: 'cartItemClass',
  111. defaultValue: '',
  112. extensions,
  113. arg,
  114. } );
  115. return (
  116. <div
  117. className={ classnames(
  118. 'wc-block-components-order-summary-item',
  119. cartItemClassNameFilter
  120. ) }
  121. >
  122. <div className="wc-block-components-order-summary-item__image">
  123. <div className="wc-block-components-order-summary-item__quantity">
  124. <Label
  125. label={ quantity.toString() }
  126. screenReaderLabel={ sprintf(
  127. /* translators: %d number of products of the same type in the cart */
  128. _n(
  129. '%d item',
  130. '%d items',
  131. quantity,
  132. 'woo-gutenberg-products-block'
  133. ),
  134. quantity
  135. ) }
  136. />
  137. </div>
  138. <ProductImage
  139. image={ images.length ? images[ 0 ] : {} }
  140. fallbackAlt={ name }
  141. />
  142. </div>
  143. <div className="wc-block-components-order-summary-item__description">
  144. <ProductName
  145. disabled={ true }
  146. name={ name }
  147. permalink={ permalink }
  148. />
  149. <ProductPrice
  150. currency={ priceCurrency }
  151. price={ priceSingle }
  152. regularPrice={ regularPriceSingle }
  153. className="wc-block-components-order-summary-item__individual-prices"
  154. priceClassName="wc-block-components-order-summary-item__individual-price"
  155. regularPriceClassName="wc-block-components-order-summary-item__regular-individual-price"
  156. format={ subtotalPriceFormat }
  157. />
  158. { showBackorderBadge ? (
  159. <ProductBackorderBadge />
  160. ) : (
  161. !! lowStockRemaining && (
  162. <ProductLowStockBadge
  163. lowStockRemaining={ lowStockRemaining }
  164. />
  165. )
  166. ) }
  167. <ProductMetadata
  168. shortDescription={ shortDescription }
  169. fullDescription={ fullDescription }
  170. itemData={ itemData }
  171. variation={ variation }
  172. />
  173. </div>
  174. <span className="screen-reader-text">
  175. { sprintf(
  176. /* translators: %1$d is the number of items, %2$s is the item name and %3$s is the total price including the currency symbol. */
  177. _n(
  178. 'Total price for %1$d %2$s item: %3$s',
  179. 'Total price for %1$d %2$s items: %3$s',
  180. quantity,
  181. 'woo-gutenberg-products-block'
  182. ),
  183. quantity,
  184. name,
  185. formatPrice( subtotalPrice, totalsCurrency )
  186. ) }
  187. </span>
  188. <div
  189. className="wc-block-components-order-summary-item__total-price"
  190. aria-hidden="true"
  191. >
  192. <ProductPrice
  193. currency={ totalsCurrency }
  194. format={ productPriceFormat }
  195. price={ subtotalPrice }
  196. />
  197. </div>
  198. </div>
  199. );
  200. };
  201. export default OrderSummaryItem;