1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
<?php
namespace PaymentPlugins;
class WC_Stripe_Admin_Meta_Box_Product_Data {
private static $_gateways = array();
private static $_options = array();
public static function init() {
add_filter( 'woocommerce_product_data_tabs', array( __CLASS__, 'product_data_tabs' ) );
add_action( 'woocommerce_product_data_panels', array( __CLASS__, 'output_panel' ) );
add_action( 'woocommerce_admin_process_product_object', array( __CLASS__, 'save' ) );
}
public static function product_data_tabs( $tabs ) {
if ( current_user_can( 'manage_woocommerce' ) ) {
$tabs['stripe'] = array(
'label' => __( 'Stripe Settings', 'woo-stripe-payment' ),
'target' => 'stripe_product_data',
'class' => array( 'hide_if_external' ),
'priority' => 100,
);
}
return $tabs;
}
public static function output_panel() {
global $product_object;
self::init_gateways( $product_object );
if ( current_user_can( 'manage_woocommerce' ) ) {
include 'views/html-product-data.php';
}
}
private static function init_gateways( $product ) {
$order = $product->get_meta( \WC_Stripe_Constants::PRODUCT_GATEWAY_ORDER );
$order = ! $order ? array() : $order;
foreach ( WC()->payment_gateways()->payment_gateways() as $gateway ) {
if ( $gateway->supports( 'wc_stripe_product_checkout' ) ) {
if ( isset( $order[ $gateway->id ] ) ) {
self::$_gateways[ $order[ $gateway->id ] ] = $gateway;
} else {
self::$_gateways[] = $gateway;
}
self::$_options[ $gateway->id ] = new \WC_Stripe_Product_Gateway_Option( $product, $gateway );
}
}
ksort( self::$_gateways );
}
private static function get_product_option( $gateway_id ) {
return self::$_options[ $gateway_id ];
}
private static function get_payment_gateways() {
$gateways = array();
foreach ( self::$_gateways as $gateway ) {
$gateways[ $gateway->id ] = $gateway;
}
return $gateways;
}
public static function save( $product ) {
if ( empty( $_POST['wc_stripe_update_product'] ) ) {
return;
}
$loop = 0;
$order = array();
self::init_gateways( $product );
$payment_gateways = self::get_payment_gateways();
if ( isset( $_POST['stripe_gateway_order'] ) ) {
foreach ( $_POST['stripe_gateway_order'] as $i => $gateway ) {
$order[ $gateway ] = $loop;
if ( isset( $_POST['stripe_capture_type'] ) ) {
self::get_product_option( $gateway )->set_option( 'charge_type', wc_clean( $_POST['stripe_capture_type'][ $i ] ) );
self::get_product_option( $gateway )->save();
}
$loop ++;
}
}
if ( isset( $_POST['_stripe_button_position'] ) ) {
$product->update_meta_data( '_stripe_button_position', wc_clean( $_POST['_stripe_button_position'] ) );
}
$product->update_meta_data( \WC_Stripe_Constants::PRODUCT_GATEWAY_ORDER, $order );
}
}
\PaymentPlugins\WC_Stripe_Admin_Meta_Box_Product_Data::init();