Menu Close
    Start a Project Request quote Reading list Switch to dark mode
    hyperlocal-marketplace-guide

    How to change price of a product in cart in Shopware 6

    In this blog, you are going to learn “How to change the price of a product in the cart in Shopware 6 at the storefront.”
    I hope you know the directory structure of Shopware 6 plugin, if you don’t know, see here- https://docs.shopware.com/en/shopware-platform-dev-en/internals/directory-structure.

     In this example, the prices are fetched from a database table. You have already created your own working plugin with a custom entity for those prices.  If you don’t know that’s done have a look at how to create a custom entity.

    Entity:- ChangePriceEntity.php

    Changing the price

    Change the price of the product in the cart, you should use the collector pattern. For this, you need to create your own cart collector.
    The collector compares the product IDs of the products in the cart with the product IDs from the custom table. If there’s any match, the price has to be overwritten.

    All adjustments are done in the method, where the product items already own a name and a price. If no product in the cart matches your condition, you can early return in the method. Afterward, in addition to this, you have to create a new line item for the new discount. For the latter, you want the line item to not be stackable and it shouldn’t be removable either.

    The respective services.xml, which registers the collector in the first instance. You can learn more about the service. The cart repository is not writable so cart interface help the to edit the cart. Line item service help to remove and add the product in the cart. You can learn about this from the docs of Shopware.

    In the service file, you have to use the tag name shopware.cart.processor and set priority to 4500.
    The quantity price calculator function calculates the price according to the quantity of the product.

    I hope it will help you. Thanks for reading. Happy Coding 🙂
    Thank You.

    . . .

    Comment

    Add Your Comment

    Be the first to comment.

    Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close