/** * External dependencies */ import classnames from 'classnames'; import { __ } from '@wordpress/i18n'; import PropTypes from 'prop-types'; import QuantitySelector from '@woocommerce/base-components/quantity-selector'; import { getCurrency } from '@woocommerce/base-utils'; import { useStoreCartItemQuantity } from '@woocommerce/base-hooks'; import { Icon, trash } from '@woocommerce/icons'; import { ProductBackorderBadge, ProductImage, ProductLowStockBadge, ProductMetadata, ProductName, ProductPrice, ProductSaleBadge, } from '@woocommerce/base-components/cart-checkout'; import Dinero from 'dinero.js'; /** * @typedef {import('@woocommerce/type-defs/cart').CartItem} CartItem */ /** * Convert a Dinero object with precision to store currency minor unit. * * @param {Dinero} priceObject Price object to convert. * @param {Object} currency Currency data. * @return {number} Amount with new minor unit precision. */ const getAmountFromRawPrice = ( priceObject, currency ) => { return priceObject.convertPrecision( currency.minorUnit ).getAmount(); }; /** * Cart line item table row component. * * @param {Object} props * @param {CartItem|Object} props.lineItem */ const CartLineItemRow = ( { lineItem = {} } ) => { const { name = '', short_description: shortDescription = '', description: fullDescription = '', low_stock_remaining: lowStockRemaining = null, show_backorder_badge: showBackorderBadge = false, quantity_limit: quantityLimit = 99, permalink = '', images = [], variation = [], prices = { currency_code: 'USD', currency_minor_unit: 2, currency_symbol: '$', currency_prefix: '$', currency_suffix: '', currency_decimal_separator: '.', currency_thousand_separator: ',', price: '0', regular_price: '0', sale_price: '0', price_range: null, raw_prices: { precision: 6, price: '0', regular_price: '0', sale_price: '0', }, }, } = lineItem; const { quantity, changeQuantity, removeItem, isPendingDelete, } = useStoreCartItemQuantity( lineItem ); const currency = getCurrency( prices ); const regularAmount = Dinero( { amount: parseInt( prices.raw_prices.regular_price, 10 ), precision: parseInt( prices.raw_prices.precision, 10 ), } ).multiply( quantity ); const purchaseAmount = Dinero( { amount: parseInt( prices.raw_prices.price, 10 ), precision: parseInt( prices.raw_prices.precision, 10 ), } ).multiply( quantity ); const saleAmount = regularAmount.subtract( purchaseAmount ); const firstImage = images.length ? images[ 0 ] : {}; return ( { /* If the image has no alt text, this link is unnecessary and can be hidden. */ } { /* We don't need to make it focusable, because product name has the same link. */ } { showBackorderBadge ? ( ) : ( !! lowStockRemaining && ( ) ) } ); }; CartLineItemRow.propTypes = { lineItem: PropTypes.object, }; export default CartLineItemRow;