Shopware购物车存储库名称

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

Shopware cart repository name

问题

我在我的代码中从购物车存储库检索数据时遇到了问题。当我尝试使用const cartRepository = this.repositoryFactory.create('cart');访问购物车存储库时,我遇到了以下错误:

在当前模块中捕获到错误:错误:找不到实体类型'cart'的定义。

以下是我的完整代码:

import template from './cartCancelRate.html.twig';
const { Criteria } = Shopware.Data;

Shopware.Component.register('cartCancelRate', {
    template,
    inject: [
        'repositoryFactory',
        'stateStyleDataProviderService',
        'acl',
    ],

    data() {
        return {
            cancelRate: 0
        };
    },

    methods: {
        fetchData() {
            const cartRepository = this.repositoryFactory.create('cart');

            const criteria = new Criteria();
            criteria.addFilter(
                this.criteriaFactory.range(
                    'createdAt',
                    { lt: new Date().toISOString().split('T')[0] } // Excluding today's date
                )
            );

            cartRepository.search(criteria, Shopware.Context.api).then((result) => {
                const cartCount = result.total;

                // Get the total number of orders
                const orderRepository = this.repositoryFactory.create('order');
                orderRepository.search(criteria, Shopware.Context.api).then((orderResult) => {
                    const orderCount = orderResult.total; // Total number of orders

                    // Calculate cancel rate
                    this.cancelRate = cartCount / orderCount;
                });
            });
        }
    },

    created() {
        this.fetchData()
    }
});
英文:

I'm having trouble retrieving data from the cart repository in my code. When I try to access the cart repository using const cartRepository = this.repositoryFactory.create('cart');, I encounter the following error:

An error was captured in the current module: Error: [EntityDefinitionRegistry] No definition found for entity type 'cart'.

Below is my complete code:

import template from './cartCancelRate.html.twig';
const { Criteria } = Shopware.Data;

Shopware.Component.register('cartCancelRate', {
    template,
    inject: [
        'repositoryFactory',
        'stateStyleDataProviderService',
        'acl',
    ],

    data() {
        return {
            cancelRate: 0
        };
    },

    methods: {
        fetchData() {
            const cartRepository = this.repositoryFactory.create('cart');

            const criteria = new Criteria();
            criteria.addFilter(
                this.criteriaFactory.range(
                    'createdAt',
                    { lt: new Date().toISOString().split('T')[0] } // Excluding today's date
                )
            );

            cartRepository.search(criteria, Shopware.Context.api).then((result) => {
                const cartCount = result.total;

                // Get the total number of orders
                const orderRepository = this.repositoryFactory.create('order');
                orderRepository.search(criteria, Shopware.Context.api).then((orderResult) => {
                    const orderCount = orderResult.total; // Total number of orders

                    // Calculate cancel rate
                    this.cancelRate = cartCount / orderCount;
                });
            });
        }
    },

    created() {
        this.fetchData()
    }
});

答案1

得分: 1

英文:

The cart table has no entity definition and therefore also no attached repository. In the administration you could inject cartStoreService and call getCart but you will need the specific context token, which you will likely not have at hand.

It might be best to take a look at the SalesChannelProxyController which is requested by the service. You could implement your own admin-API endpoint where you could fetch data directly from the cart table using an injected instance of Doctrine\DBAL\Connection. Keep in mind though, that this is not data meant to be accessed directly, hence why there is no repository. You're supposed to use the appropriate abstractions for the carts that handle calculations and such. So what you might want to do is fetch the tokens from sales_channel_api_context and use them to get the properly calculated carts. Regarding performance impacts, this would only lend itself to a detailed view of a single cart and not to an entire listing of carts though. What you could ultimately do is to fetch all carts directly from the table for the listing and then have a detail view that uses cartStoreService to get the properly calculated cart via the token.

huangapple
  • 本文由 发表于 2023年7月18日 16:45:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710982.html
匿名

发表评论

匿名网友

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

确定