【问题标题】:conditional doing both the if and else statement有条件地执行 if 和 else 语句
【发布时间】:2019-01-24 14:45:27
【问题描述】:

我有一个页面,您需要一个按钮来根据项目的数量启用或禁用。所以我做了一个 if 语句:

<?php
{

 global $woocommerce;   
 $targeted_id = 523;


 foreach ( WC()-&gt;cart-&gt;get_cart() as $cart_item ) 
 { 
  if($cart_item['product_id'] == $targeted_id )
  {
    $qty =  $cart_item['quantity'];
    break;
    }         

   }

  if ( $qty &gt; 0 )
  {  

    ?> <button 
    onclick="location.href='http://noespecadotiendagourmet.com/paso-3/'" 
    type="button">
    PASO SIGUIENTE</button> <?php


    }
   else
   {

    ?> <button 
    onclick="location.href='http://noespecadotiendagourmet.com/paso-3/'" 
    type="button" disabled> PASO SIGUIENTE</button> <?php         

    }




   ?> 

但由于某种原因,两个按钮是这样创建的:

编辑: 刚刚检查了来源,出于某种原因,除了按钮本身之外的所有内容都被评论了,我不明白为什么

【问题讨论】:

  • 购物车中有 2 件商品吗?其中一个打印第一个按钮,另一个打印第二个按钮?
  • &amp;gt; 的数量太多。是那些坏的糊状物还是从某个地方产生的?
  • 购物车中的两个项目将使 $qty 等于 2 @DirkScholten
  • 有或没有都没有关系,我已经设置所以它只能是 1 项,无论哪种方式它总是显示 2 个按钮
  • @FunkFortyNiner 我已经把它放回去了,因为我正在批准另一个编辑并认为它只是一个错字。

标签: php html wordpress woocommerce


【解决方案1】:

因为&amp;gt; 不是有效的PHP 比较运算符:您应该使用if ( $qty &gt; 0 )。

老实说,这段代码中还有几个其他 &amp;gt; 也会引发语法错误。

【讨论】:

  • 正是@splash58,它会在分号上抛出语法错误。老实说,一些 OP 的其他代码似乎使用了这个。
  • 我在页面内执行 php 部分,如果我使用 plain > 它认为我正在关闭 php 语句
  • 这毫无意义@leonidas56
  • @leonidas56 只有在查看 HTML 源代码时,&amp;gt; 才会显示为 &gt;。
  • 你需要告诉我们怎么做。在原始帖子中编辑您的代码。
【解决方案2】:

啊,现在我知道为什么它不起作用了,对不起大家的新手问题

最终使用 PHP 代码 sn-ps (Insert PHP) 插件来编写代码来完成它

global $woocommerce;    
$targeted_id = an id;
$qty = 0;



foreach ( WC()->cart->get_cart() as $cart_item ) 
{ 
if($cart_item['product_id'] == $targeted_id )
{
    $qty =  $cart_item['quantity'];
    break;
    }         

}

if ( $qty == 0 )
{  
 ?> 

     <form action="asite.com/" method="get">
     <input type="submit" value="next" 
     name="Submit" id="next" disabled/>
     </form>
     <?php  

}
else
{
 ?>   
      <form action="asite.com/" method="get">
     <input type="submit" value="next" 
     name="Submit" id="next" />
     </form> 
     <?php

 }    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-14
    • 2020-03-20
    • 2021-06-17
    相关资源
    最近更新 更多