dzinee
dzinee

Reputation: 157

Get the Page ID when the displayed product as has a different ID in Woocommerce

I am trying to run a function specific for one product that has a different ID than the page where is displayed. To this I run a function from my child theme functions.php.

I have tried page_id, post id, is_page but I do not get it to work.

My page-id is 8405, product id is 837.

My latest code try is:

    // Remove price variation on single product page
add_action( 'woocommerce_before_single_product', 'my_remove_variation_price' );
function my_remove_variation_price() {
    if ( is_page('8405') ) {
  global $product;
  if ( $product->is_type( 'variable' ) ) {
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price' );
    }}
}

How can I get the Page ID which is different frm the displayed product ID?

Upvotes: 1

Views: 924

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253867

The following code will do the job and it will work specifically for your page ID:

// Remove price variation on single product page
add_action( 'woocommerce_single_product_summary', 'remove_variable_product_price', 1 );
function remove_variable_product_price() {
    global $wp, $product;

    $page = get_page_by_path($wp->request);

    if ( $product->is_type( 'variable' ) && $page->ID == 8405 ) {
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and work.

Upvotes: 1

Related Questions