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

Mini Cart as template part #5025

Merged
merged 21 commits into from Nov 19, 2021
Merged

Mini Cart as template part #5025

merged 21 commits into from Nov 19, 2021

Conversation

Aljullu
Copy link
Contributor

@Aljullu Aljullu commented Oct 28, 2021

This PR updates how the Mini Cart block works so contents shown inside the drawer belong to a template part. That allows themes to override it and users to edit it from the Template Parts editor.

In summary, the changes are:

  • Split the Mini Cart block into two:
    • Mini Cart: displays the button and the drawer with the template part inside it.
    • Mini Cart contents: the main block of the template part, it renders the contents of the drawer.
  • Create a Mini Cart template part.
  • Updates BlockTemplatesController so it can load block template parts alongside block templates.

Testing

To make it easier to test, checkout this branch from Gutenberg so you can edit and see the template parts added by WooCommerce: WordPress/gutenberg#36379.

Also, make sure to test with a block theme, ie: Quadrat or Tove.

WooCommerce template part

  1. Add the Mini Cart block to a page and verify you can open it. Once open, it should show the contents.
  2. Go to Appearance > Editor and navigate (using the left sidebar) to the Template Parts screen.
  3. Verify a Mini Cart template part is listed.

imatge

Theme template part

  1. Create a /block-template-parts/mini-cart.html file in your theme directory with these contents:
<!-- wp:paragraph -->
<p>This is the theme template part.</p>
<!-- /wp:paragraph -->
<!-- wp:woocommerce/mini-cart-contents /-->
  1. Go back to the page you created before and open the Mini Cart drawer.
  2. Verify the This is the theme template part text appears in the drawer.

User template part

  1. Go to Appearance > Editor and navigate (using the left sidebar) to the Template Parts screen.
  2. Open the mini-cart template and edit its contents (ie, replacing This is the theme template part. with This is the user edited template part.).
  3. Go back to the page with the Mini Cart block. Open the Mini Cart and verify the template you just edited shows up.

Classic theme

  1. Switch your theme to Storefront (or any other classic theme).
  2. Verify you can open the Mini Cart and the default template shows up.

@Aljullu Aljullu added focus: FSE Work related to prepare WooCommerce for FSE. block: mini-cart Issues related to the Mini-Cart block. labels Oct 28, 2021
@Aljullu Aljullu self-assigned this Oct 28, 2021
@rubikuserbot rubikuserbot requested review from a team and tjcafferkey and removed request for a team October 28, 2021 11:15
@github-actions
Copy link
Contributor

github-actions bot commented Oct 28, 2021

Size Change: +3.17 kB (0%)

Total Size: 1.12 MB

