rp-wcdpd-method-cart-discount-simple.class.php 5.15 KB
Newer Older
Pham Huy committed
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
<?php

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

// Load dependencies
if (!class_exists('RP_WCDPD_Method_Cart_Discount')) {
    require_once('rp-wcdpd-method-cart-discount.class.php');
}

/**
 * Cart Discount Method: Simple
 *
 * @class RP_WCDPD_Method_Cart_Discount_Simple
 * @package WooCommerce Dynamic Pricing & Discounts
 * @author RightPress
 */
if (!class_exists('RP_WCDPD_Method_Cart_Discount_Simple')) {

class RP_WCDPD_Method_Cart_Discount_Simple extends RP_WCDPD_Method_Cart_Discount
{
    protected $key              = 'simple';
    protected $group_key        = 'simple';
    protected $group_position   = 10;
    protected $position         = 10;

    // Singleton instance
    protected static $instance = false;

    /**
     * Singleton control
     */
    public static function get_instance()
    {
        if (!self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Constructor class
     *
     * @access public
     * @return void
     */
    public function __construct()
    {
        $this->hook_group();
        $this->hook();
    }

    /**
     * Get group label
     *
     * @access public
     * @return string
     */
    public function get_group_label()
    {
        return __('Simple', 'rp_wcdpd');
    }

    /**
     * Get label
     *
     * @access public
     * @return string
     */
    public function get_label()
    {
        return __('Simple discount', 'rp_wcdpd');
    }

    /**
     * Apply cart discount
     *
     * Note: this implementation uses virtual (non-existing in database) coupons,
     * coupon data is later provided via woocommerce_get_shop_coupon_data hook
     *
     * @access public
     * @param array $adjustment
     * @return void
     */
    public function apply_adjustment($adjustment)
    {
        global $woocommerce;

        // Load controller
        $controller = RP_WCDPD_Controller_Methods_Cart_Discount::get_instance();

        // Use Rule UID as virtual coupon code
        $coupon_code = $adjustment['rule']['uid'];

        // Load virtual coupon
        try {
            $virtual_coupon = new WC_Coupon($coupon_code);
        }
        // Something went wrong
        catch (Exception $e) {
            return;
        }

        // Check if valid coupon was loaded and is not yet used in cart
        if ($virtual_coupon->is_valid() && !$woocommerce->cart->has_discount($coupon_code)) {


            // Apply coupon
            $woocommerce->cart->applied_coupons[] = $coupon_code;

            // Recalculate totals
            $controller->applying_cart_discount = true;
            do_action('woocommerce_applied_coupon', $coupon_code);
            $controller->applying_cart_discount = false;

            // Remove regular coupons if they are not allowed
            if (!RP_WCDPD_Settings::get('cart_discounts_allow_coupons')) {
                RP_WCDPD_WC_Cart::remove_all_regular_coupons();
            }
        }
    }

    /**
     * Get virtual coupon data
     *
     * @access public
     * @param array $adjustment
     * @return array
     */
    public function get_coupon_data($adjustment)
    {
        // Type mapping
        $types = array(
            'discount__amount'                      => (RightPress_Help::wc_version_gte('3.4') ? 'rightpress_fixed_cart' : 'fixed_cart'),
            'discount_per_cart_item__amount'        => (RightPress_Help::wc_version_gte('3.4') ? 'rightpress_fixed_cart' : 'fixed_cart'),
            'discount__percentage'                  => 'percent',
            'discount_per_cart_item__percentage'    => 'percent',
        );

        // Get coupon type
        $type = $types[$adjustment['rule']['pricing_method']];

        // Get coupon amount
        $amount = in_array($type, array('percent', 'percent_product'), true) ? $adjustment['rule']['pricing_value'] : $adjustment['adjustment_amount'];

        // Get coupon data
        return RP_WCDPD_Cart_Discounts::get_coupon_data_array(array(
            'type'          => $type,
            'discount_type' => $type,
            'amount'        => $amount,
        ));
    }

    /**
     * Get adjustment amount
     *
     * @access public
     * @param array $adjustment
     * @return float
     */
    public function get_adjustment_amount($adjustment)
    {
        // Get cart subtotal
        $cart_subtotal = $this->get_cart_subtotal();

        // Get discount amount
        $discount_amount = RP_WCDPD_Pricing::get_adjustment_value($adjustment['rule']['pricing_method'], $adjustment['rule']['pricing_value'], $cart_subtotal, $adjustment);

        // Allow developers to override
        $discount_amount = apply_filters('rp_wcdpd_cart_discount_amount', $discount_amount, $adjustment);

        // Return discount amount
        return (float) abs($discount_amount);
    }

    /**
     * Get virtual coupon label
     *
     * @access public
     * @param array $adjustment
     * @return string
     */
    public function get_coupon_label($adjustment)
    {
        return !RightPress_Help::is_empty($adjustment['rule']['title']) ? $adjustment['rule']['title'] : __('Discount', 'rp_wcdpd');
    }


}

RP_WCDPD_Method_Cart_Discount_Simple::get_instance();

}