Add action not working for the archive pages wc loop

This topic has 3 replies, 3 voices, and was last updated 1 months, 3 weeks ago ago by Andrew Mitchell

  • Avatar: Abhijit
    Abhijit
    Participant
    September 25, 2024 at 12:02

    We have added the below code which is working with other themes but not with the Xstore store theme. Let us know the correct code. We need to display the weight the same as displayed on the homepage categories tabs.

    add_action('woocommerce_after_shop_loop_item_title', 'show_weight', 20);
    add_action('woocommerce_single_product_summary', 'show_weight', 20);
    
    function show_weight() {
        global $product;
    
        // Ensure $product is defined on category/archive pages
        if ( ! is_object( $product ) ) {
            $product = wc_get_product( get_the_ID() );
        }
    
        $weight_unit = get_option('woocommerce_weight_unit');
    
        if ( $product->has_weight() ) {
            echo '<p><strong>Weight: ' . esc_html( $product->get_weight() . ' ' . $weight_unit ) . '</strong></p>';
        }
    }

    Looking forward to your response.

    best Regards,
    Abhijit

    2 Answers
    Avatar: Justin
    Luca Rossi
    Support staff
    September 26, 2024 at 06:55

    Hi @Abhijit,

    Please try with this hooks instead:

    
    add_action('after_etheme_product_grid_list_product_element_title', 'show_weight', 20);
    add_action('woocommerce_before_add_to_cart_form', 'show_weight', 20);
    
    function show_weight() {
    	global $product;
    
    	// Ensure $product is defined on category/archive pages
    	if ( ! is_object( $product ) ) {
    		$product = wc_get_product( get_the_ID() );
    	}
    
    	$weight_unit = get_option('woocommerce_weight_unit');
    
    	if ( $product->has_weight() ) {
    		echo '<p><strong>Weight: ' . esc_html( $product->get_weight() . ' ' . $weight_unit ) . '</strong></p>';
    	}
    }
    

    You can find all the hooks of single product page here: https://www.businessbloomer.com/woocommerce-visual-hook-guide-single-product-page/.

    Hope it helps!

    Avatar: Andrew Mitchell
    Andrew Mitchell
    Support staff
    September 26, 2024 at 07:52

    Dear Abhijit,

    In the spirit of gratitude, we want to express our appreciation for your trust in our products. As a valued customer, your experience matters greatly. Would you consider sharing it by giving our theme a deserving 5-star rating on ThemeForest?

    Click here to share your thoughts: https://themeforest.net/downloads

    Being part of our community means a lot, and your feedback contributes immensely.

    Best Regards,
    The 8Theme Team

  • Viewing 3 results - 1 through 3 (of 3 total)

The issue related to '‘Add action not working for the archive pages wc loop’' has been successfully resolved, and the topic is now closed for further responses

8theme customization service
We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.