You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Prestashop单价单位处理:如何提取不含价格的单价后缀

Got it, let's tackle this problem! You're trying to extract just the unit suffix (like 'KG') from the combined price+unit string that comes from $product.unit_price_full in PrestaShop, right? Here are two solid approaches, with the first being the most reliable:

The cleanest way is to skip splitting the combined string entirely—PrestaShop stores the unit separately in the product object. You can directly call $product.unit which holds exactly the suffix you want (e.g., 'KG').

If you want to keep the "per [unit]" wording without the price, use the translation string like this:

{l s='per %unit%' d='Shop.Theme.Catalog' sprintf=['%unit%' => $product.unit]}

Or if you just need the raw unit (like just 'KG'):

{$product.unit}

Why this is better? Splitting the combined string depends on the exact format of the translated unit_price_full (like the word 'per' being present), which can break if you switch languages or update translations. Using the direct unit variable avoids that entirely.

2. Split the Combined String (Fallback for Edge Cases)

If for some reason you can't use $product.unit (maybe a custom setup), you can split the unit_price_full string using Smarty's string manipulation functions.

Assuming the output follows the pattern "X€ per KG", split on the 'per ' substring and grab the last part:

{* Split the string into parts using 'per ' as the delimiter *}
{assign var="unit_parts" value=$product.unit_price_full|explode:'per '}
{* Get the last element of the array, which is your target unit *}
{assign var="unit_only" value=$unit_parts|last}
{* Output the clean unit *}
{$unit_only}

⚠️ Heads up: This relies on the translated string always including 'per ' before the unit. If you support multiple languages (e.g., French uses 'par ' instead), you'll need to adjust the delimiter based on the current language, making this approach less maintainable long-term.

内容的提问来源于stack exchange,提问作者microb14

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 09:32:29