/* 
Theme Name: Hello Elementor Child
Theme URI: https://github.com/elementor/hello-theme-child/
Description: Hello Elementor Child is a child theme of Hello Elementor, created by Elementor team
Author: Elementor Team
Author URI: https://elementor.com/
Template: hello-elementor
Version: 2.0.0
Text Domain: hello-elementor-child
License: GNU General Public License v3 or later.
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Tags: flexible-header, custom-colors, custom-menu, custom-logo, editor-style, featured-images, rtl-language-support, threaded-comments, translation-ready
*/

/* Add your custom styles here */

<?php
/**
 * Hello Elementor Child Theme functions and definitions
 */

// 1. Declare WooCommerce Support
function hello_child_woocommerce_setup() {
    add_theme_support('woocommerce');
    add_theme_support('wc-product-gallery-zoom');
    add_theme_support('wc-product-gallery-lightbox');
    add_theme_support('wc-product-gallery-slider');
}
add_action('after_setup_theme', 'hello_child_woocommerce_setup');


// 2. Display Total Cart Weight on WooCommerce Cart & Checkout Pages
function add_total_cart_weight_to_totals() {
    if ( WC()->cart ) {
        $total_weight = WC()->cart->get_cart_contents_weight();
        $weight_unit  = get_option('woocommerce_weight_unit');

        if ( $total_weight > 0 ) {
            ?>
            <tr class="cart-total-weight">
                <th><?php esc_html_e( 'Total Weight', 'woocommerce' ); ?></th>
                <td data-title="<?php esc_attr_e( 'Total Weight', 'woocommerce' ); ?>">
                    <strong><?php echo esc_html( $total_weight . ' ' . $weight_unit ); ?></strong>
                </td>
            </tr>
            <?php
        }
    }
}
add_action( 'woocommerce_cart_totals_before_shipping', 'add_total_cart_weight_to_totals' );
add_action( 'woocommerce_review_order_before_shipping', 'add_total_cart_weight_to_totals' );


// 3. Shortcode [cart_weight] to display total weight anywhere in Elementor
function custom_cart_weight_shortcode() {
    if ( null !== WC()->cart ) {
        $total_weight = WC()->cart->get_cart_contents_weight();
        $weight_unit  = get_option('woocommerce_weight_unit');
        if ( $total_weight > 0 ) {
            return '<span class="custom-cart-weight">Weight: ' . esc_html( $total_weight . ' ' . $weight_unit ) . '</span>';
        }
    }
    return '<span class="custom-cart-weight">Weight: 0 kg</span>';
}
add_shortcode( 'cart_weight', 'custom_cart_weight_shortcode' );