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条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。