Source of file EcommerceTaskAddDefaultProducts.php
Size: 3,181 Bytes - Last Modified: 2021-12-23T10:39:35+00:00
/var/www/docs.ssmods.com/process/src/src/Tasks/EcommerceTaskAddDefaultProducts.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 | <?php namespace Sunnysideup\Ecommerce\Tasks; use SilverStripe\Dev\BuildTask; use SilverStripe\ORM\DB; use Sunnysideup\Ecommerce\Pages\Product; use Sunnysideup\Ecommerce\Pages\ProductGroup; /** * @description: see EcommerceTaskAddDefaultProducts::$description * * @authors: Nicolaas [at] Sunny Side Up .co.nz * @package: ecommerce * @sub-package: tasks */ class EcommerceTaskAddDefaultProducts extends BuildTask { protected $title = 'Add default Products'; protected $description = 'Adds two default Products and a Product Group (Category) to your site.'; public function run($request) { if (! Product::get()->exists()) { if (! ProductGroup::get()->exists()) { $productGroup1 = new ProductGroup(); $productGroup1->Title = 'Products'; $productGroup1->Content = " <p>This is the top level products page, it uses the <em>product group</em> page type, and it allows you to show your products checked as 'featured' on it. It also allows you to nest <em>product group</em> pages inside it.</p> <p>For example, you have a product group called 'DVDs', and inside you have more product groups like 'sci-fi', 'horrors' or 'action'.</p> <p>In this example we have setup a main product group (this page), with a nested product group containing 2 example products.</p> "; $productGroup1->URLSegment = 'products'; $productGroup1->writeToStage('Stage'); $productGroup1->publish('Stage', 'Live'); DB::alteration_message("Product group page 'Products' created", 'created'); } else { $productGroup1 = ProductGroup::get()->first(); } $content = "<p>This is a <em>product</em>. It's description goes into the Content field as a standard SilverStripe page would have it's content. This is an ideal place to describe your product.</p>"; $page1 = new Product(); $page1->Title = 'Example product'; $page1->Content = $content . '<p>You may also notice that we have checked it as a featured product and it will be displayed on the main Products page.</p>'; $page1->URLSegment = 'example-product'; $page1->ParentID = $productGroup1->ID; $page1->Price = '15.00'; $page1->FeaturedProduct = true; $page1->writeToStage('Stage'); $page1->publish('Stage', 'Live'); DB::alteration_message("Product page 'Example product' created", 'created'); $page2 = new Product(); $page2->Title = 'Example product 2'; $page2->Content = $content; $page2->URLSegment = 'example-product-2'; $page2->ParentID = $productGroup1->ID; $page2->Price = '25.00'; $page2->writeToStage('Stage'); $page2->publish('Stage', 'Live'); DB::alteration_message("Product page 'Example product 2' created", 'created'); } else { DB::alteration_message('No products created as they already exist.'); } } } |