/** * External dependencies */ import classnames from 'classnames'; import { __, _n, sprintf } from '@wordpress/i18n'; import { useStoreEvents, useStoreAddToCart, } from '@woocommerce/base-context/hooks'; import { useStyleProps } from '@woocommerce/base-hooks'; import { decodeEntities } from '@wordpress/html-entities'; import { CART_URL } from '@woocommerce/block-settings'; import { getSetting } from '@woocommerce/settings'; import { useInnerBlockLayoutContext, useProductDataContext, } from '@woocommerce/shared-context'; import { withProductDataContext } from '@woocommerce/shared-hocs'; /** * Internal dependencies */ import './style.scss'; import type { BlockAttributes, AddToCartButtonAttributes, AddToCartButtonPlaceholderAttributes, } from './types'; const AddToCartButton = ( { product, className, style, }: AddToCartButtonAttributes ): JSX.Element => { const { id, permalink, add_to_cart: productCartDetails, has_options: hasOptions, is_purchasable: isPurchasable, is_in_stock: isInStock, } = product; const { dispatchStoreEvent } = useStoreEvents(); const { cartQuantity, addingToCart, addToCart } = useStoreAddToCart( id ); const addedToCart = Number.isFinite( cartQuantity ) && cartQuantity > 0; const allowAddToCart = ! hasOptions && isPurchasable && isInStock; const buttonAriaLabel = decodeEntities( productCartDetails?.description || '' ); const buttonText = addedToCart ? sprintf( /* translators: %s number of products in cart. */ _n( '%d in cart', '%d in cart', cartQuantity, 'woo-gutenberg-products-block' ), cartQuantity ) : decodeEntities( productCartDetails?.text || __( 'Add to cart', 'woo-gutenberg-products-block' ) ); const ButtonTag = allowAddToCart ? 'button' : 'a'; const buttonProps = {} as HTMLAnchorElement & { onClick: () => void }; if ( ! allowAddToCart ) { buttonProps.href = permalink; buttonProps.rel = 'nofollow'; buttonProps.onClick = () => { dispatchStoreEvent( 'product-view-link', { product, } ); }; } else { buttonProps.onClick = async () => { await addToCart(); dispatchStoreEvent( 'cart-add-item', { product, } ); // redirect to cart if the setting to redirect to the cart page // on cart add item is enabled const { cartRedirectAfterAdd }: { cartRedirectAfterAdd: boolean } = getSetting( 'productsSettings' ); if ( cartRedirectAfterAdd ) { window.location.href = CART_URL; } }; } return ( { buttonText } ); }; const AddToCartButtonPlaceholder = ( { className, style, }: AddToCartButtonPlaceholderAttributes ): JSX.Element => { return (