-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to edit bundle products from the cart page
Magento 2.4.7: Render discounts for bundle products Calculate the product page price dynamically based on the options and quantities selected.
- Loading branch information
Showing
13 changed files
with
338 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphcommerce/magento-product-bundle': patch | ||
--- | ||
|
||
Add the ability to edit bundle products from the cart page |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphcommerce/magento-product-bundle': patch | ||
--- | ||
|
||
Magento 2.4.7: Render discounts for bundle products |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphcommerce/magento-product-bundle': patch | ||
--- | ||
|
||
Calculate the product page price dynamically based on the options and quantities selected. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 8 additions & 8 deletions
16
packages/magento-product-bundle/components/BundleProductOptions/BundleProductOptions.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...magento-product-bundle/components/BundleProductOptions/calculateBundleOptionValuePrice.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { CurrencyEnum } from '@graphcommerce/graphql-mesh' | ||
import type { ProductListPriceFragment } from '@graphcommerce/magento-product/components' | ||
import type { BundleProductItemOptionType, BundleProductItemType, BundleProductType } from './types' | ||
|
||
export type CalculatedBundleOptionValuePrice = [number, number] // [regularPrice, finalPrice] | ||
|
||
export function calculateBundleOptionValuePrice( | ||
product: BundleProductType, | ||
item: BundleProductItemType, | ||
option: BundleProductItemOptionType, | ||
quantity = 1, | ||
): CalculatedBundleOptionValuePrice { | ||
const { dynamic_price = false } = product | ||
const precentOff = item?.price_range.minimum_price.discount?.percent_off ?? 0 | ||
|
||
const regularPrice = | ||
(dynamic_price ? option.product?.price_range.minimum_price.final_price.value : option.price) ?? | ||
0 | ||
|
||
const finalPrice = regularPrice * (1 - precentOff / 100) | ||
return [regularPrice * quantity, finalPrice * quantity] | ||
} | ||
|
||
export function sumCalculatedBundleOptionValuePrices( | ||
prices: CalculatedBundleOptionValuePrice[], | ||
): CalculatedBundleOptionValuePrice { | ||
return prices.reduce((acc, [regular, final]) => [acc[0] + regular, acc[1] + final], [0, 0]) | ||
} | ||
|
||
export function substractCalculatedBundleOptionValuePrices( | ||
basePrice: CalculatedBundleOptionValuePrice, | ||
substractPrice: CalculatedBundleOptionValuePrice, | ||
): CalculatedBundleOptionValuePrice { | ||
return [basePrice[0] - substractPrice[0], basePrice[1] - substractPrice[1]] | ||
} | ||
|
||
export function toProductListPriceFragment( | ||
price: CalculatedBundleOptionValuePrice, | ||
currency: CurrencyEnum | null | undefined, | ||
): ProductListPriceFragment { | ||
return { | ||
regular_price: { currency: currency, value: price[0] }, | ||
final_price: { currency, value: price[1] }, | ||
} | ||
} |
34 changes: 24 additions & 10 deletions
34
packages/magento-product-bundle/components/BundleProductOptions/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,32 @@ | ||
import type { ActionCardItemRenderProps } from '@graphcommerce/ecommerce-ui' | ||
import type { ActionCardListProps } from '@graphcommerce/next-ui' | ||
import type { BundleProductOptionsFragment } from './BundleProductOptions.gql' | ||
import type { ProductPageItem_BundleFragment } from '../../graphql' | ||
|
||
export type BundleProductType = ProductPageItem_BundleFragment | ||
export type BundleProductItemType = NonNullable<NonNullable<BundleProductType['items']>[number]> | ||
export type BundleProductItemOptionType = NonNullable< | ||
NonNullable<BundleProductItemType['options']>[number] | ||
> | ||
|
||
export type BundleOptionProps = { | ||
idx: number | ||
index: number | ||
renderer?: React.FC<ActionCardItemRenderProps<BundleOptionValueProps>> | ||
} & NonNullable<NonNullable<BundleProductOptionsFragment['items']>[number]> & | ||
Pick<ActionCardListProps, 'size' | 'layout' | 'color' | 'variant'> | ||
index: number | ||
product: BundleProductType | ||
item: BundleProductItemType | ||
} & Pick<ActionCardListProps, 'size' | 'layout' | 'color' | 'variant'> | ||
|
||
export type BundleOptionValueProps = NonNullable< | ||
NonNullable<BundleOptionProps['options']>[number] | ||
> & { | ||
idx: number | ||
export type BundleOptionValueProps = { | ||
index: number | ||
required: boolean | ||
product: BundleProductType | ||
item: BundleProductItemType | ||
option: BundleProductItemOptionType | ||
} | ||
|
||
const possibleTypes = ['radio', 'checkbox', 'multi', 'select'] as const | ||
export type BundleOptionType = (typeof possibleTypes)[number] | ||
|
||
export function toBundleOptionType(type: string | null | undefined): BundleOptionType { | ||
if (!type) return 'radio' | ||
if (possibleTypes.includes(type as BundleOptionType)) return type as BundleOptionType | ||
return 'radio' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
export * from './inject/CreditMemoItem_Bundle.gql' | ||
export * from './inject/InvoiceItem_Bundle.gql' | ||
export * from './inject/OrderItem_Bundle.gql' | ||
export * from './inject/ProductListItemBundle.gql' | ||
export * from './inject/ProductPageItem_Bundle.gql' | ||
export * from './inject/ShipmentItem_Bundle.gql' | ||
export * from './fragments/ItemSelectedBundleOption.gql' | ||
export * from './fragments/SelectedBundleOption.gql' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
packages/magento-product-bundle/graphql/inject/ShipmentItem_Bundle.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
fragment ShipmentItem_Bundle on BundleShipmentItem @inject(into: ["ShipmentItem"]) { | ||
bundle_options { | ||
...ItemSelectedBundleOption | ||
} | ||
} |
Oops, something went wrong.