0

有两种不同数量和价格的产品

    simple product -1 :         qty      price
                                 2        150
                                 3        145
                                 5        130
                                 10      100


    simple product -2 :         qty      price
                                 2        195
                                 5        175
                                 9        170

我想以以下格式显示数量和价格:

qty : 2          3          5       9         10

     $150       $145       $130      -        $100

     $195         -        $175     $170       -  

下面是显示价格和数量的代码

/*
$_tResult = [2, 3, 5, 9, 2, 5, 10]
*/


     /*
$_tierPrice value


    array(10) {
      ["price_id"] => string(2) "18"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "150.0000"
      ["price_qty"] => float(2)
      ["website_price"] => string(8) "150.0000"
      ["formated_price"] => string(34) "150.00"
      ["savePercent"] => float(4)
      ["formated_price_incl_tax"] => string(34) "150.00"
    }

    array(10) {
      ["price_id"] => string(2) "65"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "120.0000"
      ["price_qty"] => float(3)
      ["website_price"] => string(8) "120.0000"
      ["formated_price"] => string(34) "120.00"
      ["savePercent"] => float(23)
      ["formated_price_incl_tax"] => string(34) "120.00"
    }

    array(10) {
      ["price_id"] => string(2) "61"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "145.0000"
      ["price_qty"] => float(5)
      ["website_price"] => string(8) "145.0000"
      ["formated_price"] => int(5) "145.00"
      ["savePercent"] => float(7)
      ["formated_price_incl_tax"] => string(34) "145.00"
    }

    array(10) {
      ["price_id"] => string(2) "62"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "130.0000"
      ["price_qty"] => float(9)
      ["website_price"] => string(8) "130.0000"
      ["formated_price"] => int(5) "130.00"
      ["savePercent"] => float(17)
      ["formated_price_incl_tax"] => string(34) "130.00"
    }

    array(10) {
      ["price_id"] => string(2) "47"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "190.0000"
      ["price_qty"] => float(2)
      ["website_price"] => string(8) "190.0000"
      ["formated_price"] => int(5) "190.00"
      ["savePercent"] => float(5)
      ["formated_price_incl_tax"] => string(34) "190.00"
    }

    array(10) {
      ["price_id"] => string(2) "63"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "175.0000"
      ["price_qty"] => float(5)
      ["website_price"] => string(8) "175.0000"
      ["formated_price"] => int(5) "175.00"
      ["savePercent"] => float(13)
      ["formated_price_incl_tax"] => string(34) "175.00"
    }

    array(10) {
      ["price_id"] => string(2) "64"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "195.0000"
      ["price_qty"] => float(9)
      ["website_price"] => string(8) "195.0000"
      ["formated_price"] => int(5) "195.00"
      ["savePercent"] => float(3)
      ["formated_price_incl_tax"] => string(34) "195.00"
    }

    array(10) {
      ["price_id"] => string(2) "44"
      ["website_id"] => string(1) "0"
      ["all_groups"] => string(1) "1"
      ["cust_group"] => int(32000)
      ["price"] => string(8) "170.0000"
      ["price_qty"] => float(10)
      ["website_price"] => string(8) "170.0000"
      ["formated_price"] => int(5) "170.00"
      ["savePercent"] => float(15)
      ["formated_price_incl_tax"] => string(34) "170.00"
    }
    */




 <?php $_item->setData('tier_price',null); ?>
          <?php $_tierPrices = $this->getTierPrices($_item); ?>
          <?php //print_r($_tResult); ?>
          <?php foreach ($_tierPrices as $price): ?>
                  <td>
                        <?php if(in_array($price['price_qty'],$_tResult)) :?>
                               <?php echo $price['formated_price']; ?>
                        <?php else: ?>
                               <?php echo "-"; ?>
                        <?php endif; ?>
                   </td>    
           <?php endforeach; ?>

下面是代码的实际输出。

qty :     2          3          5       9         10 

         $150       $145       $130    $100

         $195       $175       $170      

数组中没有价格值时如何添加“-”

4

2 回答 2

0

尝试这个:

<?php $_item->setData('tier_price',null); ?>
          <?php $_tierPrices = $this->getTierPrices($_item); ?>
          <?php //print_r($_tResult); ?>
          <?php foreach ($_tierPrices as $price): ?>
            <td>
               <?= (!empty($price['price'])) ? $price['formated_price']: "-"; ?>
            </td>    
           <?php endforeach; ?>

如果它不起作用:

代替:

<?= (!empty($price['price_qty'])) ? $price['formated_price']: "-"; ?>
with
<?= (!empty($price['formated_price'])) ? $price['formated_price']: "-"; ?>

评论或编辑您的问题。告诉我们什么是输出:

echo "<pre>"; print_r($price); echo "</pre>";
于 2013-03-13T09:41:00.710 回答
-1

您的代码中的主要逻辑错误是数组元素不等于上面的数字。

您需要做的是创建一个数组,其中包含[-]要显示的元素和键。

于 2013-03-13T09:40:51.570 回答