1760653498
2025-10-15 15:48:00
一緒に作業するとき コマース コネクトを最適化する、組み込みの価格設定パイプラインが各品目の値を自動的に検証して更新します。 PlacedPrice カタログ価格に基づいています。
これは通常の SKU には最適ですが、ビジネス モデルで許可されている場合はどうでしょうか。 ユーザーが入力した金額、 のような 寄付、 ギフト金額、 または 欲しいものを支払う 製品?
デフォルトでは、コマースの検証フローは、カートが検証または保存されるたびに、ユーザーのカスタム価格をデフォルトのカタログ価格で上書きします。
この投稿では、その方法を学びます 伸ばす IPlacedPriceProcessor 残りの価格設定システムをそのまま維持しながら、Optimizely がユーザー定義の価格を置き換えるのを防ぎます。
の役割を理解する IPlacedPriceProcessor
IPlacedPriceProcessor ラインアイテムがカートに追加されるか検証されるときに、正しいカタログ価格を取得して割り当てるコンポーネントです。
デフォルトの実装 (PlacedPriceProcessor) は、カート検証パイプライン中に実行されます。
// Called internally by DefaultOrderGroupValidator
_placedPriceProcessor.UpdatePlacedPrice(lineItem, customerContact, marketId, currency, onValidationError);
これにより、すべての製品に有効なカタログ価格が設定されるようになりますが、手動で設定された価格は、 lineItem.PlacedPrice 値が置き換えられます。
ユーザーが入力した価格 (寄付など) をサポートするには、次のことができます。 交換する 価格がユーザー インターフェイスから直接決定されたことがわかっている場合、このプロセッサは再価格設定をスキップします。
寄付のシナリオ
あなたのサイトに「ギフトを贈る」ブロックがあり、寄付者は次のことができると想像してください。
- 機会を選択してください (バリエーション)
- 受信者を選択してください
- を入力してください カスタム寄付金額
- 「ギフトを送信」をクリックしてカートに追加します
寄付がコマース カートに入るとパイプラインが実行され、私たちがそれを止めなければ、 PlacedPrice (例: 寄付者が入力した $25) は、デフォルトのカタログ価格 (例: 価格表の $10) に置き換えられます。
解決策: カスタム IPlacedPriceProcessor
ユーザーが入力した価格を保持する最もクリーンかつ安全な方法は、次のとおりです。 デフォルトをラップする IPlacedPriceProcessor 独自の実装を使用してください。
これにより、ラインアイテムに特別なフラグ (例: IsCustomPrice)。
実装
// /Features/Commerce/Pricing/CustomPlacedPriceProcessor.cs
using System;
using EPiServer.Commerce.Catalog.ContentTypes;
using EPiServer.Commerce.Order;
using Mediachase.Commerce;
using Mediachase.Commerce.Customers;
namespace YourProject.Features.Commerce.Pricing
{
public class CustomPlacedPriceProcessor : IPlacedPriceProcessor
{
public const string IsCustomPriceKey = "IsCustomPrice";
private readonly IPlacedPriceProcessor _inner;
public CustomPlacedPriceProcessor(IPlacedPriceProcessor inner)
{
_inner = inner ?? throw new ArgumentNullException(nameof(inner));
}
private static bool IsCustom(ILineItem lineItem)
{
if (lineItem == null) return false;
if (lineItem.Properties.ContainsKey(IsCustomPriceKey))
{
var val = lineItem.Properties[IsCustomPriceKey];
return val is bool b && b && lineItem.PlacedPrice > 0m;
}
return false;
}
public bool UpdatePlacedPrice(
ILineItem lineItem,
CustomerContact customerContact,
MarketId marketId,
Currency currency,
Action onValidationError)
{
// Skip catalog price lookup for user-defined amounts
if (IsCustom(lineItem))
{
return true; // consider price valid as-is
}
return _inner.UpdatePlacedPrice(lineItem, customerContact, marketId, currency, onValidationError);
}
public Money? GetPlacedPrice(
EntryContentBase entry,
decimal quantity,
CustomerContact customerContact,
MarketId marketId,
Currency currency)
{
// delegate for all non-custom cases
return _inner.GetPlacedPrice(entry, quantity, customerContact, marketId, currency);
}
}
}
独自の実装を登録する
context.Services.Intercept((locator, defaultImplementation) =>
new CustomPlacedPriceProcessor(defaultImplementation));
カスタム価格の広告申込情報のマーク付け
カートを作成または更新するとき (たとえば、 CartService またはコントローラ)、カタログ価格をスキップする行をマークします。
if (request.PlacedPrice.HasValue && request.PlacedPrice.Value > 0)
{
lineItem.PlacedPrice = request.PlacedPrice.Value;
lineItem.Properties[CustomPlacedPriceProcessor.IsCustomPriceKey] = true;
}
このフラグはプロセッサに「この値は上書きしないでください。すでに正しい値です。」と伝えます。
今何が起こっているのか
カートが検証されると、次のようになります。
DefaultOrderGroupValidator電話をかける_placedPriceProcessor.UpdatePlacedPrice(...)。- 実装では、
IsCustomPriceフラグ。 - 戻ります
trueすぐに — 内部プロセッサは決して実行されません。 - 寄付者が入力した金額はそのまま残ります。
- 残りの価格設定、割引、税金の計算は通常どおり続行されます。
これが適切な拡張ポイントである理由
多くの開発者は最初にオーバーライドを試みます ILineItemCalculator 検証後にカートを変更することもできますが、それは反応的で脆弱です。
変化 IPlacedPriceProcessor で働いています ソース これは、Optimizely が価格を設定する正確なポイントであり、既存のサービス パイプラインときれいに統合されます。
#Optimizely #Commerce #で #IPlacedPriceProcessor #を使用してカスタム品目価格を維持する方法 #寄付の例 #PowerBuilder