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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么