Automattic / newspack-blocks

Gutenberg blocks for the Newspack project.
208 stars 43 forks source link

fix: add check if product can be purchased #1916

Closed laurelfulford closed 1 month ago

laurelfulford commented 1 month ago

All Submissions:

Changes proposed in this Pull Request:

This PR uses WooCommerce's purchaseable check to see if a product can be purchased before it can be assigned to a Checkout Button block, or before the Checkout Button block is rendered on the front-end.

This change is to fix an edge case where you can assign a price-less product to the Checkout Button block, which causes the modal checkout to throw an error. Some products -- like NYP products -- don't seem to throw the same error even if they don't have a suggested or minimum price set; using the purchaseable check seems to account for this but I'd definitely appreciate thoughts about this approach!

See 1207817176293825-as-1208327956169322

How to test the changes in this Pull Request:

  1. Set up a few different products, including:
    • A regular simple product with a price.
    • A product with variations with prices.
    • A simple product without a price.
    • A product with variations, at least one without a price.
    • A regular NYP product, with a minimum or suggested price.
    • A NYP product without a minimum and suggested price.
  2. View the /shop page, and confirm that some of your products can't be purchased (they should include a link to the individual product screen, but no purchase button). This should just be your product with no price, unless I've messed up a test case.
  3. Set up a checkout button block for each. Note that you can do this with the product without a price, but it will throw an error on the front-end about subscriptions, or just return a button:

CleanShot 2024-10-21 at 15 17 43

  1. Apply this PR and run npm run build.
  2. On a new page, try to add a checkout button block for each product. Note your price-less product won't be returned when you search for products in the sidebar.
  3. On the old page, confirm that the price-less product is still in the editor, but no longer renders on the front-end.

Other information:

laurelfulford commented 1 month ago

My only concern is the NYP w/o a suggested or minimum price case.

Ahhh, that's a good point!

I've added a check for NYP on the front-end (don't show buttons for products without prices), but in the editor they're still available since you can set a price for the block once you pick a product. Hopefully this isn't too confusing!

This should probably need some more testing steps, like:

  1. Checking if a button shows when the product has each a suggested or minimum price, or both.
  2. Checking if a button shows when the product has a price set on the button itself (in the sidebar).

Let me know if that makes sense, or if you have any questions -- thanks @chickenn00dle!!

laurelfulford commented 1 month ago

Thanks @chickenn00dle! 🙌

laurelfulford commented 1 month ago

Probably a bit of overkill, but I created a PR to merge trunk into epic/ras-acc here just to make sure the NYP stuff got removed, and nothing else weird happened (the view.php files are VERY different!).

matticbot commented 3 weeks ago

:tada: This PR is included in version 4.3.5-alpha.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

matticbot commented 2 weeks ago

:tada: This PR is included in version 4.3.5 :tada:

The release is available on:

Your semantic-release bot :package::rocket: