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
|
|||
34 | $cart->add($product, $quantity); |
||
35 | } |
||
36 | } |
||
37 | } |
||
38 | Controller::curr()->redirect(CheckoutPage::find_link()); |
||
39 | } |
||
40 | } |
||
41 |
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.