如何在Shopware 6中添加最低订单金额?

huangapple go评论53阅读模式
英文:

how to add a minimum line order value shopware 6

问题

我想知道是否有启用最低线值的选项?
例如,1个零件的成本为0.06,我启用了所有项目的最低1欧元价值规则,因此这将是16/17个零件。

希望有人知道是否可以实现这一点。

英文:

I would like to know if there is an option to enable a sort of minimum line value?
for exemple 1 part costs 0.06 that i enable a minimum rule of minimum 1 euro value to all items. so this would be 16/17pcs.

Hope someone knows if this can be done.

答案1

得分: 1

product 中的 minPurchase 字段(或者在管理产品详细信息中的 "最小订购数量")与物品的数量相关,而不是所有物品的总价。因此,在您的示例中,如果购物车中该产品的行项目应该至少达到1欧元,您需要将 minPurchase 设置为17。

如果值得你的时间,你可以通过向产品添加一个自定义字段来以编程方式执行此操作,在该字段中输入最低货币金额。然后订阅 sales_channel.product.loaded 事件,并根据自定义字段的值动态设置 minPurchase 字段。

public function loaded(EntityLoadedEvent $event): void
{
    /** @var SalesChannelProductEntity $product */
    foreach ($event->getEntities() as $product) {
        $customFields = $product->getCustomFields();
        $minMonetaryPurchase = $customFields['min_monetary_purchase'] ?? null;

        if (!$minMonetaryPurchase) {
            continue;
        }

        $minPurchase = ceil($minMonetaryPurchase / $product->getCalculatedPrice()->getUnitPrice());
        $product->setMinPurchase((int) $minPurchase);
    }
}
英文:

There's the minPurchase field of a product (or "Min. order quantity" within the details of a product in the administration. However this relates to the quantity of an item not the total price of all items. So in your example you'd have to set minPurchase to 17 if in the cart the line items for that product should amount to a minimum of 1 Euro.

If it is worth your time, you could do this programmatically by adding a custom field to the product where you enter the minimum monetary amount. Then subscribe to sales_channel.product.loaded and set the minPurchase field dynamically depending on the custom field value.

public function loaded(EntityLoadedEvent $event): void
{
    /** @var SalesChannelProductEntity $product */
    foreach ($event->getEntities() as $product) {
        $customFields = $product->getCustomFields();
        $minMonetaryPurchase = $customFields['min_monetary_purchase'] ?? null;

        if (!$minMonetaryPurchase) {
            continue;
        }

        $minPurchase = ceil($minMonetaryPurchase / $product->getCalculatedPrice()->getUnitPrice());
        $product->setMinPurchase((int) $minPurchase);
    }
}

答案2

得分: 0

你可以添加一个购物车验证器,如文档中所述

你需要实现自己的Shopware\Core\Checkout\Cart\CartValidatorInterface,注册它,并添加自己的错误,扩展Shopware\Core\Checkout\Cart\Error\Error

在你的services.xml中进行注册:

<services>
    <service id="Swag\BasicExample\Core\Checkout\Cart\Custom\CustomCartValidator">
        <tag name="shopware.cart.validator"/>
    </service>
</services>

通过这种方式,你可以遍历购物车中的所有行项目/产品,对它们进行验证,并在其中一个不通过验证时引发错误。

英文:

You can add a cart validator, es described in the docs

You implement your own Shopware\Core\Checkout\Cart\CartValidatorInterface, register, add your own error extending Shopware\Core\Checkout\Cart\Error\Error

Register it in your services.xml

&lt;services&gt;
    &lt;service id=&quot;Swag\BasicExample\Core\Checkout\Cart\Custom\CustomCartValidator&quot;&gt;
        &lt;tag name=&quot;shopware.cart.validator&quot;/&gt;
    &lt;/service&gt;
&lt;/services&gt;

And this way you can run through all line items/products in your cart, validate them and throw an error if one of them doesn't validate.

huangapple
  • 本文由 发表于 2023年3月1日 15:13:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定