Current page ID on WooCommerce page in WordPress

I am using a WooCommerce plugin for WordPress. Now in a file page.php I am trying to get the current opened page id via $post->ID and the_id() but all I am getting is the product ID and not the current page ID. Probably because WooCommerce overwrites the loop ?

What can I do ?

Related posts

Leave a Reply

7 comments

  1. It might be too late, but i was trying to get the same result as you and this below worked pretty well for me:

    <?php 
      if(is_shop()){
        $page_id = woocommerce_get_page_id('shop');
      }else{
        $page_id = $post->ID;
      }
    ?>
    

    Hope this will help you.

  2. You can try this, inspired by woocommerce_product_archive_description

    add_action('woocommerce_after_main_content', 'custom_name', 11, 2); 
    
    function custom_name() {
      if ( is_post_type_archive( 'product' ) && 0 === absint( get_query_var( 'paged' ) ) ) :
    
        global $post;
        $shop_page = get_post( wc_get_page_id( 'shop' ) );
    
        if ( $shop_page ) {
          $post = $shop_page;
        }
        // your code here
    
      endif;
    }
    
  3. $woo_pages_id = array(
        //get_option( 'woocommerce_shop_page_id' ), 
        get_option( 'woocommerce_cart_page_id' ), 
        get_option( 'woocommerce_checkout_page_id' ), 
        //get_option( 'woocommerce_pay_page_id' ), 
        //get_option( 'woocommerce_thanks_page_id' ), 
        get_option( 'woocommerce_myaccount_page_id' ),
        //get_option( 'woocommerce_edit_address_page_id' ), 
        //get_option( 'woocommerce_view_order_page_id' ), 
        get_option( 'woocommerce_terms_page_id' )
    );
    
    $current_page_id = get_the_ID();
    
    if ( in_array($current_page_id, $woo_pages_id) ) : 
        get_template_part('content/woo', 'pages');
    else :
        get_template_part('content', 'page');
    endif;
    

    I’m using this for my page.php file.

  4. Had the same problem found a solution here
    The code I used on my shop page was:

    <?php echo woocommerce_get_page_id('shop'); ?>
    

    Basically replace the shop value with whatever woocommerce page you are trying to get.

    More documentation on the function and some other helpful functions can be found here