• I’m not a developer and I know this code is UGLY, but I learn from piecing stuff together and I need your help. I finally figured out how to display my bulk discounts and I need to clean this code up, any suggestions?
    1. It looks like this now, https://awesomescreenshot.com/0eb2lyon2b, I need to get rid of the list part, “x” out in red.
    2. Suggestions on cleaning up the code?

    Thanks in advance.

    <?php
    					$bulkqty1 = get_post_meta($post->ID, '_bulkdiscount_quantity_1', false);
    					$bulkdis1 = get_post_meta($post->ID, '_bulkdiscount_discount_1', false);
    					$bulkqty2 = get_post_meta($post->ID, '_bulkdiscount_quantity_2', false);
    					$bulkdis2 = get_post_meta($post->ID, '_bulkdiscount_discount_2', false);
    					$bulkqty3 = get_post_meta($post->ID, '_bulkdiscount_quantity_3', false);
    					$bulkdis3 = get_post_meta($post->ID, '_bulkdiscount_discount_3', false);
    					$bulkqty4 = get_post_meta($post->ID, '_bulkdiscount_quantity_4', false);
    					$bulkdis4 = get_post_meta($post->ID, '_bulkdiscount_discount_4', false);
    					$bulkqty5 = get_post_meta($post->ID, '_bulkdiscount_quantity_5', false);
    					$bulkdis5 = get_post_meta($post->ID, '_bulkdiscount_discount_5', false);
    
    					foreach($bulkqty1 as $bulkqty1a) {
    						echo '<li>'.$bulkqty1a.'</li>';
    						}
    					foreach($bulkdis1 as $bulkdis1a) {
    						echo '<li>'.$bulkdis1a.'</li>';
    						}
    					foreach($bulkqty2 as $bulkqty2a) {
    						echo '<li>'.$bulkqty1a.'</li>';
    						}
    					foreach($bulkdis2 as $bulkdis2a) {
    						echo '<li>'.$bulkdis1a.'</li>';
    						}
    					foreach($bulkqty3 as $bulkqty3a) {
    						echo '<li>'.$bulkqty1a.'</li>';
    						}
    					foreach($bulkdis3 as $bulkdis3a) {
    						echo '<li>'.$bulkdis1a.'</li>';
    						}
    					foreach($bulkqty4 as $bulkqty4a) {
    						echo '<li>'.$bulkqty1a.'</li>';
    						}
    					foreach($bulkdis4 as $bulkdis4a) {
    						echo '<li>'.$bulkdis1a.'</li>';
    						}
    					foreach($bulkqty5 as $bulkqty5a) {
    						echo '<li>'.$bulkqty1a.'</li>';
    						}
    					foreach($bulkdis5 as $bulkdis5a) {
    						echo '<li>'.$bulkdis1a.'</li>';
    						}
    					echo '<table>';
    					echo '<tr>';
    					echo "<td>Quantity</td><td>$bulkqty1a</td><td>$bulkqty2a</td><td>$bulkqty3a</td><td>$bulkqty4a</td><td>$bulkqty5a</td>";
    					echo '</tr>';
    					echo '<tr>';
    					echo "<td>% Discount</td><td>$bulkdis1a</td><td>$bulkdis2a</td><td>$bulkdis3a</td><td>$bulkdis4a</td><td>$bulkdis5a</td>";
    					echo '</tr>';
    					echo '</table>';
    				?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • When you call get_post_meta($post->ID, ‘some_key’, false), it returns an array. You are then saving a single value from the array with your foreach loops.

    If you change ‘false’ to ‘true’, it will return only a single value for each key and you can eliminate the foreach loops.

    This may do what you want:

    <?php
       $bulkqty1a = get_post_meta($post->ID, '_bulkdiscount_quantity_1', true);
       $bulkdis1a = get_post_meta($post->ID, '_bulkdiscount_discount_1', true);
       $bulkqty2a = get_post_meta($post->ID, '_bulkdiscount_quantity_2', true);
       $bulkdis2a = get_post_meta($post->ID, '_bulkdiscount_discount_2', true);
       $bulkqty3a = get_post_meta($post->ID, '_bulkdiscount_quantity_3', true);
       $bulkdis3a = get_post_meta($post->ID, '_bulkdiscount_discount_3', true);
       $bulkqty4a = get_post_meta($post->ID, '_bulkdiscount_quantity_4', true);
       $bulkdis4a = get_post_meta($post->ID, '_bulkdiscount_discount_4', true);
       $bulkqty5a = get_post_meta($post->ID, '_bulkdiscount_quantity_5', true);
       $bulkdis5a = get_post_meta($post->ID, '_bulkdiscount_discount_5', true);
    
       echo '<table>';
       echo '<tr>';
       echo "<td>Quantity</td><td>$bulkqty1a</td><td>$bulkqty2a</td><td>$bulkqty3a</td><td>$bulkqty4a</td><td>$bulkqty5a</td>";
       echo '</tr>';
       echo '<tr>';
       echo "<td>% Discount</td><td>$bulkdis1a</td><td>$bulkdis2a</td><td>$bulkdis3a</td><td>$bulkdis4a</td><td>$bulkdis5a</td>";
       echo '</tr>';
       echo '</table>';
    ?>
    Thread Starter estuffs

    (@estuffs)

    Hi vtxyzzy, thanks, it worked.

    1. Regarding, $bulkqty1a = get_post_meta($post->ID, ‘_bulkdiscount_quantity_1’, true); how come we’re using $bulkqty1a, instead of $bulkqty1, without the “a”?

    2. Another question (for anyone), instead of showing the % discount, I want to show the value based on this calculation, $bulkdis1a-($bulkdis1a * $pricea), but that doesn’t work. Do I need to put this in the function.php file and somehow reference in this file?

    Thanks again.

    Thread Starter estuffs

    (@estuffs)

    Figured out item 2. Had to put the calculation outside the echo and then call the value in echo.

    For item 1, you used $bulkqty1a in the echo, so that is where to store the value retrieved by get_post_meta($post->ID, ‘_bulkdiscount_quantity_1’, true);).

    Before, when you were using ‘false’ instead of ‘true’ you were getting an array of values in $bulkqty1 and used the foreach loop to pull out the single $bulkqty1a value.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Displaying Custom Fields in a table’ is closed to new replies.