laravelshoppingcart
laravelshoppingcart copied to clipboard
How to add session $user->id information in Cart Facade
Hi,
I have created a Card Facade & Cart class extending DarryldecodeCart like below:
use Illuminate\Support\Facades\Facade;
class Cart extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return \Modules\Cart\Cart::class;
}
}
class Cart extends DarryldecodeCart
{
/**
* Collection of all cart item.
*
* @var \Illuminate\Support\Collection
*/
private $items;
/**
* Holds the cart shipping method.
*
* @var \Modules\Cart\CartShippingMethod
*/
private $shippingMethod;
/**
* Holds the cart coupon.
*
* @var \Modules\Cart\CartCoupon
*/
private $coupon;
/**
* Collection of cart taxes.
*
* @var \Illuminate\Support\Collection
*/
private $taxes;
/**
* Get the current instance.
*
* @return $this
*/
public function instance()
{
return $this;
}
/**
* Clear the cart.
*
* @return void
*/
public function clear()
{
parent::clear();
$this->clearCartConditions();
}
/**
* Store a new item to the cart.
*
* @param int $productId
* @param int $qty
* @param array $options
* @return $this
*/
public function store($productId, $qty, $options = [])
{
$product = Product::with('files', 'categories', 'taxClass')->findOrFail($productId);
$options = array_filter($options);
return $this->add([
'id' => "product_id.{$product->id}:options." . serialize($options),
'name' => $product->name,
'price' => $product->selling_price->amount(),
'quantity' => $qty,
'attributes' => [
'product' => $product,
'options' => $this->options($product, $options),
],
]);
}
}
Where can i add session information containing user id here
Please help here @Turaylon @it-can @pvdptje @Beaudinn @darryldecode
Not sure what you want to achieve, but i'd suggest something like this:
`class Cart extends DarryldecodeCart {
/**
* Overwrite parent constructor
*/
public function __construct($session, $events, $instanceName, $session_key, $config)
{
parent::__construct($session, $events, $instanceName, $session_key, $config);
$this->user = auth()->user();
}
/**
* Collection of all cart item.
*
* @var \Illuminate\Support\Collection
*/
private $items;
/**
* Holds the cart shipping method.
*
* @var \Modules\Cart\CartShippingMethod
*/
private $shippingMethod;
/**
* Holds the cart coupon.
*
* @var \Modules\Cart\CartCoupon
*/
private $coupon;
/**
* Collection of cart taxes.
*
* @var \Illuminate\Support\Collection
*/
private $taxes;
/**
* The User
*/
private $user;
/**
* Return the user
*/
public function user()
{
return $this->user;
}
/**
* Get the current instance.
*
* @return $this
*/
public function instance()
{
return $this;
}
/**
* Clear the cart.
*
* @return void
*/
public function clear()
{
parent::clear();
$this->clearCartConditions();
}
/**
* Store a new item to the cart.
*
* @param int $productId
* @param int $qty
* @param array $options
* @return $this
*/
public function store($productId, $qty, $options = [])
{
$product = Product::with('files', 'categories', 'taxClass')->findOrFail($productId);
$options = array_filter($options);
return $this->add([
'id' => "product_id.{$product->id}:options." . serialize($options),
'name' => $product->name,
'price' => $product->selling_price->amount(),
'quantity' => $qty,
'attributes' => [
'product' => $product,
'options' => $this->options($product, $options),
],
]);
}
}`