Filename Size Change
build/active-filters-frontend.js 8.24 kB +59 B (+1%)
build/active-filters.js 8 kB +1 B (0%)
build/all-products-frontend.js 23.5 kB +50 B (0%)
build/all-products.js 38.3 kB -19 B (0%)
build/atomic-block-components/add-to-cart--atomic-block-components/button--atomic-block-components/image---a7e2bb9b.js 3.19 kB +1 B (0%)
build/atomic-block-components/add-to-cart--atomic-block-components/button.js 1.81 kB +1 B (0%)
build/atomic-block-components/button.js 875 B +2 B (0%)
build/atomic-block-components/image.js 1.36 kB -1 B (0%)
build/atomic-block-components/price.js 2.11 kB +3 B (0%)
build/atomic-block-components/title.js 1.47 kB -3 B (0%)
build/attribute-filter-frontend.js 18.2 kB +52 B (0%)
build/attribute-filter.js 12.1 kB +5 B (0%)
build/cart-blocks/line-items-frontend.js 5.87 kB -1 B (0%)
build/cart-frontend.js 52.6 kB +49 B (0%)
build/cart.js 50.5 kB +1 B (0%)
build/checkout-frontend.js 54.9 kB +50 B (0%)
build/checkout.js 53.9 kB +119 B (0%)
build/featured-product.js 9.43 kB -5 B (0%)
build/handpicked-products.js 6.27 kB +5 B (0%)
build/legacy-template.js 2.05 kB -2 B (0%)
build/mini-cart-component-frontend.js 44.8 kB +249 B (+1%)
build/mini-cart.js 6.11 kB +5 B (0%)
build/price-filter-frontend.js 14.2 kB +57 B (0%)
build/price-filter.js 9.66 kB +2 B (0%)
build/product-best-sellers.js 6.62 kB +3 B (0%)
build/product-category.js 7.48 kB -9 B (0%)
build/product-new.js 6.77 kB -1 B (0%)
build/product-on-sale.js 7.11 kB +2 B (0%)
build/product-search.js 2.71 kB +1 B (0%)
build/product-tag.js 6.6 kB +1 B (0%)
build/products-by-attribute.js 7.71 kB -1 B (0%)
build/reviews-by-product.js 13 kB -2 B (0%)
build/reviews-frontend.js 9.03 kB +59 B (+1%)
build/single-product-frontend.js 26.9 kB +55 B (0%)
build/single-product.js 10 kB +1 B (0%)
build/stock-filter-frontend.js 8.68 kB +55 B (+1%)
build/stock-filter.js 7.81 kB +2 B (0%)
build/wc-blocks-vendors.js 255 kB -23 B (0%)
build/wc-blocks.js 3.49 kB -3 B (0%)
build/mini-cart-contents.js 2.35 kB +2.35 kB (new file) 🆕
ℹ️ View Unchanged
Filename Size
build/all-reviews.js 9.56 kB
build/atomic-block-components/add-to-cart-frontend.js 8.34 kB
build/atomic-block-components/add-to-cart.js 7.85 kB
build/atomic-block-components/button-frontend.js 1.74 kB
build/atomic-block-components/category-list-frontend.js 466 B
build/atomic-block-components/category-list.js 469 B
build/atomic-block-components/image-frontend.js 1.71 kB
build/atomic-block-components/price-frontend.js 2.13 kB
build/atomic-block-components/rating-frontend.js 562 B
build/atomic-block-components/rating.js 566 B
build/atomic-block-components/sale-badge-frontend.js 861 B
build/atomic-block-components/sale-badge.js 868 B
build/atomic-block-components/sku-frontend.js 390 B
build/atomic-block-components/sku.js 393 B
build/atomic-block-components/stock-indicator-frontend.js 611 B
build/atomic-block-components/stock-indicator.js 610 B
build/atomic-block-components/summary-frontend.js 907 B
build/atomic-block-components/summary.js 912 B
build/atomic-block-components/tag-list-frontend.js 468 B
build/atomic-block-components/tag-list.js 471 B
build/atomic-block-components/title-frontend.js 1.47 kB
build/blocks-checkout.js 21.3 kB
build/cart-blocks/accepted-payment-methods-frontend.js 1.39 kB
build/cart-blocks/checkout-button-frontend.js 1.21 kB
build/cart-blocks/empty-cart-frontend.js 348 B
build/cart-blocks/express-payment--checkout-blocks/express-payment--checkout-blocks/payment-frontend.js 4.73 kB
build/cart-blocks/express-payment-frontend.js 1.59 kB
build/cart-blocks/filled-cart-frontend.js 805 B
build/cart-blocks/items-frontend.js 302 B
build/cart-blocks/order-summary--checkout-blocks/billing-address--checkout-blocks/shipping-address-frontend.js 3.69 kB
build/cart-blocks/order-summary-frontend.js 7.4 kB
build/cart-blocks/totals-frontend.js 324 B
build/checkout-blocks/actions-frontend.js 1.48 kB
build/checkout-blocks/billing-address-frontend.js 2.64 kB
build/checkout-blocks/contact-information-frontend.js 3.62 kB
build/checkout-blocks/express-payment-frontend.js 1.93 kB
build/checkout-blocks/fields-frontend.js 348 B
build/checkout-blocks/order-note-frontend.js 1.25 kB
build/checkout-blocks/order-summary-frontend.js 12.9 kB
build/checkout-blocks/payment-frontend.js 4.28 kB
build/checkout-blocks/shipping-address-frontend.js 2.72 kB
build/checkout-blocks/shipping-methods-frontend.js 5.54 kB
build/checkout-blocks/terms-frontend.js 1.29 kB
build/checkout-blocks/totals-frontend.js 329 B
build/featured-category.js 7.74 kB
build/mini-cart-frontend.js 2.34 kB
build/price-format.js 1.37 kB
build/product-categories.js 3.38 kB
build/product-top-rated.js 6.74 kB
build/reviews-by-category.js 11.5 kB
build/vendors--atomic-block-components/add-to-cart--cart-blocks/order-summary--checkout-blocks/billing-ad--c5eb4dcd-frontend.js 16.1 kB
build/vendors--atomic-block-components/add-to-cart-frontend.js 4.77 kB
build/vendors--atomic-block-components/price--cart-blocks/line-items--cart-blocks/order-summary--checkout--8a3571de-frontend.js 5.71 kB
build/vendors--cart-blocks/line-items--checkout-blocks/order-summary-frontend.js 3.14 kB
build/vendors--cart-blocks/order-summary--checkout-blocks/billing-address--checkout-blocks/order-summary---eb4d2cec-frontend.js 5.03 kB
build/wc-blocks-data.js 11.3 kB
build/wc-blocks-editor-style-rtl.css 15.7 kB
build/wc-blocks-editor-style.css 15.7 kB
build/wc-blocks-google-analytics.js 1.98 kB
build/wc-blocks-middleware.js 1.19 kB
build/wc-blocks-registry.js 3.71 kB
build/wc-blocks-shared-context.js 1.54 kB
build/wc-blocks-shared-hocs.js 1.92 kB
build/wc-blocks-style-rtl.css 21.1 kB
build/wc-blocks-style.css 21.1 kB
build/wc-blocks-vendors-style-rtl.css 1.37 kB
build/wc-blocks-vendors-style.css 1.37 kB
build/wc-payment-method-bacs.js 806 B
build/wc-payment-method-cheque.js 806 B
build/wc-payment-method-cod.js 898 B
build/wc-payment-method-paypal.js 839 B
build/wc-payment-method-stripe.js 12.2 kB
build/wc-settings.js 2.91 kB

