1

I'm having an issue with a foreach loop iterating its entire array contents in each instance of the while loop.

I've tried moving the loop, adding break & continue points and using alternate syntaxes-- I feel like I'm just missing something small here. Do I need to further define the array? Do I integrate the foreach loop into the while somehow? Any pointers would be much appreciated. Here is a basic run down of the code (using "Shopp" an ecommerce plugin with a similar code example at: https://shopplugin.net/api/shopp_cart_items/):

<?php  while(shopp('cart','items')): // initializes the shopping cart loop

           $Items = shopp_cart_items(); 
            foreach ( $Items as $item ):

        echo $item->quantity * $item->weight; // multiplies weight by quantity

                endforeach; echo 'g'; endwhile; ?>

The products are measured in weight, so as an example, there are 2 items in the cart with their weights at 1 gram, and 2 grams respectively.

Here's the current output:

Cart item 1 - Weight: 12g
Cart item 2 - Weight: 12g

Here's the expected output:

Cart item 1 - Weight: 1g
Cart item 2 - Weight: 2g

Thanks in advance!

IH

Edit:

There is no documentation in Shopp concerning product weight, so I had to find the product object and arrays and added an ++ operator to cycle through them with each while iteration. Here is the code I used.

echo $Items[$i++]->option->dimensions['weight'] * shopp('cartitem','quantity', 'echo=false');

with the integer set outside the loop: $i = 0;

5
  • Please add a language tag. Commented Sep 9, 2013 at 21:38
  • 2
    I don't understand why there are two looping structures in play here. What is your intent in doing that? Commented Sep 9, 2013 at 21:41
  • 1
    Looks like you are printing both item weight for each item. Seems that only one loop would be enough Commented Sep 9, 2013 at 21:42
  • Drop the while(shopp(...)) line. That call does nothing but confuse you. The example you site doesn't even show the while loop. Commented Sep 9, 2013 at 21:45
  • The first loop is the cart items loop, where each cart item's details are instanced. It does seem logical that only one loop is required, but using the code "echo $item->quantity * $item->weight;" without the loop doesn't return anything! Commented Sep 10, 2013 at 18:45

2 Answers 2

0
echo $item->quantity * $item->weight; // multiplies weight by quantity

is probably returning improperly, but could be as simple as

echo ($item->quantity * $item->weight);

I'd also recommend using something besides Item as $item $Item and $items is getting really confusing to look at.

Sign up to request clarification or add additional context in comments.

1 Comment

operand operator operand - there is nothing which is ambiguous with such expressions - no parenthesis required.
0

another approach

  $product_id = shopp('cartitem.get-product');
  $weight = shopp('product.get-weight'); // use product tag to get weight
  $quantity = shopp('cartitem.get-quantity'); // use cart-item tag to get quantity
  echo $weight * $quantity .' g';

2 Comments

It looks you are mixing old syntax (shopp('cart','items')) togather with new syntax (shopp('cart.items')). New syntax of cart.items is exampled here: shopplugin.net/api/cart-hasitems
I have adjusted it, but there is no old and new syntax. The 'new' one is just a shorthand version. Mixing the two doesn't result in errors. When you look at the core Shopp code you will find both notations are used. Eventually every shopp() command in the core code will be replaced with the shorthand version of course.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.