wtyczki – Utworzono fragmenty rabatu za pośrednictwem chatgpt w oparciu o wybraną bramkę płatniczą. Działa, ale moja strona ustawień lub strona pulpitu nawigacyjnego wygląda tak
![wtyczki – Utworzono fragmenty rabatu za pośrednictwem chatgpt w oparciu o wybraną bramkę płatniczą. Działa, ale moja strona ustawień lub strona pulpitu nawigacyjnego wygląda tak](https://oen.pl/wp-content/uploads/2023/09/apple-touch-icon@2.png)
class Payment_Gateway_Discounts { funkcja publiczna __construct() { // Zarejestruj ustawienia add_action(’admin_init’, array($this, 'register_payment_gateway_discount_settings’));
// Add the settings page
add_action('admin_menu', array($this, 'add_payment_gateway_discount_settings_page'));
// Display the settings page
add_action('admin_menu', array($this, 'payment_gateway_discount_settings_page'));
// Display discount in gateway title
add_filter('woocommerce_gateway_title', array($this, 'add_discount_amount_to_gateway_title'), 10, 2);
// Apply discount based on payment gateway
add_action('woocommerce_cart_calculate_fees', array($this, 'apply_discount_based_on_payment_gateway'), 10, 1);
// Refresh payment method
add_action('woocommerce_review_order_before_payment', array($this, 'ts_refresh_payment_method'));
}
public function register_payment_gateway_discount_settings() {
register_setting('payment_gateway_discount', 'payment_gateway_discounts');
}
public function add_payment_gateway_discount_settings_page() {
add_options_page(
'Payment Gateway Discount',
'Payment Gateway Discount',
'manage_options',
'payment_gateway_discount',
array($this, 'payment_gateway_discount_settings_page')
);
}
public function payment_gateway_discount_settings_page() {
$gateways = WC()->payment_gateways->payment_gateways();
?>
<div class="wrap">
<h1>Payment Gateway Discount</h1>
<form method="post" action="options.php">
<?php settings_fields('payment_gateway_discount'); ?>
<table class="form-table">
<tbody>
<?php foreach ($gateways as $gateway) : ?>
<tr>
<th scope="row"><?php echo $gateway->title; ?></th>
<td>
<input type="number" name="payment_gateway_discounts[<?php echo $gateway->id; ?>]" value="<?php echo get_option('payment_gateway_discounts')[$gateway->id] ?? ''; ?>" min="0" max="100">%
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
public function add_discount_amount_to_gateway_title($title, $gateway_id) {
$discounts = get_option('payment_gateway_discounts');
if (isset($discounts[$gateway_id]) && $discounts[$gateway_id] > 0) {
$discount_amount = $discounts[$gateway_id];
$title .= " <span style="color: green;">(-$discount_amount% off)</span>";
}
return $title;
}
public function apply_discount_based_on_payment_gateway($cart) {
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
$discounts = get_option('payment_gateway_discounts');
$chosen_gateway = WC()->session->get('chosen_payment_method');
if (isset($discounts[$chosen_gateway])) {
$discount_amount = $discounts[$chosen_gateway];
$discount = $cart->subtotal * ($discount_amount / 100);
$cart->add_fee("Discount for $chosen_gateway", -$discount);
}
}
public function ts_refresh_payment_method() {
?>
<script type="text/javascript">
(function($){
$('form.checkout').on('change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}
}
nowa Payment_Gateway_Discounts();