/* 
 Theme Name:   Neve Child
 Theme URI:    
 Description:  
 Author:       frederickfrei
 Author URI:   http://frederickfrei.wpvence.com
 Template:     neve
 Version:      1.0

 /* == Add your own styles below this line ==
--------------------------------------------*/

add_action('woocommerce_before_calculate_totals', 'apply_coupon_for_local_pickup', 10, 1);

function apply_coupon_for_local_pickup($cart) {
    if (is_admin() && !defined('DOING_AJAX'))
        return;

    if (did_action('woocommerce_before_calculate_totals') >= 2)
        return;

    // Define the coupon code
    $coupon_code = 'FARMERS3';

    // Check if local pickup is selected
    $is_local_pickup = false;
    foreach (WC()->cart->get_shipping_packages() as $package) {
        foreach ($package['rates'] as $rate) {
            if ($rate->get_method_id() == 'local_pickup' && $rate->get_label() == 'Farmer\'s Market') {
                $is_local_pickup = true;
                break;
            }
        }
        if ($is_local_pickup) break;
    }

    // Check if there are specific variable products in the cart
    $has_specific_variable_products = false;
    foreach ($cart->get_cart() as $cart_item) {
        $product = $cart_item['data'];
        if ($product->is_type('variable') && in_array($product->get_id(), array(1233, 1129, 1230, 1218, 1185, 1228, 1232, 1213, 1224, 1210, 1226, 1206, 1222, 1202))) {
            $has_specific_variable_products = true;
            break;
        }
    }

    // Apply the coupon if conditions are met
    if ($is_local_pickup && $has_specific_variable_products) {
        if (!WC()->cart->has_discount($coupon_code)) {
            WC()->cart->add_discount($coupon_code);
        }
    } else {
        if (WC()->cart->has_discount($coupon_code)) {
            WC()->cart->remove_coupon($coupon_code);
        }
    }
}
function change_continue_shopping_button_text($text) {
    if ($text === 'Continue Shopping') {
        return 'Back to Coffee';
    }
    return $text;
}
add_filter('woocommerce_continue_shopping', 'change_continue_shopping_button_text');
