Created
May 31, 2022 07:57
-
-
Save eduardgermoniy/32d868d04e18504034beadba3fd9650e to your computer and use it in GitHub Desktop.
Добавляет возможность дарить подарки пользователям при определенной сумме заказа
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* @wordpress-plugin | |
* Plugin Name: Подарок при сумме заказа | |
* Plugin URI: https://github.com/eduardgermoniy/woocommerce-gifts-order-amount | |
* Description: Добавляет возможность дарить подарки пользователям при определенной сумме заказа | |
* Version: 1 | |
* Author: Eduard Germoniy | |
* Author URI: https://github.com/eduardgermoniy | |
* Text Domain: gifts-order-amount | |
* | |
*/ | |
//check woocommerce is active | |
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { | |
return; | |
} | |
// ничего не делаем в админке | |
if ( is_admin() ) { | |
return; | |
} | |
class Gift_Order_Amount { | |
private static $amount_for_gift = 2000; | |
private static $id_gift_product = 217; | |
public static function init() { | |
// ничего не делаем если корзина пуста | |
if ( WC()->cart->is_empty() ) { | |
return; | |
} | |
add_action( 'template_redirect', [ __CLASS__, 'add_gifts' ] ); | |
} | |
private static function add_gifts() { | |
$cart = WC()->cart; | |
// если целевой товар не в корзине | |
if ( $cart->get_subtotal() >= self::$amount_for_gift ) { | |
$gift_cart_id = $cart->generate_cart_id( self::$id_gift_product ); | |
$is_gift_in_cart = $cart->find_product_in_cart( $gift_cart_id ); | |
// если подарок присутствует в корзине, то удаляем его из неё | |
if ( $is_gift_in_cart ) { | |
$gift_cart_item = $cart->get_cart_item($gift_cart_id); | |
$gift_cart_item['data']->set_price(0); | |
$gift_cart_item['data']['is-it-gift'] = true; | |
} | |
} | |
} | |
public static function get_id_gift_product() { | |
return self::$id_gift_product; | |
} | |
public static function get_amount_for_gift() { | |
return self::$amount_for_gift; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment