gpt4 book ai didi

php - 添加项目后 Sylius 不记得购物车

转载 作者:搜寻专家 更新时间:2023-10-31 21:32:16 26 4
gpt4 key购买 nike

我正在使用 Sylius 购物车和订单 bundle ,并尝试将商品添加到购物车并显示购物车中商品的摘要。我遇到的问题是 Symfony/Sylius 忘记了它已经制作了一个购物车并向其中添加了元素。

如果我单击一个链接,转到 sylius 购物车的添加页面,由

{{ path('sylius_cart_item_add', {'productId': class.ClassID}) }}

没有错误发生。如果我查看数据库,我可以看到在 sylius_cart 表和 sylius_cart_item 表中创建了一个新条目,其中包含正确的信息;然而,购物车摘要页面什么都不显示,并认为购物车是空的。

如果我尝试添加另一个项目,它会创建另一个新购物车并立即忘记它创建了购物车。

我在 AppKernel.php 中加载了以下包

public function registerBundles()
{
$bundles = array(
//bundles for using the shopping cart
new FOS\RestBundle\FOSRestBundle(),
new JMS\SerializerBundle\JMSSerializerBundle($this),
new Sylius\Bundle\ResourceBundle\SyliusResourceBundle(),
new WhiteOctober\PagerfantaBundle\WhiteOctoberPagerfantaBundle(),
new Sylius\Bundle\MoneyBundle\SyliusMoneyBundle(),
new Sylius\Bundle\OrderBundle\SyliusOrderBundle(),
new Sylius\Bundle\CartBundle\SyliusCartBundle(),
new Sylius\Bundle\SequenceBundle\SyliusSequenceBundle(),


//bundles for styling with bootstrap 3
new Mopa\Bundle\BootstrapBundle\MopaBootstrapBundle(),

//mssql connection library
new Realestate\MssqlBundle\RealestateMssqlBundle(),

//default frameworks
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new Symfony\Bundle\MonologBundle\MonologBundle(),
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new CE\CEBundle\CEBundle(),
);

if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}

return $bundles;
}

我的 ItemResolver 类看起来像

namespace CE\CEBundle\Cart;

use Sylius\Component\Cart\Model\CartItemInterface;
use Sylius\Component\Cart\Resolver\ItemResolverInterface;
use Sylius\Component\Cart\Resolver\ItemResolvingException;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManager;

class ItemResolver implements ItemResolverInterface
{
private $entityManager;

public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}


public function resolve(CartItemInterface $item, $data)
{
//grab the product ID
$productId = $data->get('productId');


if (!$productId || !$product = $this->getProductRepository()->find($productId)) {
throw new ItemResolvingException('Requested product was not found');
}

$item->setProductId($product->getId());
$item->setUnitPrice($product->getClassFee() * 100);

return $item;
}

private function getProductRepository()
{
return $this->entityManager->getRepository('CEBundle:Product');
}
}

我的 CartItem 类看起来像

namespace CE\CEBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Cart\Model\CartItem as BaseCartItem;

/**
* @ORM\Entity
* @ORM\Table(name="sylius_cart_item")
*/
class CartItem extends BaseCartItem
{
/**
* @ORM\Column(type="string", name="product_id", length=8)
*/
private $productId;

public function getProductId()
{
return $this->productId;
}

public function setProductId($Id)
{
$this->productId = $Id;
}
}

我的 config.yml 中有以下配置

imports:
- { resource: parameters.yml }
- { resource: security.yml }

framework:
#esi: ~
#translator: { fallback: "%locale%" }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~

csrf_protection: ~
validation: { enable_annotations: true }
templating:
engines: ['twig']
#assets_version: SomeVersionScheme
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
name: SYLIUS_SESSION
cookie_lifetime: 72000
fragments: ~
http_method_override: true

# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"

# Assetic Configuration
assetic:
debug: "%kernel.debug%"
use_controller: false
bundles: [ ]
#java: /usr/bin/java
filters:
cssrewrite: ~
#closure:
# jar: "%kernel.root_dir%/Resources/java/compiler.jar"
#yui_css:
# jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"

# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
mssqlDB:
driver_class: Realestate\MssqlBundle\Driver\PDODblib\Driver
host: %db.other.host%
dbname: %db.other.db_name%
user: %db.other.user%
password: %db.other.password%
# if using pdo_sqlite as your database driver, add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# path: "%database_path%"

orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
CEBundle: ~
mssqlDB:
connection: mssqlDB
mappings:
CEBundle: ~

auto_generate_proxy_classes: "%kernel.debug%"
#auto_mapping: true


# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }

#sylius configuration
sylius_cart:
resolver: ce.cart_item_resolver
classes: ~
provider: sylius.cart_provider.default
storage: sylius.storage.session

sylius_order:
driver: doctrine/orm
classes:
order_item:
model: CE\CEBundle\Entity\CartItem

sylius_sequence:
driver: doctrine/orm

#mopa configuration for forms and bootstrap
mopa_bootstrap:
form:
checkbox_label: 'widget'

值得注意的是,如果我查看 Chrome 开发人员工具中的“资源”选项卡,我可以看到当我将商品添加到购物车时 SYLIUS_SESSION 确实已设置。

我已尝试将存储更改为 cookie 而不是 session 并倾倒文档,但我不知道如何解决此问题。我猜它可能是个小东西,我只是不知道它是什么。

提前感谢您的帮助。

最佳答案

在您的 config.yml 文件中,您正在使用 sylius.cart_provider.default,这意味着您需要先获取当前的 Sylius Cart Provider,然后再向其添加商品。

这意味着在您的 Controller 中,您需要像这样检索 cart_provider 服务:

// gets the current cart
$cart = $this->get('sylius.cart_provider')->getCart();

之后,您可以创建一个 $item,就像您在调用 ItemResolver::resolve() 方法时所做的那样,并且该项目可以添加到大车。

您可能还需要将购物车的调度程序事件 ITEM_ADD_INITIALIZECART_CHANGE 等添加到您的代码中,以使商品实际出现在购物车中。

查看 CartItemEvent 类。如果您需要这方面的帮助,请告诉我。

关于php - 添加项目后 Sylius 不记得购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28183158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com