diff --git a/wp/wp-content/themes/medicalalert/helpers/WooHelper.php b/wp/wp-content/themes/medicalalert/helpers/WooHelper.php index 4b7c1c61..d54fde00 100644 --- a/wp/wp-content/themes/medicalalert/helpers/WooHelper.php +++ b/wp/wp-content/themes/medicalalert/helpers/WooHelper.php @@ -68,11 +68,36 @@ add_filter('woocommerce_add_error', 'customize_coupon_error_message', 10, 1); function customize_coupon_error_message($error) { // Check if the error message is related to a coupon already applied if (strpos($error, 'Coupon code already applied!') !== false) { - return ''; // Return an empty string to suppress this specific error message + return ''; } - return $error; // Return the original error message if it's not related to coupons + return $error; } +function get_current_shipping_comment() { + // Get the chosen shipping methods from the cart + $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods'); + if (empty($chosen_shipping_methods)) { + return null; // No shipping methods selected + } + + // Loop through chosen shipping methods to find their settings + foreach ($chosen_shipping_methods as $chosen_method) { + $method_parts = explode(':', $chosen_method); // Split method ID and instance ID + $shipping_method_id = $method_parts[0]; + $instance_id = isset($method_parts[1]) ? $method_parts[1] : ''; + + // Build the option name for the instance-specific settings + $option_name = 'woocommerce_' . $shipping_method_id . '_' . $instance_id . '_settings'; + $instance_settings = get_option($option_name); + + // Check if `shipping_comment` exists + if (!empty($instance_settings) && isset($instance_settings['shipping_comment'])) { + return $instance_settings['shipping_comment']; // Return the first found Salesforce ID + } + } + + return null; // No `shipping_comment` found +} /** * Generates a cart summary with accurate total calculation and full details @@ -157,31 +182,17 @@ function generateCartSummary($showCheckoutButton = true) { $couponDescription[] = $coupon->get_description(); } + $chosen_method = WC()->session->get('chosen_shipping_methods')[0] ?? ''; + error_log('Chosen Method: ' . $chosen_method); - - $order = wc_get_order($order_id); - foreach ($order->get_shipping_methods() as $shipping_item_id => $shipping_item) { - $method_id = $shipping_item->get_method_id(); - $salesforce_id = get_post_meta($order_id, '_salesforce_id_' . $method_id, true); - - if (!empty($salesforce_id)) { - //echo '
Salesforce ID: ' . esc_html($salesforce_id) . '
'; - var_dump($salesforce_id); - } + $shipping_comment = get_current_shipping_comment(); + if ($shipping_comment) { + error_log('Current shipping comment (Salesforce ID): ' . $shipping_comment); + } else { + error_log('No shipping comment found for the currently applied shipping method.'); } - - -// $shipping_methods = WC()->shipping->get_shipping_methods(); -// -// foreach ($shipping_methods as $shipping_method) { -// // Retrieve the settings for each shipping method instance. -// $instance_settings = get_option('woocommerce_' . $shipping_method->id . '_settings'); -// $salesforce_id = $instance_settings['shipping_comment']; -// var_dump($salesforce_id); -// -// } - + // Render with original structure and components @@ -251,27 +262,9 @@ function add_custom_meta_to_order($order, $data) { // Recalculate order totals after adding fees $order->calculate_totals(); } + add_action('woocommerce_checkout_create_order', 'add_custom_meta_to_order', 20, 2); -/** - * Get the name of the chosen shipping method - */ -function getShippingMethodName() { - $packages = WC()->shipping->get_packages(); - - if (!empty($packages)) { - // Get the chosen shipping method - $chosenMethod = WC()->session->get('chosen_shipping_methods')[0] ?? ''; - - // Check if the chosen shipping method exists - if (!empty($chosenMethod) && isset($packages[0]['rates'][$chosenMethod])) { - return $packages[0]['rates'][$chosenMethod]->get_label(); // Get the label (name) of the shipping method - } - } - - return 'Shipping'; // Default to "Standard Shipping" if no method is found -} - /** * Retrieves the default title and cost for the "Shipping" flat rate method from WooCommerce settings. * @@ -297,11 +290,9 @@ function get_default_shipping_info() { $shipping_info['cost'] = $cost; error_log("Shipping Method Title: {$shipping_info['title']}"); error_log("Shipping Method Default Cost: {$shipping_info['cost']}"); - - return $shipping_info; // Exit once the method is found } } - + // Fallback if cost retrieval fails $shipping_info['cost'] = 'N/A'; error_log("Shipping Method Cost not found using get_instance_option; using fallback 'N/A'."); @@ -310,8 +301,6 @@ function get_default_shipping_info() { } } - // Log if 'Shipping' method is not found - error_log("Shipping method 'Shipping' not found in WooCommerce settings."); return null; } @@ -493,9 +482,6 @@ function getShippingType() { return get_title_shipping_method_from_method_id($shippingMethodId); } -/** - * Get the cost of shipping - */ /** * Get the cost of shipping, providing free shipping for annual or semi-annual plans * @@ -564,6 +550,7 @@ function get_title_shipping_method_from_method_id( $method_rate_id = '' ){ $method_key_id = str_replace( ':', '_', $method_rate_id ); $option_name = 'woocommerce_'.$method_key_id.'_settings'; return get_option( $option_name, true )['title']; + } else { return false; } @@ -910,8 +897,8 @@ function apply_custom_fees_and_discounts() { $hasProfessionalInstall = isset($_SESSION["one-time"]["Professional Install"]); $packages = WC()->shipping()->get_packages(); if (!empty($packages) && !empty($packages[0]['rates'])) { - if (($ratePlan === 'Annually' || $ratePlan === 'Semi-Annually' || $isAarpMember || $hasProfessionalInstall) && isset($packages[0]['rates']['free_shipping:4'])) { - WC()->session->set('chosen_shipping_methods', ['free_shipping:4']); + if (($ratePlan === 'Annually' || $ratePlan === 'Semi-Annually' || $isAarpMember || $hasProfessionalInstall) && isset($packages[0]['rates']['free_shipping:1'])) { + WC()->session->set('chosen_shipping_methods', ['free_shipping:1']); } elseif ($ratePlan === 'Monthly' && isset($packages[0]['rates']['flat_rate:2'])) { WC()->session->set('chosen_shipping_methods', ['flat_rate:2']); } @@ -1123,6 +1110,7 @@ function getProductAccessories($product_id) { * Creates the payload for the order to send to Salesforce. */ function createOrderRequest($order) { + $shipping_comment = get_current_shipping_comment(); // Retrieve card type from order meta $card_type = get_post_meta($order->get_id(), '_card_type', true); @@ -1195,7 +1183,7 @@ function createOrderRequest($order) { "CustomField3" => "", "CustomField4" => "", "MarketingCampaign" => "ecom campaign", - "ShippingID" => "a4t3s000000pYVVAA2", + "ShippingID" => $shipping_comment, "PaymentInformation" => [ "PaymentType" => $payment_type, "CardholderName" => $billing['first_name'] . ' ' . $billing['last_name'],