Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Revert "Remove unused Add to Cart product element (#11948)" #12033

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions assets/js/atomic/blocks/component-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,12 @@ registerBlockComponent( {
)
),
} );

registerBlockComponent( {
blockName: 'woocommerce/product-add-to-cart',
component: lazy( () =>
import(
/* webpackChunkName: "product-add-to-cart" */ './product-elements/add-to-cart/frontend'
)
),
} );
1 change: 1 addition & 0 deletions assets/js/atomic/blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import './product-elements/summary';
import './product-elements/sale-badge';
import './product-elements/sku';
import './product-elements/stock-indicator';
import './product-elements/add-to-cart';
import './product-elements/add-to-cart-form';
import './product-elements/product-image-gallery';
import './product-elements/product-details';
Expand Down
12 changes: 12 additions & 0 deletions assets/js/atomic/blocks/product-elements/add-to-cart/attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const blockAttributes = {
showFormElements: {
type: 'boolean',
default: false,
},
productId: {
type: 'number',
default: 0,
},
};

export default blockAttributes;
87 changes: 87 additions & 0 deletions assets/js/atomic/blocks/product-elements/add-to-cart/block.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* External dependencies
*/
import classnames from 'classnames';
import {
AddToCartFormContextProvider,
useAddToCartFormContext,
} from '@woocommerce/base-context';
import { useProductDataContext } from '@woocommerce/shared-context';
import { isEmpty } from '@woocommerce/types';
import { withProductDataContext } from '@woocommerce/shared-hocs';

/**
* Internal dependencies
*/
import './style.scss';
import { AddToCartButton } from './shared';
import {
SimpleProductForm,
VariableProductForm,
ExternalProductForm,
GroupedProductForm,
} from './product-types';

interface Props {
/**
* CSS Class name for the component.
*/
className?: string;
/**
* Whether or not to show form elements.
*/
showFormElements?: boolean;
}

/**
* Renders the add to cart form using useAddToCartFormContext.
*/
const AddToCartForm = () => {
const { showFormElements, productType } = useAddToCartFormContext();

if ( showFormElements ) {
if ( productType === 'variable' ) {
return <VariableProductForm />;
}
if ( productType === 'grouped' ) {
return <GroupedProductForm />;
}
if ( productType === 'external' ) {
return <ExternalProductForm />;
}
if ( productType === 'simple' || productType === 'variation' ) {
return <SimpleProductForm />;
}
return null;
}

return <AddToCartButton />;
};

/**
* Product Add to Form Block Component.
*/
const Block = ( { className, showFormElements }: Props ) => {
const { product } = useProductDataContext();
const componentClass = classnames(
className,
'wc-block-components-product-add-to-cart',
{
'wc-block-components-product-add-to-cart--placeholder':
isEmpty( product ),
}
);

return (
<AddToCartFormContextProvider
product={ product }
showFormElements={ showFormElements }
>
<div className={ componentClass }>
<AddToCartForm />
</div>
</AddToCartFormContextProvider>
);
};

export default withProductDataContext( Block );
15 changes: 15 additions & 0 deletions assets/js/atomic/blocks/product-elements/add-to-cart/constants.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { cart } from '@woocommerce/icons';
import { Icon } from '@wordpress/icons';

export const BLOCK_TITLE = __( 'Add to Cart', 'woo-gutenberg-products-block' );
export const BLOCK_ICON = (
<Icon icon={ cart } className="wc-block-editor-components-block-icon" />
);
export const BLOCK_DESCRIPTION = __(
'Displays an add to cart button. Optionally displays other add to cart form elements.',
'woo-gutenberg-products-block'
);
94 changes: 94 additions & 0 deletions assets/js/atomic/blocks/product-elements/add-to-cart/edit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import EditProductLink from '@woocommerce/editor-components/edit-product-link';
import { useProductDataContext } from '@woocommerce/shared-context';
import classnames from 'classnames';
import {
Disabled,
PanelBody,
ToggleControl,
Notice,
} from '@wordpress/components';
import { InspectorControls } from '@wordpress/block-editor';
import { productSupportsAddToCartForm } from '@woocommerce/base-utils';

/**
* Internal dependencies
*/
import './style.scss';
import Block from './block';
import withProductSelector from '../shared/with-product-selector';
import { BLOCK_TITLE, BLOCK_ICON } from './constants';

