Skip to content

Add payment method position to the gather cms data and fix typeError for display in page #326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions includes/AlmaSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ class AlmaSettings {
public $display_cart_eligibility;

/**
* Bool if In Page is activated.
* In Page is activated ("yes" or "no").
*
* @var bool
* @var string
*/
public $display_in_page;

Expand Down
33 changes: 32 additions & 1 deletion includes/Services/CollectCmsDataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Alma\Woocommerce\Helpers\ToolsHelper;
use Alma\Woocommerce\WcProxy\FunctionsProxy;
use Alma\Woocommerce\WcProxy\OptionProxy;
use Alma\Woocommerce\WcProxy\PaymentGatewaysProxy;
use Alma\Woocommerce\WcProxy\ThemeProxy;

/**
Expand Down Expand Up @@ -180,13 +181,14 @@ private function get_cms_features() {
'widget_cart_activated' => 'yes' === $this->alma_settings->display_cart_eligibility,
'widget_product_activated' => 'yes' === $this->alma_settings->display_product_eligibility,
'used_fee_plans' => $this->format_fee_plans(),
'in_page_activated' => $this->alma_settings->display_in_page,
'in_page_activated' => 'yes' === $this->alma_settings->display_in_page,
'log_activated' => 'yes' === $this->alma_settings->debug,
'excluded_categories' => $this->alma_settings->excluded_products_list,
'is_multisite' => is_multisite(),
'specific_features' => array(
( in_array( 'alma-woocommerce-gateway/alma-gateway-for-woocommerce.php', $auto_update_plugins, true ) ) ? 'auto_update' : null,
),
'payment_method_position' => $this->get_alma_gateway_position(),
)
);
}
Expand Down Expand Up @@ -255,4 +257,33 @@ private function format_fee_plans() {
return $plans;
}

/**
* Get Alma gateway position.
* Remove alma_* gateways from the list of gateways.
* Sort gateways to re-index all.
*
* @return int
*/
private function get_alma_gateway_position() {
$gateways = PaymentGatewaysProxy::get_instance()->get_payment_gateways();
$gateway_position = 0;

$gateways = array_filter(
$gateways,
function( $gateway) {
return ! preg_match( '/alma_.+/', $gateway->id );
}
);

$gateways = array_values( $gateways );

foreach ( $gateways as $position => $gateway ) {
if ( 'alma' === $gateway->id ) {
$gateway_position = $position + 1;
}
}

return $gateway_position;
}

}
64 changes: 64 additions & 0 deletions includes/WcProxy/PaymentGatewaysProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Payment gateways proxy.
*
* @package Alma\Woocommerce\WcProxy
*/
namespace Alma\Woocommerce\WcProxy;

use WC_Payment_Gateways;

class PaymentGatewaysProxy {

/**
* @var PaymentGatewaysProxy|null
*/
private static $instance = null;

/**
* @var WC_Payment_Gateways
*/
private $wc_payment_gateway;

/**
* PaymentGatewaysProxy constructor.
*
* @param WC_Payment_Gateways|null $wc_payment_gateway
*/
private function __construct( $wc_payment_gateway = null) {
$this->wc_payment_gateway = $wc_payment_gateway ? $wc_payment_gateway : WC_Payment_Gateways::instance();
}

/**
* Get the singleton instance of PaymentGatewaysProxy.
*
* @param WC_Payment_Gateways|null $wc_payment_gateway
* @return PaymentGatewaysProxy
*/
public static function get_instance( $wc_payment_gateway = null) {
if (null === self::$instance) {
self::$instance = new self( $wc_payment_gateway );
}

return self::$instance;
}

/**
* Get payment gateways.
*
* @return array
*/
public function get_payment_gateways() {
return $this->wc_payment_gateway->payment_gateways();
}

/**
* Used for unit tests.
*
* @return void
*/
public static function reset_instance() {
self::$instance = null;
}

}
Loading