Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement @woocommerce/price-format dependency #29

Open
wants to merge 25 commits into
base: trunk
Choose a base branch
from

Conversation

nielslange
Copy link
Member

@nielslange nielslange commented Oct 22, 2021

Fixes #28

Note

The remaining price should be formatted using the @woocommerce/price-format dependency, which is included in the WooCommerce Blocks plugin.

Testing instructions

Before all test cases

  1. Check out this PR.
  2. Create a test page and add the Cart block.
  3. Insert the Free Shipping Progress Bar block into the Cart block and save the page.
  4. Set the free shipping value within the Free Shipping Progress Bar block to 1200.
  5. Head over to the frontend and add products to your cart.
  6. Ensure the cart total is higher than 1000 (to test the thousand separator) and below 1200 (to see the remaining price).

Test 0 decimals currency

  1. Go to WP Admin » WooCommerce » Settings » Currency options and use the following settings:
    • Currency: Indonesian rupiah (Rp)
    • Currency position: Left
    • Thousand separator: .
    • Decimal separator: ,
    • Number of decimals: 0
  2. Go to the cart and verify that the price is formatted as expected.

Test 2 decimals currency

  1. Go to WP Admin » WooCommerce » Settings » Currency options and use the following settings:
    • Currency: USD $
    • Currency position: Left + space
    • Thousand separator: ,
    • Decimal separator: .
    • Number of decimals: 2
  2. Go to the cart and verify that the price is formatted as expected.

Test 3 decimals currency

  1. Go to WP Admin » WooCommerce » Settings » Currency options and use the following settings:
    • Currency: Libyan Dinar
    • Currency position: Right + space
    • Thousand separator: .
    • Decimal separator: ,
    • Number of decimals: 3
  2. Go to the cart and verify that the price is formatted as expected.

Base automatically changed from add/25-register-block-in-cart-i2 to trunk October 25, 2021 10:00
@nielslange nielslange marked this pull request as draft November 4, 2021 11:19
@nielslange nielslange changed the title Add helper function to format price Implement @woocommerce/price-format dependency Nov 15, 2021
@nielslange nielslange marked this pull request as ready for review November 26, 2021 14:49
@nielslange nielslange force-pushed the add/28-implement-@woocommerce-price-format-dependency branch from af79156 to 6fc3130 Compare November 27, 2021 07:57
Copy link
Member

@senadir senadir left a comment

Choose a reason for hiding this comment

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

I'm finding it hard to follow what's happening, I left some comments about (what I believe to be) redundant code and eager abstraction.

To my mind, this is how it should be done:

  1. Convert the string provided from "50" to 5000. This is, to my knowledge, not something that `@woocommerce/format-price provide, but is something we're doing in Filter By Product block.
  2. Do the math.
  3. Format result back.

This should all happen directly on the component, no need for extra utils that are one time use, depending on how or easy 1 is, it might get its own utility function to reduce clutter, the rest can stay.

Comment on lines 22 to 25
const currentTotal = getCurrentTotal( cart );
const minorUnit = getMinorUnit( cart );
const remaining = getRemaining( freeShippingFrom, currentTotal, minorUnit );
const currency = getCurrencyFormat( cart );
Copy link
Member

Choose a reason for hiding this comment

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

This seems like an early abstraction that shouldn't happen yet until you actually need it, further more, some of functions are useless if you check for cart here.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure what you mean with the early abstraction. Could you elaborate on that? And could you tell me which functions you think are useless here? I created them to extract the cart total, the minor units and the currency format from the cart object, if a cart object is available.

src/util.js Show resolved Hide resolved
src/util.js Outdated Show resolved Hide resolved
src/util.js Outdated Show resolved Hide resolved
src/util.js Show resolved Hide resolved
src/util.js Outdated
* @param {Object} cart The cart object.
* @returns {Object} The currency format object.
*/
export function getCurrencyFormat( cart ) {
Copy link
Member

Choose a reason for hiding this comment

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

There seems to be a lot of redundant logic here, you can get currency by using getCurrencyFromPriceResponse( cart.cartTotals )

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. I removed my function getCurrencyFormat() and I'm using getCurrencyFromPriceResponse() instead.

@nielslange
Copy link
Member Author

I'm finding it hard to follow what's happening, I left some comments about (what I believe to be) redundant code and eager abstraction.

To my mind, this is how it should be done:

  1. Convert the string provided from "50" to 5000. This is, to my knowledge, not something that `@woocommerce/format-price provide, but is something we're doing in Filter By Product block.
  2. Do the math.
  3. Format result back.

That's pretty much what I'm doing here. As for converting "50" to 5000, this would only be correct when the merchant selected 2 decimals to display. For 3 decimals, the minor unit price should be 50000 and for 0 decimals, the minor unit price should remain 50.

This should all happen directly on the component, no need for extra utils that are one time use, depending on how or easy 1 is, it might get its own utility function to reduce clutter, the rest can stay.

I moved the helper functions into util.js, as I'm calling the function getCurrentTotal() both in src/components/progress-bar/index.js and in src/components/progress-label/index.js. In addition, it keeps the individual components leaner which, in my opinion, improves both readability and maintainability.

As I'm using getMinorUnit() and getRemaining() only in src/components/progress-label/index.js, I could technically move these two functions directly into the corresponding component. I decided against that, as I believe it makes more sense to have all related functions in one file instead of spreading them across the project.

I'm looking forward to hearing your thoughts on my feedback regarding your review and the additional notes I added before.

Copy link
Member

@mikejolley mikejolley left a comment

Choose a reason for hiding this comment

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

Changes look good, and Nadirs feedback was resolved. I made some small suggestions but nothing blocking. Test coverage also good.

src/util.js Outdated Show resolved Hide resolved
src/util.js Outdated Show resolved Hide resolved
src/util.js Outdated Show resolved Hide resolved
…of github.com:woocommerce/woocommerce-free-shipping-progress-bar-block into add/28-implement-@woocommerce-price-format-dependency

# Conflicts:
#	src/components/progress-bar/index.js
#	src/components/progress-label/index.js
@nielslange
Copy link
Member Author

I applied all changes and fixed the integration tests, which broke due to adding @woocommerce/price-format in this change. Do you want to run another review, @mikejolley?

@nielslange nielslange mentioned this pull request Feb 3, 2022
@nielslange nielslange requested a review from opr March 30, 2022 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add @woocommerce/price-format dependency
4 participants