Source of file EcommerceTaskDebugCart.php
Size: 2,711 Bytes - Last Modified: 2021-12-23T10:39:35+00:00
/var/www/docs.ssmods.com/process/src/src/Tasks/EcommerceTaskDebugCart.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 | <?php namespace Sunnysideup\Ecommerce\Tasks; use SilverStripe\Core\Config\Config; use SilverStripe\Dev\BuildTask; use SilverStripe\ORM\FieldType\DBBoolean; use Sunnysideup\Ecommerce\Api\ShoppingCart; class EcommerceTaskDebugCart extends BuildTask { protected $title = 'Debug your cart'; protected $description = 'Check all the values in your cart to find any potential errors.'; public function run($request) { $order = ShoppingCart::current_order(); echo self::debug_object($order); } public static function debug_object($obj) { $html = ' <h2>' . $obj->ClassName . '</h2><ul>'; $fields = Config::inst()->get($obj->ClassName, 'db'); //db if (count($fields)) { foreach ($fields as $key => $type) { $value = self::cleanup_value($type, $obj->{$key}); $html .= "<li><b>{$key} ({$type}):</b> " . $value . '</li>'; } } //casted variables $fields = Config::inst()->get($obj->ClassName, 'casting', Config::UNINHERITED); if (count($fields)) { foreach ($fields as $key => $type) { $method = $key; if ($obj->hasMethod($method)) { $value = $obj->{$method}(); } else { $method = 'get' . $key; $value = $obj->hasMethod($method) ? $obj->{$method}() : $obj->{$key}; } $value = self::cleanup_value($type, $value); $html .= "<li><b>{$key} ({$type}):</b> " . $value . '</li>'; } } //has_one $fields = Config::inst()->get($obj->ClassName, 'has_one', Config::UNINHERITED); if (count($fields)) { foreach ($fields as $key => $type) { $value = ''; $field = $key . 'ID'; $object = $obj->{$key}(); if ($object) { if ($object && $object->exists()) { $value = ', ' . $object->getTitle(); } } $html .= "<li><b>{$key} ({$type}):</b> " . $obj->{$field} . $value . ' </li>'; } } //to do: has_many and many_many return $html . '</ul>'; } private static function cleanup_value($type, $value) { switch ($type) { case 'HTMLText': $value = substr(strip_tags($value), 0, 100); break; case DBBoolean::class: $value = $value ? 'YES' : 'NO'; break; default: break; } return $value; } } |