| Conditions | 12 |
| Paths | 10 |
| Total Lines | 59 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 19 | public function onAfterInit() |
||
| 20 | { |
||
| 21 | $owner = $this->owner; |
||
| 22 | if ($owner->EnableGoogleAnalytics && $owner->currentOrder && (Director::isLive() || isset($_GET['testanalytics']))) { |
||
| 23 | $var = EcommerceConfig::get(CheckoutPageDataExtension::class, 'google_analytics_variable'); |
||
| 24 | if ($var) { |
||
| 25 | $currencyUsedObject = $owner->currentOrder->CurrencyUsed(); |
||
| 26 | if ($currencyUsedObject) { |
||
| 27 | $currencyUsedString = $currencyUsedObject->Code; |
||
| 28 | } |
||
| 29 | if (empty($currencyUsedString)) { |
||
| 30 | $currencyUsedString = EcommerceCurrency::default_currency_code(); |
||
| 31 | } |
||
| 32 | $orderItems = $owner->currentOrder->OrderItems(); |
||
| 33 | $items = ''; |
||
| 34 | if (Config::inst()->get(CheckoutPageExtensionController::class, 'include_product_items')) { |
||
| 35 | foreach ($orderItems as $orderItem) { |
||
| 36 | $product = Product::get_by_id($orderItem->BuyableID); |
||
| 37 | $sku = $product->InternalItemID ?: $product->ID; |
||
| 38 | $topParent = $product->TopParentGroup(); |
||
| 39 | $catetory = 'Unknown'; |
||
|
|
|||
| 40 | if($topParent) { |
||
| 41 | |||
| 42 | } |
||
| 43 | $orderItemName = preg_replace("#\r|\n#", '', (string) $orderItem->TableTitle()); |
||
| 44 | $category = preg_replace("#\r|\n#", '', (string) $category); |
||
| 45 | $items .= |
||
| 46 | 'ga( |
||
| 47 | \'ecommerce:addItem\', |
||
| 48 | { |
||
| 49 | \'id\': \'' . $owner->currentOrder->ID . '\', |
||
| 50 | \'name\': \'' . $orderItemName . '\', |
||
| 51 | \'sku\': \'' . $sku . '\', |
||
| 52 | \'category\': \'' . $category . '\', |
||
| 53 | \'price\': \'' . $orderItem->CalculatedTotal . '\', |
||
| 54 | \'quantity\': \'' . $orderItem->Quantity . '\', |
||
| 55 | } |
||
| 56 | );'; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | $js = ' |
||
| 60 | jQuery("#OrderForm_OrderForm").on( |
||
| 61 | "submit", |
||
| 62 | function(e){ |
||
| 63 | console.log(e); |
||
| 64 | ' . $var . '(\'require\', \'ecommerce\'); |
||
| 65 | ' . $var . '( |
||
| 66 | \'ecommerce:addTransaction\', |
||
| 67 | { |
||
| 68 | \'id\': \'' . $owner->currentOrder->ID . '\', |
||
| 69 | \'revenue\': \'' . $owner->currentOrder->getSubTotal() . '\', |
||
| 70 | \'currency\': \'' . $currencyUsedString . '\' |
||
| 71 | } |
||
| 72 | ); |
||
| 73 | ' . $items . ' |
||
| 74 | ' . $var . '(\'ecommerce:send\'); |
||
| 75 | } |
||
| 76 | );'; |
||
| 77 | Requirements::customScript($js, 'GoogleAnalyticsEcommerce'); |
||
| 78 | } |
||
| 82 |