dongyi9330 2016-08-29 22:29 采纳率: 100%
浏览 69
已采纳

IF语句忽略第二个嵌套的IF语句

I wrote the following piece of code and the 2nd nested IF statement is being ignored. If I switch the order of them the same thing happens. I've tried an elseif statement and that doesn't seem to work either.

Any ideas why? I'm guessing it's a pretty noob question, so please skool me :)

<!-- Facebook Pixel Code -->
<script>
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','https://connect.facebook.net/en_US/fbevents.js');

fbq('init', '169969516705075');
fbq('track', "PageView");
<?php 

  if($_GET['order_total'] && $_GET['product_id']) {

    $order_total = $_GET['order_total'];
    $product_id = $_GET['product_id'];

    if($product_id === 8421 || 1925 || 1932) {
        $output = "fbq('track', 'Purchase', {value: '$order_total', currency: 'USD'});";
        echo $output;
    }
    if($product_id === 1647) {
        $free_output = "fbq('track', 'CompleteRegistration');";
        echo $free_output;
    }  
  }
  if (is_cart()) {
    $output = "fbq('track', 'AddToCart');";
    echo $output;
  }
  if(is_checkout()) {
    $output = "fbq('track', 'InitiateCheckout');";
    echo $output;
  }
  if (is_wc_endpoint_url( 'order-received' )) {
    $output = "fbq('track', 'Purchase', {value: '$order_total', currency: 'USD'});";
    echo $output;
  }

   ?></script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=169969516705075&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code --> 
  • 写回答

2条回答 默认 最新

  • dongshen7561 2016-08-29 23:04
    关注

    The other answers address the issue with your core problem - which is that PHP does not do what you think it does with this statement:

    if ($product_id == 8421 || 1925 || 1932) {...
    

    This would ALWAYS evaluate to TRUE, because you are saying:

    if $product_id == 8421 (which does a comparison as intended)
    OR
    1925 (which is not a comparison, but rather is a non-zero integer, which is "truthy")
    OR
    1932 (which is another non-zero integer, which is also "truthy")
    then...
    

    So, as a matter of coding style whenever there's more than a few values that I'm checking for, I like to use in_array:

    if ( in_array( $product_id, array( 1925, 1932, 8421 ) ) {...
    

    However, there's more that you're doing I'd like to help you with as well.

    WordPress best practices would not have you put that code in the top of your template file, but rather in a functions file.

    So, take that same code, but instead, alter it like so and place it in your theme's functions.php file.

    Note that it will get CALLED based on the wp_head hook (which I've also included below), which keeps your template file free from logic / functionality:

    // First, hook into the "wp_head" so that this is output in the head 
    add_action('wp_head','facebook_output');
    
    <?php  
    // This function is called by the 'wp_head' action above
    function facebook_output() { 
        // Close the PHP tag to output some script simply
        ?>
        <!-- Facebook Pixel Code -->
        <script>
            !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,document,'script','https://connect.facebook.net/en_US/fbevents.js');
    
            fbq('init', '169969516705075');
            fbq('track', "PageView");
    <?php  // Re-open the PHP tag so we can do our PHP logic
        if($_GET['order_total'] && $_GET['product_id']) {
    
            $order_total = $_GET['order_total'];
            $product_id = $_GET['product_id'];
    
            // For maintainability and readability, put the array(s) of values into variables
            $products_purchase = array(8421, 1925, 1932);
            $products_registration = array(1647);
    
            // Check if the product_id is in the array
            if( in_array( $product_id, $products_purchase ) ) {
                // No need to assign to $output and then echo - just echo
                echo "fbq('track', 'Purchase', {value: '$order_total', currency: 'USD'});";
            }
    
            if( in_array( $product_id, $products_registration ) ) {
                echo "fbq('track', 'CompleteRegistration');";
            }  
        }
    
        if ( is_cart() ) {
            echo "fbq('track', 'AddToCart');";
        }
    
        if( is_checkout() ) {
            echo "fbq('track', 'InitiateCheckout');";
        }
    
        if ( is_wc_endpoint_url( 'order-received' ) ) {
            echo "fbq('track', 'Purchase', {value: '$order_total', currency: 'USD'});";
        }
        // Close the PHP tag again to output the straight script
        ?>
        </script>
        <noscript><img height="1" width="1" style="display:none"
    src="https://www.facebook.com/tr?id=169969516705075&ev=PageView&noscript=1"
    /></noscript>
        <!-- End Facebook Pixel Code --> 
    <?php // And finally re-open the PHP tag
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?