Home > other >  if / else weight values not being passed to woocommerce shopping cart page
if / else weight values not being passed to woocommerce shopping cart page

Time:09-26

I need a progress bar on the shopping cart page to indicate how many items are added for a specific total value. Count won't work for these items because it would require decimals. $ don't work because of a membership discount is applied before even subtotal. So I thought weight would give me a value that could be measured and applied.

The cart is doing a good job of adding the amounts and displaying a number using

    add_filter( 'woocommerce_add_to_cart_fragments', 'wc_refresh_mini_cart_weight');
function wc_refresh_mini_cart_weight($fragments){
    ob_start();
    $items_weight = WC()->cart->get_cart_contents_weight();
    ?>
    <div id="mini-cart-weight">
        
<?php echo $items_weight ? $items_weight : '&nbsp;'; ?>


</div>
    <?php
        $fragments['#mini-cart-weight'] = ob_get_clean();
    return $fragments;
}

But what I need is to format that total amount into a progress bar - preferrably with CSS so I have been trying variations of

add_filter( 'woocommerce_add_to_cart_fragments', 'wc_refresh_mini_cart_weight');
function wc_refresh_mini_cart_weight($fragments){
    ob_start();
    $items_weight = WC()->cart->get_cart_contents_weight(); ?>

 
<div id="mini-cart-weight">
    <?php echo $items_weight ? $items_weight : '&nbsp;'; 
    
if ( $items_weight = 0) {
        echo '<div >' ,  $items_weight ? $items_weight : '&nbsp;' , '</div>';
    }
        elseif ( $items_weight = 1 ) {
        echo '<div >' ,  $items_weight ? $items_weight : '&nbsp;' , '</div>';
    }
    
            elseif ( $items_weight = 2 ) {
        echo '<div >' ,  $items_weight ? $items_weight : '&nbsp;' , '</div>';
    }
            elseif ( $items_weight = 3 ) {
        echo '<div >' ,  $items_weight ? $items_weight : '&nbsp;' , '</div>';
    }
        else  {
        echo '<div >' ,  $items_weight ? $items_weight : '&nbsp;' , '</div>';
    }
    
     ?> 


</div>
    
<?php
        $fragments['#mini-cart-weight'] = ob_get_clean();
    return $fragments;
            
}

the first echo statement (only included to tell me what the weight total is while I'm troubleshooting this) gives the correct total but the if/else echos are only returning "1" no matter what the actual total is:

<div id="mini-cart-weight">
    2<div >1</div> 


</div>

I'm running around in circles on this. Any suggestions, ideas, or answers? Thanks.

CodePudding user response:

$items_weight = 3 here you're using assignment, you need to compare use == for comparing or === for strict comparison, it will check if $items_weight a number/integer and it is 3, fix it in all statement where you have done it wrong.

  • Related