interface EditProps {
attributes: {
className: string;
showFormElements: boolean;
};
setAttributes: ( attributes: { showFormElements: boolean } ) => void;
}

const Edit = ( { attributes, setAttributes }: EditProps ) => {
const { product } = useProductDataContext();
const { className, showFormElements } = attributes;

return (
<div
className={ classnames(
className,
'wc-block-components-product-add-to-cart'
) }
>
<EditProductLink productId={ product.id } />
<InspectorControls>
<PanelBody
title={ __( 'Layout', 'woo-gutenberg-products-block' ) }
>
{ productSupportsAddToCartForm( product ) ? (
<ToggleControl
label={ __(
'Display form elements',
'woo-gutenberg-products-block'
) }
help={ __(
'Depending on product type, allow customers to select a quantity, variations etc.',
'woo-gutenberg-products-block'
) }
checked={ showFormElements }
onChange={ () =>
setAttributes( {
showFormElements: ! showFormElements,
} )
}
/>
) : (
<Notice
className="wc-block-components-product-add-to-cart-notice"
isDismissible={ false }
status="info"
>
{ __(
'This product does not support the block based add to cart form. A link to the product page will be shown instead.',
'woo-gutenberg-products-block'
) }
</Notice>
) }
</PanelBody>
</InspectorControls>
<Disabled>
<Block { ...attributes } />
</Disabled>
</div>
);
};

export default withProductSelector( {
icon: BLOCK_ICON,
label: BLOCK_TITLE,
description: __(
'Choose a product to display its add to cart form.',
'woo-gutenberg-products-block'
),
} )( Edit );
12 changes: 12 additions & 0 deletions assets/js/atomic/blocks/product-elements/add-to-cart/frontend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* External dependencies
*/
import { withFilteredAttributes } from '@woocommerce/shared-hocs';

/**
* Internal dependencies
*/
import Block from './block';
import attributes from './attributes';

export default withFilteredAttributes( attributes )( Block );
29 changes: 29 additions & 0 deletions assets/js/atomic/blocks/product-elements/add-to-cart/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* External dependencies
*/
import { registerExperimentalBlockType } from '@woocommerce/block-settings';

/**
* Internal dependencies
*/
import sharedConfig from '../shared/config';
import edit from './edit';
import attributes from './attributes';
import {
BLOCK_TITLE as title,
BLOCK_ICON as icon,
BLOCK_DESCRIPTION as description,
} from './constants';

const blockConfig = {
title,
description,
icon: { src: icon },
edit,
attributes,
};

registerExperimentalBlockType( 'woocommerce/product-add-to-cart', {
...sharedConfig,
...blockConfig,
} );
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Internal dependencies
*/
import AddToCartButton from '../shared/add-to-cart-button';

/**
* External Product Add To Cart Form
*/
const External = () => {
return <AddToCartButton />;
};

export default External;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Grouped Product Add To Cart Form
*/
const Grouped = () => (
<p>This is a placeholder for the grouped products form element.</p>
);

export default Grouped;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { default as SimpleProductForm } from './simple';
export { default as VariableProductForm } from './variable/index';
export { default as ExternalProductForm } from './external';
export { default as GroupedProductForm } from './grouped';
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { useAddToCartFormContext } from '@woocommerce/base-context';

/**
* Internal dependencies
*/
import { AddToCartButton, QuantityInput, ProductUnavailable } from '../shared';

/**
* Simple Product Add To Cart Form
*/
const Simple = () => {
// @todo Add types for `useAddToCartFormContext`
const {
product,
quantity,
minQuantity,
maxQuantity,
multipleOf,
dispatchActions,
isDisabled,
} = useAddToCartFormContext();

if ( product.id && ! product.is_purchasable ) {
return <ProductUnavailable />;
}

if ( product.id && ! product.is_in_stock ) {
return (
<ProductUnavailable
reason={ __(
'This product is currently out of stock and cannot be purchased.',
'woo-gutenberg-products-block'
) }
/>
);
}

return (
<>
<QuantityInput
value={ quantity }
min={ minQuantity }
max={ maxQuantity }
step={ multipleOf }
disabled={ isDisabled }
onChange={ dispatchActions.setQuantity }
/>
<AddToCartButton />
</>
);
};

export default Simple;
Loading
Loading