• Resolved whatthecup

    (@whatthecup)


    Hi,

    I am using this code for products page but it is displaying the incorrect weight. Please advise.

    add_action (‘woocommerce_single_product_summary’, ‘show_weight’, 20);
    function show_weight() {
    global $product;
    $attributes = $product->get_attributes();
    if ( $product->has_weight() ) {
    print ‘<p>Weight: ‘.$product->get_weight().'</p>’.PHP_EOL;
    }
    }

    And using this code for shop page and this is also displaying incorrect weight.

    /**
    * Show product weight on archive pages
    */
    add_action( ‘woocommerce_after_shop_loop_item’, ‘rs_show_weights’, 9 );

    function rs_show_weights() {

    global $product;
    $weight = $product->get_weight();

    if ( $product->has_weight() ) {
    echo ‘<div class=”product-meta”><span class=”product-meta-label”> </span>’ . $weight . get_option(‘woocommerce_weight_unit’) . ‘</div></br>’;
    }
    }

    Thanks in advance

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • @whatthecup – I’ve just tested this code on a local install and it seems to be returning the correct weight. Can you expand on what you mean by “it is displaying the incorrect weight”?

    Product settings: https://d.pr/i/KVSAjl
    Product page: https://d.pr/i/F0XRLf
    Shop page: https://d.pr/i/3VjiSM

    – Joey

    Thread Starter whatthecup

    (@whatthecup)

    We have variable products (but weight is mentioned the same for each variation). Can that be messing the numbers?

    The weight is displayed correctly in additional information tab. but the weight mentioned for the same product on shop and product page shows a different number using the code.

    Thread Starter whatthecup

    (@whatthecup)

    Thanks its working now but the above code doesn’t give the weight unit on product page.

    Can you please let me know how to add that

    add_action (‘woocommerce_single_product_summary’, ‘show_weight’, 20);
    function show_weight() {
    global $product;
    $attributes = $product->get_attributes();
    if ( $product->has_weight() ) {
    print ‘<p>Weight: ‘.$product->get_weight().'</p>’.PHP_EOL;
    }
    }

    Thanks in advance

    @whatthecup – When you say that the code is not working on the product page, does this still just apply to variable products?

    – Joey

    Thread Starter whatthecup

    (@whatthecup)

    For all. it shows the weight but not the unit. For eg: 454g weight only shows 454 (grams missing).

    Please let me know.

    thanks

    Plugin Support con

    (@conschneider)

    Engineer

    Hi again,

    For all. it shows the weight but not the unit. For eg: 454g weight only shows 454 (grams missing).

    Gotcha. Here you go:

    add_action ('woocommerce_single_product_summary', 'show_weight', 20);
    function show_weight() {
    global $product;
    $weight_unit = get_option('woocommerce_weight_unit');
    $attributes = $product->get_attributes();
    if ( $product->has_weight() ) {
    print '<p>Weight: '.$product->get_weight(). $weight_unit . '</p>'.PHP_EOL;
    }
    }

    Kind regards,

    Thread Starter whatthecup

    (@whatthecup)

    Thank you so much. it worked! Appreciate all your help

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Display Product Weight on Product Page and Shop Page’ is closed to new replies.