| Total Complexity | 59 |
| Total Lines | 409 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Commerce often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Commerce, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class Commerce extends Component |
||
| 35 | { |
||
| 36 | // Public Methods |
||
| 37 | // ========================================================================= |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Enqueue analytics information for the completed order |
||
| 41 | * |
||
| 42 | * @param ?Order $order the Product or Variant |
||
| 43 | */ |
||
| 44 | public function triggerOrderCompleteEvent(Order $order = null) |
||
| 56 | ); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Enqueue analytics information for a new checkout flow |
||
| 62 | * |
||
| 63 | * @param ?Order $order |
||
| 64 | */ |
||
| 65 | public function triggerBeginCheckoutEvent(Order $order = null) |
||
| 66 | { |
||
| 67 | if ($order) { |
||
| 68 | $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->BeginCheckoutEvent(); |
||
| 69 | // First, include the transaction data |
||
| 70 | $event->setCurrency($order->getPaymentCurrency()) |
||
| 71 | ->setValue($order->getTotalPrice()); |
||
| 72 | |||
| 73 | // Add each line item in the cart |
||
| 74 | $index = 1; |
||
| 75 | foreach ($order->lineItems as $lineItem) { |
||
| 76 | $this->addProductDataFromLineItem($event, $lineItem, $index); |
||
| 77 | $index++; |
||
| 78 | } |
||
| 79 | |||
| 80 | InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
||
| 81 | |||
| 82 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 83 | 'Adding `Commerce - Begin Checkout event``', |
||
| 84 | [], |
||
| 85 | __METHOD__ |
||
| 86 | ); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Send analytics information for the item added to the cart |
||
| 92 | * |
||
| 93 | * @param LineItem $lineItem the line item that was added |
||
| 94 | */ |
||
| 95 | public function triggerAddToCartEvent(LineItem $lineItem): void |
||
| 96 | { |
||
| 97 | $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->AddToCartEvent(); |
||
| 98 | $this->addProductDataFromLineItem($event, $lineItem); |
||
| 99 | InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
||
| 100 | |||
| 101 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 102 | 'Adding `Commerce - Add to Cart event`: `{title}` => `{quantity}`', |
||
| 103 | ['title' => $lineItem->purchasable->title ?? $lineItem->getDescription(), 'quantity' => $lineItem->qty], |
||
| 104 | __METHOD__ |
||
| 105 | ); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Send analytics information for the item removed from the cart |
||
| 110 | * |
||
| 111 | * @param LineItem $lineItem |
||
| 112 | */ |
||
| 113 | public function triggerRemoveFromCartEvent(LineItem $lineItem) |
||
| 114 | { |
||
| 115 | $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->RemoveFromCartEvent(); |
||
| 116 | $this->addProductDataFromLineItem($event, $lineItem); |
||
| 117 | InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
||
| 118 | |||
| 119 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 120 | 'Adding `Commerce - Remove from Cart event`: `{title}` => `{quantity}`', |
||
| 121 | ['title' => $lineItem->purchasable->title ?? $lineItem->getDescription(), 'quantity' => $lineItem->qty], |
||
| 122 | __METHOD__ |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * Add a Craft Commerce OrderModel to a Purchase Event |
||
| 129 | * |
||
| 130 | * @param PurchaseEvent $event The PurchaseEvent |
||
| 131 | * @param Order $order |
||
| 132 | */ |
||
| 133 | protected function addCommerceOrderToEvent(PurchaseEvent $event, Order $order) |
||
| 134 | { |
||
| 135 | // First, include the transaction data |
||
| 136 | $event->setCurrency($order->getPaymentCurrency()) |
||
| 137 | ->setTransactionId($order->reference) |
||
| 138 | ->setValue($order->getTotalPrice()) |
||
| 139 | ->setTax($order->getTotalTax()) |
||
| 140 | ->setShipping($order->getTotalShippingCost()); |
||
| 141 | |||
| 142 | // Coupon code |
||
| 143 | if ($order->couponCode) { |
||
| 144 | $event->setCoupon($order->couponCode); |
||
| 145 | } |
||
| 146 | |||
| 147 | // Add each line item in the transaction |
||
| 148 | // Two cases - variant and non variant products |
||
| 149 | $index = 1; |
||
| 150 | |||
| 151 | foreach ($order->lineItems as $lineItem) { |
||
| 152 | $this->addProductDataFromLineItem($event, $lineItem, $index); |
||
| 153 | $index++; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Add a Craft Commerce LineItem to an Analytics object |
||
| 159 | * |
||
| 160 | * @param ItemBaseEvent $event |
||
| 161 | * @param LineItem $lineItem |
||
| 162 | * @param int $index |
||
| 163 | * @param string $listName |
||
| 164 | * |
||
| 165 | * @return string the title of the product |
||
| 166 | * @throws \yii\base\InvalidConfigException |
||
| 167 | */ |
||
| 168 | protected function addProductDataFromLineItem(ItemBaseEvent $event, LineItem $lineItem, int $index = 0, string $listName = ''): string |
||
| 169 | { |
||
| 170 | $eventItem = $this->getNewItemParameter(); |
||
| 171 | |||
| 172 | $product = null; |
||
| 173 | $purchasable = $lineItem->purchasable; |
||
| 174 | |||
| 175 | $eventItem->setItemName($purchasable->title ?? $lineItem->getDescription()); |
||
| 176 | $eventItem->setItemId($purchasable->getSku() ?? $lineItem->getSku()); |
||
| 177 | $eventItem->setPrice($lineItem->salePrice); |
||
| 178 | $eventItem->setQuantity($lineItem->qty); |
||
| 179 | |||
| 180 | // Handle this purchasable being a Variant |
||
| 181 | if (is_a($purchasable, Variant::class)) { |
||
| 182 | /** @var Variant $purchasable */ |
||
| 183 | $product = $purchasable->getProduct(); |
||
| 184 | $variant = $purchasable; |
||
| 185 | // Product with variants |
||
| 186 | $eventItem->setItemName($product->title); |
||
| 187 | $eventItem->setItemVariant($variant->title); |
||
| 188 | $eventItem->setItemCategory($product->getType()); |
||
| 189 | } |
||
| 190 | |||
| 191 | // Handle this purchasable being a Product |
||
| 192 | if (is_a($purchasable, Product::class)) { |
||
| 193 | /** @var Product $purchasable */ |
||
| 194 | $product = $purchasable; |
||
| 195 | $eventItem->setItemName($product->title); |
||
| 196 | $eventItem->setItemVariant($product->title); |
||
| 197 | $eventItem->setItemCategory($product->getType()); |
||
| 198 | } |
||
| 199 | |||
| 200 | // Handle product lists |
||
| 201 | if ($index) { |
||
| 202 | $eventItem->setIndex($index); |
||
| 203 | } |
||
| 204 | |||
| 205 | if ($listName) { |
||
| 206 | $eventItem->setItemListName($listName); |
||
| 207 | } |
||
| 208 | |||
| 209 | // Add in any custom categories/brands that might be set |
||
| 210 | if (InstantAnalytics::$settings && $product) { |
||
| 211 | if (isset(InstantAnalytics::$settings['productCategoryField']) |
||
| 212 | && !empty(InstantAnalytics::$settings['productCategoryField'])) { |
||
| 213 | $category = $this->pullDataFromField( |
||
| 214 | $product, |
||
| 215 | InstantAnalytics::$settings['productCategoryField'] |
||
| 216 | ); |
||
| 217 | $eventItem->setItemCategory($category); |
||
| 218 | } |
||
| 219 | if (isset(InstantAnalytics::$settings['productBrandField']) |
||
| 220 | && !empty(InstantAnalytics::$settings['productBrandField'])) { |
||
| 221 | $brand = $this->pullDataFromField( |
||
| 222 | $product, |
||
| 223 | InstantAnalytics::$settings['productBrandField'] |
||
| 224 | ); |
||
| 225 | |||
| 226 | $eventItem->setItemBrand($brand); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | //Add each product to the hit to be sent |
||
| 231 | $event->addItem($eventItem); |
||
| 232 | |||
| 233 | return $eventItem->getItemName(); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Add a product impression from a Craft Commerce Product or Variant |
||
| 238 | * |
||
| 239 | * @param Product|Variant $productVariant the Product or Variant |
||
| 240 | * @throws \yii\base\InvalidConfigException |
||
| 241 | */ |
||
| 242 | public function addCommerceProductImpression(Variant|Product $productVariant): void |
||
| 243 | { |
||
| 244 | if ($productVariant) { |
||
| 245 | $event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->ViewItemEvent(); |
||
| 246 | $this->addProductDataFromProductOrVariant($event, $productVariant); |
||
| 247 | |||
| 248 | InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
||
| 249 | |||
| 250 | $sku = $productVariant instanceof Product ? $productVariant->getDefaultVariant()->sku : $productVariant->sku; |
||
| 251 | $name = $productVariant instanceof Product ? $productVariant->getName() : $productVariant->getProduct()->getName(); |
||
| 252 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 253 | 'Adding view item event for `{sku}` - `{name}` - `{name}` - `{index}`', |
||
| 254 | ['sku' => $sku, 'name' => $name], |
||
| 255 | __METHOD__ |
||
| 256 | ); |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Add a product list impression from a Craft Commerce Product or Variant list |
||
| 262 | * |
||
| 263 | * @param Product[]|Variant[] $products |
||
| 264 | * @param string $listName |
||
| 265 | */ |
||
| 266 | public function addCommerceProductListImpression(array $products, string $listName = 'default'): void |
||
| 280 | ); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Extract product data from a Craft Commerce Product or Variant |
||
| 286 | * |
||
| 287 | * @param Product|Variant|null $productVariant the Product or Variant |
||
| 288 | * |
||
| 289 | * @throws \yii\base\InvalidConfigException |
||
| 290 | */ |
||
| 291 | protected function addProductDataFromProductOrVariant(ItemBaseEvent $event, $productVariant = null, $index = null, $listName = ''): void |
||
| 292 | { |
||
| 293 | if ($productVariant === null) { |
||
| 294 | return; |
||
| 295 | } |
||
| 296 | |||
| 297 | $eventItem = $this->getNewItemParameter(); |
||
| 298 | |||
| 299 | $isVariant = $productVariant instanceof Variant; |
||
| 300 | $variant = $isVariant ? $productVariant : $productVariant->getDefaultVariant(); |
||
| 301 | |||
| 302 | if (!$variant) { |
||
| 303 | return; |
||
| 304 | } |
||
| 305 | |||
| 306 | $eventItem->setItemId($variant->sku); |
||
| 307 | $eventItem->setItemName($variant->title); |
||
| 308 | $eventItem->setPrice(number_format($variant->price, 2, '.', '')); |
||
| 309 | |||
| 310 | $category = ($isVariant ? $variant->getProduct() : $productVariant)->getType()['name']; |
||
| 311 | |||
| 312 | if (InstantAnalytics::$settings) { |
||
| 313 | if (isset(InstantAnalytics::$settings['productCategoryField']) |
||
| 314 | && !empty(InstantAnalytics::$settings['productCategoryField'])) { |
||
| 315 | $category = $this->pullDataFromField( |
||
| 316 | $productVariant, |
||
| 317 | InstantAnalytics::$settings['productCategoryField'] |
||
| 318 | ); |
||
| 319 | if (empty($productData['category']) && $isVariant) { |
||
| 320 | $category = $this->pullDataFromField( |
||
| 321 | $productVariant->product, |
||
| 322 | InstantAnalytics::$settings['productCategoryField'] |
||
| 323 | ); |
||
| 324 | } |
||
| 325 | } |
||
| 326 | $eventItem->setItemCategory($category); |
||
| 327 | |||
| 328 | if (isset(InstantAnalytics::$settings['productBrandField']) |
||
| 329 | && !empty(InstantAnalytics::$settings['productBrandField'])) { |
||
| 330 | $brand = $this->pullDataFromField( |
||
| 331 | $productVariant, |
||
| 332 | InstantAnalytics::$settings['productBrandField'], |
||
| 333 | true |
||
| 334 | ); |
||
| 335 | |||
| 336 | if (empty($productData['brand']) && $isVariant) { |
||
| 337 | $brand = $this->pullDataFromField( |
||
| 338 | $productVariant, |
||
| 339 | InstantAnalytics::$settings['productBrandField'], |
||
| 340 | true |
||
| 341 | ); |
||
| 342 | } |
||
| 343 | $eventItem->setItemBrand($brand); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | if ($index !== null) { |
||
| 348 | $eventItem->setIndex($index); |
||
| 349 | } |
||
| 350 | |||
| 351 | if (!empty($listName)) { |
||
| 352 | $eventItem->setItemListName($listName); |
||
| 353 | } |
||
| 354 | |||
| 355 | // Add item info to the event |
||
| 356 | $event->addItem($eventItem); |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param Product|Variant|null $productVariant |
||
| 361 | * @param string $fieldHandle |
||
| 362 | * @param bool $isBrand |
||
| 363 | * |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | protected function pullDataFromField($productVariant, $fieldHandle, $isBrand = false): string |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param bool $isBrand |
||
| 400 | * @param array $elements |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | protected function getDataFromElements(bool $isBrand, array $elements): string |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Create an item parameter and set affiliation on it, if any exists. |
||
| 435 | * |
||
| 436 | * @return ItemParameter |
||
| 437 | */ |
||
| 438 | protected function getNewItemParameter(): ItemParameter |
||
| 443 | } |
||
| 444 | } |
||
| 445 |