Skip to content

Commit 5eab29f

Browse files
Hide options if product not available
* Hide params if product not available * Code improvements
1 parent 8acc613 commit 5eab29f

File tree

9 files changed

+353
-220
lines changed

9 files changed

+353
-220
lines changed

plugin/aplazame.php

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class WC_Aplazame {
2929
const METHOD_ID = 'aplazame';
3030
const METHOD_TITLE = 'Aplazame';
3131

32+
// Product types
33+
const INSTALMENTS = 'instalments';
34+
const PAY_LATER = 'pay_later';
35+
3236
public static function _m_or_a( $obj, $method, $attribute ) {
3337
if ( method_exists( $obj, $method ) ) {
3438
return $obj->$method();
@@ -67,8 +71,7 @@ public static function configure_aplazame_profile( $sandbox, $private_key ) {
6771
$private_key
6872
);
6973

70-
$response = $client->get( '/me' );
71-
return $response;
74+
return $client->get( '/me' );
7275
}
7376

7477
/**
@@ -141,7 +144,7 @@ public function __construct( $apiBaseUri ) {
141144
),
142145
100
143146
);
144-
};
147+
}
145148

146149
if ( $this->is_cart_widget_enabled() ) {
147150
add_action(
@@ -152,7 +155,7 @@ public function __construct( $apiBaseUri ) {
152155
),
153156
100
154157
);
155-
};
158+
}
156159

157160
add_filter( 'woocommerce_product_data_tabs', array( $this, 'aplazame_campaigns_tab' ) );
158161
add_action( 'woocommerce_product_data_panels', array( $this, 'product_campaigns' ) );
@@ -207,12 +210,20 @@ public function capture_order( $order_id ) {
207210
/**
208211
* Add relevant links to plugins page
209212
*
210-
* @param array $links
213+
* @param array $links
214+
*
211215
* @return array
216+
* @throws Exception
212217
*/
213218
public function plugin_action_links( $links ) {
219+
$extra_param = '';
220+
221+
if ( self::is_aplazame_product_available( self::PAY_LATER ) ) {
222+
$extra_param = '_' . self::PAY_LATER;
223+
}
224+
214225
$plugin_links = array(
215-
'<a href="' . admin_url( 'admin.php?page=wc-settings&tab=checkout&section=aplazame' ) . '">' . __( 'Settings', 'aplazame' ) . '</a>',
226+
'<a href="' . admin_url( 'admin.php?page=wc-settings&tab=checkout&section=aplazame' . $extra_param ) . '">' . __( 'Settings', 'aplazame' ) . '</a>',
216227
);
217228
return array_merge( $plugin_links, $links );
218229
}
@@ -243,17 +254,24 @@ public function add_order_note( $order_id, $msg ) {
243254
* @param array $methods
244255
*
245256
* @return array|void
257+
* @throws Exception
246258
*/
247259
public function add_gateway( $methods ) {
248260
if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
249261
return;
250262
}
251263

252-
include_once 'classes/wc-aplazame-gateway.php';
253-
include_once 'classes/wc-aplazame-pay-later-gateway.php';
264+
if ( self::is_aplazame_product_available( self::PAY_LATER ) ) {
265+
include_once 'classes/wc-aplazame-pay-later-gateway.php';
266+
$methods[] = 'WC_Aplazame_Pay_Later_Gateway';
267+
268+
if ( ! self::is_aplazame_product_available( self::INSTALMENTS ) ) {
269+
return $methods;
270+
}
271+
}
254272

273+
include_once 'classes/wc-aplazame-gateway.php';
255274
$methods[] = 'WC_Aplazame_Gateway';
256-
$methods[] = 'WC_Aplazame_Pay_Later_Gateway';
257275

258276
return $methods;
259277
}
@@ -308,7 +326,32 @@ protected static function is_aplazame_order( $order_id ) {
308326
*/
309327
protected static function is_aplazame_pay_later_order( $order_id ) {
310328
$order = wc_get_order( $order_id );
311-
return $order->get_payment_method() === self::METHOD_ID . '_pay_later';
329+
return $order->get_payment_method() === self::METHOD_ID . '_' . self::PAY_LATER;
330+
}
331+
332+
/**
333+
* @param $product_type
334+
*
335+
* @return bool
336+
*/
337+
public function is_aplazame_product_available( $product_type ) {
338+
if ( ! $this->private_api_key ) {
339+
return false;
340+
}
341+
342+
$client = $this->get_client()->apiClient;
343+
try {
344+
$response = $client->get( '/me' );
345+
} catch ( Exception $e ) {
346+
return false;
347+
}
348+
349+
$products = array();
350+
foreach ( $response['products'] as $product ) {
351+
$products[] = $product['type'];
352+
}
353+
354+
return in_array( $product_type, $products );
312355
}
313356

314357
public function api_router() {

plugin/classes/api/Aplazame_Api_ConfirmController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function confirm( $payload ) {
4747
return Aplazame_Api_Router::not_found();
4848
}
4949

50-
if ( ! in_array( $order->get_payment_method(), array( WC_Aplazame::METHOD_ID, WC_Aplazame::METHOD_ID . '_pay_later' ) ) ) {
50+
if ( ! in_array( $order->get_payment_method(), array( WC_Aplazame::METHOD_ID, WC_Aplazame::METHOD_ID . '_' . WC_Aplazame::PAY_LATER ) ) ) {
5151
return self::ko( 'Aplazame is not the current payment method' );
5252
}
5353

0 commit comments

Comments
 (0)