Ich stecke auf die Logik zu sagen, dies einmal zu tun. Ich muss einen Einkaufswagen für den Benutzer erstellen und dann den neu erstellten Warenkorb verwenden, um die Produkte, die der Benutzer ausgewählt hat, zu seinem Warenkorb hinzuzufügen.Wie man einmal etwas macht
Ich verwende keine SESSION-Variablen, ich weiß, dass das eine Lösung wäre.
Grundsätzlich versuche ich, dies zu sagen:
IF a User HAS a CART CREATED FOR THEM
then
PERSIST their SELECTED PRODUCTS to the DB in THEIR CART
Jede Hilfe ist willkommen, danke an alle.
/**
* Creates the option to 'add product to cart'.
*
* @Route("/{id}/addToCart", name="product_addToCart")
* @Method("GET")
* @Template()
*/
public function addToCartAction(Request $request, $id) {
$em = $this->getDoctrine()->getManager();
$product = $em->getRepository('ShopBundle:Product')->find($id);
$product->getId();
$product->getName();
$product->getPrice();
// --------------------- assign added products to userCart id ------------------------ //
$cart = new UserCart();
$quantity = new Quantity();
// Set Time Product was Added
$cart->setTimestamp(new \DateTime());
// Set Quantity Purchased
$quantity->setQuantity(4);
// Set Submitted
$cart->setSubmitted(false);
if ($this->checkUserLogin()) {
$this->addFlash('notice', 'Login to create a cart');
} else {
$cart->setUser($this->getUser()); // Sets the User ONCE
$cart->addQuantity($quantity); // Add Quantity ONCE
$quantity->setUserCart($cart); // Create a UserCart ONCE
$this->addFlash('notice', 'The product: '.$product->getName().' has been added to the cart!');
$quantity->setProduct($product); // Sets the Product to Quantity Association ONCE
$em->persist($product);
$em->persist($cart);
$em->persist($quantity);
$em->flush();
}
return $this->redirectToRoute('product');
}