Completed
Push — master ( 3c661d...2a57f9 )
by Will
26s queued 12s
created

src/Tasks/PopulateCartTask.php (1 issue)

Severity
1
<?php
2
3
namespace SilverShop\Tasks;
4
5
use SilverShop\Cart\ShoppingCart;
6
use SilverShop\Page\CheckoutPage;
7
use SilverShop\Page\Product;
8
use SilverStripe\Control\Controller;
9
use SilverStripe\Dev\BuildTask;
10
use SilverStripe\Security\Member;
11
use SilverStripe\Versioned\Versioned;
12
13
/**
14
 * Add 5 random Live products to cart, with random quantities between 1 and 10.
15
 */
16
class PopulateCartTask extends BuildTask
17
{
18
    protected $title = 'Populate Cart';
19
20
    protected $description = 'Add 5 random Live products or variations to cart, with random quantities between 1 and 10.';
21
22
    public function run($request)
23
    {
24
        $cart = ShoppingCart::singleton();
25
        $count = $request->getVar('count') ? $request->getVar('count') : 5;
26
        if ($products = Versioned::get_by_stage(Product::class, 'Live', '', 'RAND()', '', $count)) {
27
            foreach ($products as $product) {
28
                $variations = $product->Variations();
29
                if ($variations->exists()) {
30
                    $product = $variations->sort('RAND()')->first();
31
                }
32
                $quantity = (int)rand(1, 5);
33
                if ($product->canPurchase(Member::currentUser(), $quantity)) {
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Security\Member::currentUser() has been deprecated: 5.0.0 use Security::getCurrentUser() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

33
                if ($product->canPurchase(/** @scrutinizer ignore-deprecated */ Member::currentUser(), $quantity)) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
34
                    $cart->add($product, $quantity);
35
                }
36
            }
37
        }
38
        Controller::curr()->redirect(CheckoutPage::find_link());
39
    }
40
}
41