user1736479
user1736479

Reputation: 245

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 ?

Upvotes: 3

Views: 28654

Answers (7)

Sanjay KS
Sanjay KS

Reputation: 31

if you are trying to get the particular page id. Try with this.

if ( wc_get_page_id( 'name of page' ) != get_the_ID()): 

This worked fine for me

Upvotes: 3

sakarya
sakarya

Reputation: 393

$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.

Upvotes: 0

Erol Abzait
Erol Abzait

Reputation: 1

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;
}

Upvotes: 0

IqbalBary
IqbalBary

Reputation: 1126

global $post;$post->ID; or global $wp_query;$wp_query->post->ID; don't work with woocommerce. You can get all the id's of woocommerce page here

Upvotes: 1

gknf
gknf

Reputation: 121

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.

Upvotes: 12

Xtremefaith
Xtremefaith

Reputation: 947

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

Upvotes: -1

Krunal Shah
Krunal Shah

Reputation: 2101

put this line above your code or at starting of script.

global $post;

now you can use $post->ID;

thanks

Upvotes: -1

Related Questions