یلدا ادامه داره... ❤️ ۴۰ درصد تخفیف همه دورهها
استفاده از تخفیفهامن میخواستم session های سبد خریدم رو به ردیس وصل کنم اما اصلا دیتا داخل سشن نمیمونه
این کنترلر سبد خریدمه
<?php
namespace App\\Http\\Controllers;
use App\\Facades\\Alert;
use App\\Models\\Additive;
use App\\Models\\Product;
use App\\Services\\Global\\Cart;
use Illuminate\\Http\\Request;
use Illuminate\\Support\\Facades\\Session;
use Illuminate\\Support\\Str;
use function PHPUnit\\Framework\\isNull;
class CartController extends Controller
{
/\*\*
- construct
-
- @param Cart $cart Cart service constructor
-
- @return void
\*/
public function construct(
protected Cart $cart,
) {
}
public function index()
{
$this->cart->addToCart(productId: 1);
$items = $this->cart->convertIdToObject();
// $items = $cart->convertIdToObject();
dd($items);
return view('cart', compact('items'));
}
public function addToCart(Request $request)
{
$product = Product::find($request->product);
$additives = $request->additives;
if ($product->status == 0) {
Alert::danger(('cart.ERRORADD'));
return redirect()->back();
}
if (!$this->checkIsActive($additives)) {
Alert::danger(__('cart.ERROR_ADD'));
return redirect()->back();
}
$product_qy = $request->product_qy;
$this->cart->addToCart($product->id, $product_qy, $additives);
Alert::success(__('cart.SUCCESS_ADD'));
return redirect()->back();
}
public function updateCart(string $hash, int $qty, array $additives = null)
{
if (!$this->checkIsActive($additives)) {
Alert::danger(('cart.ERRORUPDATED'));
return redirect()->back();
}
$this->cart->updateCart($hash, $qty, $additives);
Alert::success(__('cart.SUCCESS_UPDATED'));
return redirect()->back();
}
// public function destroy()
// {
// $this->cart->destroyCart();
// Alert::success(('cart.SUCCESSDESTROY'));
// return redirect()->back();
// }
protected function checkIsActive($additives = null): bool
{
if (!isnull($additives)) {
foreach ($additives as $key => $additive) {
$activeAddiitve = Additive::find($additive\['id'\]);
if (!$activeAddiitve->active) {
return false;
}
}
return true;
}
}
}
اینم کلاس Cart هست که ازش استفاده میکنم:
<?php
namespace App\\Services\\Global;
use App\\Models\\Additive;
use App\\Models\\Product;
use Illuminate\\Support\\Facades\\Redis;
use Illuminate\\Support\\Facades\\Session;
class Cart
{
private $cartData;
/**
* Constructor method for the CartManager class.
* This method initializes the CartManager object and checks if a cart exists in the session.
* If the cart does not exist, it creates an empty cart and stores it in the session.
* The cart data is then retrieved and stored in the $cartData property.
*
* @return void
*/
public function __construct()
{
// $this->cartData = Session::get('cart', ['products' => []]);
// $this->cartData = (Session::put('cart', ['products' => ['id' => 1, 'qty' => 6]]));
}
/**
* method to conver id to object datas
*
* @return array of cart datas
*/
public function convertIdToObject(): array|null
{
$cartData = Session::get('cart', ['products' => []]);
// $products = $this->cartData['products'];
$products = $cartData['products'];
$items = [];
if (!empty($products)) {
$productObject = [];
foreach ($products as $key => $product) {
$mediaItems = Product::find($product['id'])->getFirstMedia('images');
$productObject[$key] = Product::find($product['id'])->first()->toArray();
$productObject[$key]['qty'] = $product['qty'];
if (!empty($mediaItems)) {
$productObject[$key]['publicUrl'] = $mediaItems->getUrl();
$productObject[$key]['publicFullUrl'] = $mediaItems->getFullUrl();
}
$additives = [];
foreach ($product['additives'] as $additive) {
$additiveData = Additive::findOrFail($additive['id'])->toArray();
$additiveData['qty'] = $additive['qty'];
$additives[] = $additiveData;
}
$productObject[$key]['additives'] = $additives;
$items['products'] = $productObject;
}
}
return $items;
}
/**
* method to update cart quantity
*
* @param int $productId id of the product
* @param int $qty quantity of the product
* @param array $additives list of additives
*
* @return void
*
*/
public function addToCart(int $productId, int $qty = 1, array $additives = []): void
{
$newAdditives = [];
foreach ($additives as $addKey => $additive) {
$newAdditives[$addKey] = [
'id' => $additive['id'],
'qty' => $additive['qty']
];
}
$cartItem = [
'id' => $productId,
'qty' => $qty,
'additives' => $newAdditives
];
$jsonString = json_encode($cartItem);
$hashedData = md5($jsonString);
// $this->cartData['products'][$hashedData] = $cartItem;
$cartData['products'][$hashedData] = $cartItem;
Session::put('cart', $cartData);
// Redis::connection('session')->set('cart user: ', json_encode($this->cartData));
}
/**
* method to update cart quantity
*
* @param string $hash hashed cart items data
* @param int $qty quantity of the product
* @param array $additives list of additives
*
* @return void
*
*/
public function updateCart(string $hash, int $qty, array $additives = null): void
{
$cartData = Session::get('cart', ['products' => []]);
foreach ($cartData['products'] as $key => $value) {
if ($key == $hash) {
if (!is_null($additives)) {
$additivesUpdated = [];
$value['additives'] = $additives;
$productObject['additives'] = $additives;
foreach ($additives as $addKey => $additive) {
$additiveData['id'] = $additive['id'];
$additiveData['qty'] = $additive['qty'];
$additivesUpdated[] = $additiveData;
}
$value['additives'] = $additivesUpdated;
}
$value['qty'] = $qty;
$jsonString = json_encode($value);
$newHash = md5($jsonString);
// unset($this->cartData['products'][$key]);
unset($cartData['products'][$key]);
// $this->cartData['products'][$newHash] = $value;
$cartData['products'][$newHash] = $value;
}
Session::put('cart', $cartData);
}
}
/**
* method to return cart data
*
* @return array cart data
*
*/
public function getCartData(): array
{
$cartData = Session::get('cart', ['products' => []]);
return $cartData;
}
/**
* method to destroy cart
*
* @return void
*/
public function destroyCart(): void
{
$cartData = Session::get('cart', ['products' => []]);
$cartData = ['products' => []];
Session::put('cart', $cartData);
}
}
اینم تنظیمات داکر من هست:
version: '3.8'
services:
bus:
envfile: ./.env
image: bitnami/redis:latest
restart: unless-stopped
ports:
- '6400:6379'
volumes:
- ./data/redis:/bitnami/redis/data
- ./data/redis:/redis.config # user: 0:0
# command: bash -c "sysctl vm.overcommit_memory=1";
environment:
ALLOW_EMPTY_PASSWORD: ${REDIS_EMPTY_PASSWORD}
REDIS_DISABLE_COMMANDS: ${REDIS_DISABLE_COMMANDS}
REDIS_PASSWORD: ${REDIS_PASSWORD}
REDIS_AOF_ENABLED: ${REDIS_AOF_ENABLED}
آیا مایل به ارسال نوتیفیکیشن و اخبار از طرف راکت هستید ؟