< Cart page buy icon

add_action('woocommerce_before_cart', 'add_custom_icon_to_cart_page');

function add_custom_icon_to_cart_page() {
    // Check if the user is logged in and if the billing address is complete
    $user_id = get_current_user_id();
    if ($user_id > 0) {
        $required_fields = array(
            'billing_address_1',
            'billing_city',
            'billing_postcode',
            'billing_country',
        );

        $hide_icon = false;
        foreach ($required_fields as $field) {
            if (!get_user_meta($user_id, $field, true)) {
                $hide_icon = true;
                break;
            }
        }

        // Only proceed if the billing address is complete
        if (!$hide_icon) {
            // Get the total cart item count and total cart amount
            $cart_count = WC()->cart->get_cart_contents_count();
            $cart_total = WC()->cart->get_cart_total();

            // Output the HTML for the icon with cart count and total amount
            echo '<div id="custom-icon" style="position: fixed; z-index: 9999; top: 50%; right: 0; transform: translateY(-50%); cursor: pointer;">';
            echo '<span style="position: absolute; top: -10px; left: -10px; background-color: #ff0000; color: #ffffff; border-radius: 50%; padding: 2px 5px; font-size: 12px;">' . esc_html($cart_count) . '</span>';
            echo '<img src="https://www.hlomart.com/wp-content/uploads/2023/12/IMG_1640-7.png" alt="Icon" style="max-width: 100px;">';
            echo '<div style="position: absolute; bottom: -20px; left: 50%; transform: translateX(-50%); background-color: #ffffff; color: #ff0000; border-radius: 5px; padding: 2px 5px; font-size: 12px; border: 1px solid #ff0000;">' . $cart_total . '</div>';
            echo '</div>';
        }
    }
}

add_action('wp_footer', 'custom_icon_js');

function custom_icon_js() {
    if (is_cart()) { // Ensure this JS runs only on the cart page
        ?>
        <script type="text/javascript">
            jQuery(document).ready(function($) {
                // Attach click event to the custom icon
                $('#custom-icon').on('click', function() {
                    // Trigger click event of the "Place Order" button
                    $('#place_order').click();
                    // Redirect to the order page after 2 seconds
                    setTimeout(function() {
                        window.location.href = '/order'; // Replace '/order' with your actual order page URL
                    }, 200); // 200 milliseconds 
                });
            });
        </script>
        <?php
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart
🏠 Home 🛍️ Category 🧑🏻‍⚕️ Account 💝 0 Wishlist 🏦 Wallet
Custom Icon
0
0.00
Scroll to Top