compressed-size-action

@Aljullu Aljullu force-pushed the fix/mini-cart-as-template-part branch 3 times, most recently from 1c21969 to de2ab4d Compare November 12, 2021 13:00
@woocommerce woocommerce deleted a comment from github-actions bot Nov 12, 2021
@woocommerce woocommerce deleted a comment from github-actions bot Nov 12, 2021
@woocommerce woocommerce deleted a comment from github-actions bot Nov 12, 2021
@woocommerce woocommerce deleted a comment from github-actions bot Nov 12, 2021
@woocommerce woocommerce deleted a comment from github-actions bot Nov 12, 2021
@woocommerce woocommerce deleted a comment from github-actions bot Nov 12, 2021
@@ -36,7 +36,7 @@ const setUp = (): void => {
const addListeners = (): void => {
setUp();

if ( ! window.wcBlocksStoreCartListeners.count ) {
if ( window.wcBlocksStoreCartListeners.count === 0 ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not directly related to this PR, but still wanted to add it. 🙂

count is going to be an integer, so it's more precise to compare it to 0 than to check if it's falsy.

@Aljullu Aljullu added skip-changelog PRs that you don't want to appear in the changelog. status: needs review labels Nov 15, 2021
@Aljullu Aljullu marked this pull request as ready for review November 15, 2021 10:28
@Aljullu Aljullu changed the title WIP: Mini Cart as template part Mini Cart as template part Nov 15, 2021
Copy link
Member

@dinhtungdu dinhtungdu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is working great on my end. I just have some minor comments:

  • Because we support the template parts now, we need to pass the template type to line this and this line. Without this change, the edit link of the Mini cart content template is broken because of wrong template type.
  • I got a warning about the missing area property of the template part: PHP Warning: Undefined property: WP_Block_Template::$area in /wp-content/plugins/gutenberg/lib/compat/wordpress-5.9/class-gutenberg-rest-templates-controller.php on line 418.

@Aljullu Aljullu force-pushed the fix/mini-cart-as-template-part branch 2 times, most recently from 550e0bf to 63222ba Compare November 18, 2021 16:26
@Aljullu
Copy link
Contributor Author

Aljullu commented Nov 18, 2021

Thanks for the review @dinhtungdu!

  • Because we support the template parts now, we need to pass the template type to line this and this line. Without this change, the edit link of the Mini cart content template is broken because of wrong template type.

Good catch! Should be fixed in a730af0.

  • I got a warning about the missing area property of the template part: PHP Warning: Undefined property: WP_Block_Template::$area in /wp-content/plugins/gutenberg/lib/compat/wordpress-5.9/class-gutenberg-rest-templates-controller.php on line 418.

For now, I set it to uncategorized in 63222ba, but wondering if we should create a Mini Cart specific area. What do you think?

I think this issue (WordPress/gutenberg#35989) is blocking us, but might be good to take a decision anyway so we can create an issue in our repo and follow-up based on it.

@dinhtungdu
Copy link
Member

For now, I set it to uncategorized in 63222ba, but wondering if we should create a Mini Cart specific area. What do you think?

@Aljullu At the first glance, it seems that the Mini Cart only needs one template part because we only allow one mini cart per page. But given that we have multiple header template parts, and Mini Cart is "usually" added to the header, the cart content style should match with the header (and page style), to do that, we need an area for Mini Cart. So for your question, my answer is yes, we should create a Mini Cart area.

@dinhtungdu
Copy link
Member

Note that with the latest changes merged to trunk, we can create template parts for custom areas. All custom templates created for any custom areas are displayed correctly as well, so I think the WordPress/gutenberg#35989 can be marked as fixed.

image

@Aljullu
Copy link
Contributor Author

Aljullu commented Nov 19, 2021

TIL, thanks for letting me know @dinhtungdu! I created three follow-up issues based on that: #5194, #5195 and #5196. Could you review them and let me know if they make sense to you and look like the correct next steps?

Regarding this PR, what do you think about merging it in the way it currently works and handling anything else in follow-ups?

Copy link
Member

@dinhtungdu dinhtungdu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aljullu This is working great on my end, I don't get any PHP error or warning 👍🏽 . I have some minor comments btw, please check the review comment for more detail.

Comment on lines 112 to 114
if ( 'wp_template_part' === $post->post_type ) {
$template->area = 'uncategorized';
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should get the template area from the database instead of hard-coding it like this. We can get the area of a template part using get_the_terms( $post, 'wp_template_part_area' );

Comment on lines 143 to 145
if ( 'wp_template_part' === $template_type ) {
$template->area = 'uncategorized';
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this code, all template parts will have area set to uncategorized. This is fine for now. But I think we need to add a todo here to improve this in the future. Each template part may have a different area, and we can filter that area base on the slug IMO.

@dinhtungdu
Copy link
Member

I created three follow-up issues based on that: #5194, #5195 and #5196. Could you review them and let me know if they make sense to you and look like the correct next steps?

@Aljullu The follow-up issues make a lot of sense to me! The #5196 is actually a very nice UX enhancement! 🎉

Regarding this PR, what do you think about merging it in the way it currently works and handling anything else in follow-ups?

I think we should fix #5025 (comment) and leave #5025 (comment) for the next release.

@Aljullu
Copy link
Contributor Author

Aljullu commented Nov 19, 2021

Thanks for taking another look @dinhtungdu!

I think we should fix #5025 (comment) and leave #5025 (comment) for the next release.

Actually, in 5ee58d2 I tried to fix it in both places, but happy to undo the second change if you are not comfortable with it. In any case, I think that's something that we can keep iterating on in the follow-up issues.

Copy link
Member

@dinhtungdu dinhtungdu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, in 5ee58d2 I tried to fix it in both places, but happy to undo the second change if you are not comfortable with it. In any case, I think that's something that we can keep iterating on in the follow-up issues.

@Aljullu 5ee58d2 looks good to me. Initially, I think it's better to extract the logic for the template part area into its own function (under the BlockTemplateUtils class). But given that this PR is already large, and we haven't known if we need other template part areas yet. So I think we so keep it as it is for now, and improve it later if we actually have so many template part areas.

Comment on lines 307 to 311
if ( 'wp_template_part' === $template_type ) {
$dir_name = self::TEMPLATE_PARTS_DIR_NAME;
} else {
$dir_name = self::TEMPLATES_DIR_NAME;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move this check outside of the foreach loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 51930ed.

@Aljullu Aljullu merged commit 95ac903 into trunk Nov 19, 2021
@Aljullu Aljullu deleted the fix/mini-cart-as-template-part branch November 19, 2021 11:47
jonny-bull pushed a commit to jonny-bull/woocommerce-gutenberg-products-block that referenced this pull request Dec 14, 2021
* Fix wrong event prefix in doc comment

* Make className prop in CartLineItemsTableProps optional

* Mini Cart as template part

* Remove BlockTemplatePartsController and instead use BlockTemplatesController

* Remove old code

* Clean up frontend rendering

* Update tests

* Improve if clause

* Fix wrong tests title

* Fix wrong variable name

* Make sure Mini Cart contents block is unmounted whem mini cart closes or unmounts

* Remove unnecessary waitFor

* Fix PaymentMethodDataProvider wrong children type

* TypeScript fixes

* Make comment shorter

* Remove test code

* Fix contant unmounts of Mini Cart contents block

* Fix wrong template_type passed

* Set Template part area to 'uncategorized'

* Set Template part area to the correct value

* Move template dir check outside loop
jonny-bull pushed a commit to jonny-bull/woocommerce-gutenberg-products-block that referenced this pull request Dec 16, 2021
* Fix wrong event prefix in doc comment

* Make className prop in CartLineItemsTableProps optional

* Mini Cart as template part

* Remove BlockTemplatePartsController and instead use BlockTemplatesController

* Remove old code

* Clean up frontend rendering

* Update tests

* Improve if clause

* Fix wrong tests title

* Fix wrong variable name

* Make sure Mini Cart contents block is unmounted whem mini cart closes or unmounts

* Remove unnecessary waitFor

* Fix PaymentMethodDataProvider wrong children type

* TypeScript fixes

* Make comment shorter

* Remove test code

* Fix contant unmounts of Mini Cart contents block

* Fix wrong template_type passed

* Set Template part area to 'uncategorized'

* Set Template part area to the correct value

* Move template dir check outside loop
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
block: mini-cart Issues related to the Mini-Cart block. focus: FSE Work related to prepare WooCommerce for FSE. skip-changelog PRs that you don't want to appear in the changelog.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants