【问题标题】:How to check that we are not in a Woocommerce endpoint如何检查我们不在 Woocommerce 端点中
【发布时间】:2014-04-24 11:06:51
【问题描述】:

因为 WooCommerce 2.1 页面(例如 order-received)已被删除并替换为 WC 端点。我的结帐页面有一个自定义页面模板 (page-checkout.php),现在所有结帐端点也在使用这个自定义页面模板。

仅当我的客户位于 /checkout/ 页面时,我才需要修改页眉和页脚,但我想在他们位于结帐端点时显示不同的内容。我发现这是有条件的:

if(is_wc_endpoint_url("order-received")) echo "yes";

当我们处于“订单已收到”结帐端点时,它可以工作。但是我正在寻找一种条件逻辑,它可以告诉我我们何时不在端点中,例如:

if(!is_wc_endpoint()) echo "yes";

谢谢。

【问题讨论】:

    标签: woocommerce endpoints


    【解决方案1】:

    这些问题似乎得到了回答,而且有点老了。但我找到了一个更好的解决方案,这可能对其他人有所帮助。

    您可以使用以下功能。 Documentation

    is_wc_endpoint_url()
    

    如果不带参数使用,它将针对所有端点检查当前 url。如果一个端点被指定为

    is_wc_endpoint_url('edit-account');
    

    它将检查 url 是否属于特定端点。

    【讨论】:

      【解决方案2】:

      这是 is_wc_endpoint_url 函数的一个从不版本,将包含在未来的 woocommerce 版本中。因此,只需给它一个不同的名称,然后将其放入您的 functions.php 中。

      function is_wc_endpoint_url( $endpoint = false ) {
       global $wp;
      
       $wc_endpoints = WC()->query->get_query_vars();
      
       if ( $endpoint ) {
           if ( ! isset( $wc_endpoints[ $endpoint ] ) ) {
               return false;
           } else {
               $endpoint_var = $wc_endpoints[ $endpoint ];
           }
      
           return isset( $wp->query_vars[ $endpoint_var ] );
       } else {
           foreach ( $wc_endpoints as $key => $value ) {
               if ( isset( $wp->query_vars[ $key ] ) ) {
                   return true;
               }
           }
           return false;
       }
      }
      

      【讨论】:

      • 该功能已经开始工作了。我正在使用它: if(is_wc_endpoint_url("order-received")) $thanks-page=1;
      【解决方案3】:

      试试下面的功能:

      function is_wc_endpoint() {
          if ( empty( $_SERVER['REQUEST_URI'] ) ) return false;
          $url = parse_url( $_SERVER['REQUEST_URI'] ); 
          if ( empty( $url['query'] ) ) return false;
          global $wpdb;
          $all_woocommerce_endpoints = array();
          $results = $wpdb->get_results( "SELECT option_name, option_value FROM {$wpdb->prefix}options WHERE option_name LIKE 'woocommerce_%_endpoint'", 'ARRAY_A' );
          foreach ( $results as $result ) {
              $all_woocommerce_endpoints[$result['option_name']] = $result['option_value'];
          }
          foreach ( $all_woocommerce_endpoints as $woocommerce_endpoint ) {
              if ( strpos( $url['query'], $woocommerce_endpoint ) !== false ) {
                  return true;
              }
          }
          return false;
      }
      

      希望它能给你带来你期望的结果。

      【讨论】:

      • 非常感谢,它有效。但是我认为它需要太多查询,不是吗?不存在更简单的条件吗?
      【解决方案4】:

      我尝试了默认的WC功能:

      is_wc_endpoint_url('my-custom-endpoint');
      

      但它总是对我返回 false。所以我创建了自己的函数:

      function yourtheme_is_wc_endpoint($endpoint) {
          // Use the default WC function if the $endpoint is not provided
          if (empty($endpoint)) return is_wc_endpoint_url();
          // Query vars check
          global $wp;
          if (empty($wp->query_vars)) return false;
          $queryVars = $wp->query_vars;
          if (
              !empty($queryVars['pagename'])
              // Check if we are on the Woocommerce my-account page
              && $queryVars['pagename'] == 'my-account'
          ) {
              // Endpoint matched i.e. we are on the endpoint page
              if (isset($queryVars[$endpoint])) return true;
              // Dashboard my-account page special check - check whether the url ends with "my-account"
              if ($endpoint == 'dashboard') {
                  $requestParts = explode('/', trim($wp->request, ' \/'));
                  if (end($requestParts) == 'my-account') return true;
              }
          }
          return false;
      }
      

      例子:

      yourtheme_is_wc_endpoint('my-custom-endpoint');
      

      或者:

      yourtheme_is_wc_endpoint('edit-account');
      

      【讨论】:

        猜你喜欢
        • 2017-05-24
        • 2019-09-12
        • 2017-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-19
        • 1970-01-01
        相关资源
        最近更新 更多