Skip to content

Commit

Permalink
Added config fields, added frontend model, not in working state (#1)
Browse files Browse the repository at this point in the history
Added configuration fields
  • Loading branch information
peterjaap authored Nov 10, 2022
1 parent 84a1ee9 commit be2aa49
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 10 deletions.
47 changes: 47 additions & 0 deletions Block/Adminhtml/Form/Field/Checkouts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Elgentos\HyvaCheckoutABTest\Block\Adminhtml\Form\Field;

use Hyva\CheckoutCore\Block\Adminhtml\Element\FieldArray\TypeRenderer;
use Magento\Backend\Block\Template\Context;
use \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
use Magento\Framework\View\Helper\SecureHtmlRenderer;

class Checkouts extends AbstractFieldArray
{
private \Magento\Framework\View\Element\BlockFactory $blockFactory;
private \Hyva\CheckoutCore\Model\Config\Source\Checkout $checkoutSource;

public function __construct(
Context $context,
\Magento\Framework\View\Element\BlockFactory $blockFactory,
\Hyva\CheckoutCore\Model\Config\Source\Checkout $checkoutSource,
array $data = [],
?SecureHtmlRenderer $secureRenderer = null
) {
parent::__construct($context, $data, $secureRenderer);
$this->blockFactory = $blockFactory;
$this->checkoutSource = $checkoutSource;
}

protected function _prepareToRender()
{
$checkoutOptions = array_combine(
array_column($this->checkoutSource->toOptionArray(), 'value'),
array_column($this->checkoutSource->toOptionArray(), 'label')
);
$this->addColumn('checkout', [
'label' => __('Checkout'),
'class' => 'required-entry',
'renderer' => $this->blockFactory->createBlock(TypeRenderer::class)
->setElementType('select')
->setElementOptions($checkoutOptions),
'style' => 'width: 150px'
]);
$this->addColumn('percentage', ['label' => __('Percentage Shown During Test'), 'class' => 'required-entry']);
$this->_addAfter = false;
$this->_addButtonLabel = __('Add Checkout');
}
}
62 changes: 52 additions & 10 deletions Plugin/PickRandomCheckout.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@
use Hyva\CheckoutCore\Model\CheckoutInformation\Luma;
use Hyva\CheckoutCore\Model\Config;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\State;
use Magento\Framework\Serialize\SerializerInterface;

class PickRandomCheckout
{
public function __construct(private CheckoutSession $checkoutSession, private State $appState) {
public const HYVA_CHECKOUT_AB_TEST_ENABLED = 'hyva_themes_checkout/ab_test/enable';
public const HYVA_CHECKOUT_AB_TEST_CHECKOUTS = 'hyva_themes_checkout/ab_test/checkouts';

public function __construct(
private readonly CheckoutSession $checkoutSession,
private readonly State $appState,
private readonly ScopeConfigInterface $config,
private readonly SerializerInterface $serializer
) {

}

Expand All @@ -25,26 +35,58 @@ public function afterGetActiveCheckoutNamespace(
Config $subject,
string $result
): string {
// When the AB test is disabled, use the default configuration
if (!$this->config->getValue(self::HYVA_CHECKOUT_AB_TEST_ENABLED)) {
return $result;
}

try {
$checkouts = $this->serializer->unserialize($this->config->getValue(self::HYVA_CHECKOUT_AB_TEST_CHECKOUTS));
} catch (Exception $e) {
$checkouts = [];
}

// When no checkouts are configured, use the default configuration
if (count($checkouts) === 0) {
return $result;
}

// When in developer mode, use the default configuration
if ($this->appState->getMode() !== State::MODE_DEVELOPER) {
return $result;
}

// 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;
}
// Pick a random result based on percentage given
$result = $this->randomWithProbability($checkouts);

// 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;
}

/**
* @param $checkouts
*
* @return string
*/
private function randomWithProbability($checkouts): string
{
$temp = [];
foreach ($checkouts as $checkout) {
$num = $checkout['percentage'];
while ($num > 0) {
$temp[] = $checkout['checkout'];
$num--;
}
}
return $temp[array_rand($temp)];
}
}
25 changes: 25 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Elgentos B.V.. All rights reserved.
* https://www.elgentos.nl/
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="hyva_themes_checkout">
<group id="ab_test" translate="label" type="text" sortOrder="15" showInDefault="1" showInWebsite="0" showInStore="0">
<label>A/B Test</label>
<field id="enabled" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="checkouts" translate="label" sortOrder="20" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Checkouts</label>
<frontend_model>Elgentos\HyvaCheckoutABTest\Block\Adminhtml\Form\Field\Checkouts</frontend_model>
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
</field>
</group>
</section>
</system>
</config>

0 comments on commit be2aa49

Please sign in to comment.