Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjaap committed Nov 9, 2022
1 parent aeffac6 commit 96261bf
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 1 deletion.
50 changes: 50 additions & 0 deletions Plugin/PickRandomCheckout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Elgentos\HyvaCheckoutABTest\Plugin;

use Exception;
use Hyva\CheckoutCore\Model\CheckoutInformation\Luma;
use Hyva\CheckoutCore\Model\Config;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\App\State;

class PickRandomCheckout
{
public function __construct(private CheckoutSession $checkoutSession, private State $appState) {

}

/**
* @param Config $subject
* @param string $result
*
* @return string
* @throws Exception
*/
public function afterGetActiveCheckoutNamespace(
Config $subject,
string $result
): string {
// Retrieve the active checkout from the session, if present
$activeCheckoutNamespace = $this->checkoutSession->getData('active_checkout_namespace');
if ($activeCheckoutNamespace) {
return $activeCheckoutNamespace;
}

// 50% of the time, use the Luma checkout.
// Otherwise, default to the Hyva Checkout config
// Only do the AB test in production
if (
random_int(0, 1) &&
$this->appState->getMode() !== State::MODE_DEVELOPER
) {
$result = Luma::NAMESPACE;
}

// Save the randomly chosen checkout in the checkout session to make sure
// this session always has the same checkout
$this->checkoutSession->setData('active_checkout_namespace', $result);
$this->checkoutSession->getQuote()->setData('active_checkout_namespace', $result)->save();
return $result;
}
}
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
# magento2-hyva-checkout-ab-test
# elgentos/magento2-hyva-checkout-ab-test

This extension allows you to set up an A/B test with different available Hyvä and the Luma fallback checkout.

It will randomly do a 50% split between the configured Hyva Checkout and the Luma fallback checkout.

## Installation

```bash
composer require elgentos/magento2-hyva-checkout-ab-test
bin/magento set:up
```

## Configuration

None, as of now.

## Reports

You can check the progress of the A/B test by running this query;

```sql
SET SQL_MODE='';
SELECT quote.active_checkout_namespace as checkout, COUNT(quote.entity_id) as quotes, COUNT(sales_order.quote_id) as orders, (COUNT(sales_order.quote_id) / COUNT(quote.entity_id)*100) as conversion_percentage
FROM quote
LEFT JOIN sales_order ON quote.entity_id = sales_order.quote_id AND sales_order.state IN ('completed', 'processing')
WHERE quote.active_checkout_namespace IS NOT NULL
GROUP BY quote.active_checkout_namespace;
```

If you want to see the results in the Magento admin, you can install [degdigital/magento2-customreports](https://github.com/degdigital/magento2-customreports) and add the above query. Be sure to leave out the SQL_MODE part (Magento does this for you) and leave no trailing/leading white lines.
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "elgentos/magento2-hyva-checkout-ab-test",
"version": "1.0.0",
"description": "Set up an A/B test with different available Hyvä and Luma checkouts",
"type": "magento2-module",
"require": {
"php": "^8.1",
"magento/framework": "*",
"hyva-themes/checkout-core": "*"
},
"suggest": {
"degdigital/magento2-customreports": "Useful to check A/B test results in the Magento admin"
},
"license": [
"MIT"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Elgentos\\HyvaHyvaCheckoutABTest\\": ""
}
}
}
6 changes: 6 additions & 0 deletions etc/db_schema.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" ?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="quote">
<column name="active_checkout_namespace" nullable="true" xsi:type="varchar" comment="Active Checkout Namespace" length="255"/>
</table>
</schema>
8 changes: 8 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

<type name="Hyva\CheckoutCore\Model\Config">
<plugin name="PickRandomCheckout"
type="Elgentos\HyvaCheckoutABTest\Plugin\PickRandomCheckout"/>
</type>
</config>
7 changes: 7 additions & 0 deletions etc/frontend/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Hyva\CheckoutCore\Plugin\Magento\Checkout\Controller\Index\Index">
<plugin name="PickRandomCheckout"
type="Elgentos\HyvaCheckoutABTest\Plugin\PickRandomCheckout"/>
</type>
</config>
8 changes: 8 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Elgentos_HyvaCheckoutABTest">
<sequence>
<module name="Hyva_CheckoutCore"/>
</sequence>
</module>
</config>
9 changes: 9 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Elgentos_HyvaCheckoutABTest',
__DIR__
);

0 comments on commit 96261bf

Please sign in to comment.