dowe98261 2012-03-13 07:35
浏览 18
已采纳

删除“echo”并将其替换为“return”?

I have the following code in place, however, I have to strip away all the styling (any css) and have it return rather than echo, otherwise it messes up with some other code I'm using. The other code I'm using echo's a shortcode that outputs the following determined by if there is items in the cart or not.

Obviously I can't echo (on the echo shortcode end of things), so I must return. If the cart is empty, it returns just fine as per my commented code below. It is when the cart has items, I can't get it to return :( my attempt is below.

//Original code, if there's items

if(is_object($cart) && $cart->countItems()) {
  ?>
  <div id="Cart66scCartContents" style="float:right; text-align: right;">
    <a id="Cart66scCartLink" href='<?php echo get_permalink($cartPage->ID) ?>'>
    <span id="Cart66scCartCount"><?php echo $cart->countItems(); ?></span>
    <span id="Cart66scCartCountText"><?php echo $cart->countItems() > 1 ? ' items' : ' item' ?></span> 
    <span id="Cart66scCartCountDash">&ndash;</span>
    <!-- <span id="Cart66scCartPrice"><?php //echo CART66_CURRENCY_SYMBOL . 
      number_format($cart->getSubTotal() - $cart->getDiscountAmount(), 2); ?> -->
    </span></a>
    <a id="Cart66scViewCart" href='<?php echo get_permalink($cartPage->ID) ?>'>View Cart</a>
    <span id="Cart66scLinkSeparator"> | </span>
    <a id="Cart66scCheckout" href='<?php echo get_permalink($checkoutPage->ID) ?>'>Check out</a>
  </div>
  <?php
}
else {

//My code, if there's no items (which works perfectly as a return)

    $emptyMessage = isset( $attrs['empty_msg'] ) ? $attrs['empty_msg'] : 'Your cart is empty';
    return "<p id=\"Cart66scEmptyMessage\" style=\"float:right; text-align: right;\">" . $emptyMessage . "</p>";

}

My attempt on setting the "if there's items in the cart" to a return rather than echo..

if(is_object($cart) && $cart->countItems()) {      
  return "<a href='" . get_permalink($cartPage->ID) . "'>" . $cart->countItems(); . " " . $cart->countItems() > 1 ? ' items' : ' item' . "&ndash;" . number_format($cart->getSubTotal() - $cart->getDiscountAmount(), 2); . "</a> <a href='" . get_permalink($cartPage->ID) . "'>View Cart</a> | <a href='" . get_permalink($checkoutPage->ID) . "'>Check out</a>";
}
else {
    $emptyMessage = isset( $attrs['empty_msg'] ) ? $attrs['empty_msg'] : 'Your cart is empty';
    return "<p id=\"Cart66scEmptyMessage\" style=\"float:right; text-align: right;\">" . $emptyMessage . "</p>";

}

The text "1 item – View Cart | Check out" is not showing up with my attempt. What have I done wrong?

Thank you!!

  • 写回答

4条回答 默认 最新

  • douli4852 2012-03-13 07:38
    关注

    You have a semicolon at . $cart->countItems();, remove that and you should be good to go

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题