| |
| <?php
|
|
|
| add_action('woocommerce_cart_calculate_fees', 'progressive_discount', 20, 1);
|
| function progressive_discount($cart) {
|
|
|
| if (is_admin() && !defined('DOING_AJAX'))
|
| return;
|
|
|
|
|
| $discount_tiers = array(
|
|
|
| array(
|
| 'quantity' => 10,
|
| 'discount_rate' => 0.02,
|
| 'label' => 'Progressive Discount'
|
| ),
|
|
|
| array(
|
| 'quantity' => 20,
|
| 'discount_rate' => 0.03,
|
| 'label' => 'Additional Progressive discount 1'
|
| ),
|
|
|
| array(
|
| 'quantity' => 50,
|
| 'discount_rate' => 0.04,
|
| 'label' => 'Additional Progressive Discount 2'
|
| ),
|
| );
|
|
|
|
|
|
|
| $total_quantity = 0;
|
| $discounted_quantity = 0;
|
|
|
|
|
| foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
|
| $product_id = $cart_item['product_id'];
|
| $quantity = $cart_item['quantity'];
|
| $enable_discount = get_post_meta($product_id, 'progressive_offer', true);
|
|
|
|
|
| if ($enable_discount === 'yes') {
|
| $total_quantity += $quantity;
|
|
|
|
|
| $discount_amount = 0;
|
| $previous_tier_quantity = 0;
|
| $current_discount_label = '';
|
|
|
|
|
| foreach ($discount_tiers as $discount_tier) {
|
| if ($total_quantity >= $discount_tier['quantity']) {
|
|
|
| $tier_discount_amount = ($total_quantity - max($previous_tier_quantity, $discount_tier['quantity'])) * $cart->subtotal * $discount_tier['discount_rate'] / $cart->cart_contents_count;
|
|
|
| $discount_amount += $tier_discount_amount;
|
|
|
| $previous_tier_quantity = $discount_tier['quantity'];
|
|
|
| $current_discount_label = $discount_tier['label'];
|
| } else {
|
| break;
|
| }
|
| }
|
| }
|
| }
|
|
|
|
|
| if ($discount_amount > 0) {
|
| $cart->add_fee($current_discount_label, -$discount_amount);
|
| }
|
| }
|
|
|
|
|
| add_action('woocommerce_product_options_general_product_data', 'add_progressive_offer_checkbox');
|
| function add_progressive_offer_checkbox() {
|
| global $post;
|
| woocommerce_wp_checkbox(
|
| array(
|
| 'id' => 'progressive_offer',
|
| 'label' => __('Enable Progressive Offer', 'woocommerce'),
|
| 'description' => __('Check this box if you want this product to be eligible for the progressive offer.', 'woocommerce'),
|
| 'value' => get_post_meta($post->ID, 'progressive_offer', true),
|
| )
|
| );
|
| }
|
|
|
|
|
| add_action('woocommerce_process_product_meta', 'save_progressive_offer_checkbox');
|
| function save_progressive_offer_checkbox($post_id) {
|
|
|
| $progressive_offer = isset($_POST['progressive_offer']) ? 'yes' : 'no';
|
| update_post_meta($post_id, 'progressive_offer', $progressive_offer);
|
| }
|
|
|
|
|
| add_action('woocommerce_after_add_to_cart_button', 'add_progressive_offer_notice');
|
| function add_progressive_offer_notice() {
|
| global $post;
|
|
|
| $enable_discount = get_post_meta($post->ID, 'progressive_offer', true);
|
| if ($enable_discount === 'yes') {
|
|
|
| echo '<div class="woocommerce-message">' . __('This product is eligible for the progressive offer!', 'woocommerce') . '</div>';
|
|
|
|
|
| $discount_tiers = array(
|
|
|
| array(
|
| 'quantity' => 10,
|
| 'discount_rate' => 0.02,
|
| 'label' => 'Progressive Discount'
|
| ),
|
|
|
| array(
|
| 'quantity' => 20,
|
| 'discount_rate' => 0.03,
|
| 'label' => 'Additional Progressive discount 1'
|
| ),
|
|
|
| array(
|
| 'quantity' => 50,
|
| 'discount_rate' => 0.04,
|
| 'label' => 'Additional Progressive Discount 2'
|
| ),
|
| );
|
|
|
|
|
|
|
| add_action('wp_enqueue_scripts', 'add_custom_css');
|
| function add_custom_css() {
|
|
|
| $custom_css = "
|
| .woocommerce-message {
|
| clear: both;
|
| margin-top: 1em;
|
| background-color: white;
|
| }
|
| ";
|
| wp_add_inline_style('woocommerce-general', $custom_css);
|
| }
|
| |
| |
Comments