laravelshoppingcart icon indicating copy to clipboard operation
laravelshoppingcart copied to clipboard

How to add session $user->id information in Cart Facade

Open ankitit2015 opened this issue 5 years ago • 2 comments

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

ankitit2015 avatar Jun 20 '20 11:06 ankitit2015

Please help here @Turaylon @it-can @pvdptje @Beaudinn @darryldecode

ankitit2015 avatar Jun 20 '20 11:06 ankitit2015

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),
        ],
    ]);
}

}`

pvdptje avatar Jun 30 '20 08:06 pvdptje