PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 9, 2022

[FIXED] How to target an array of Product IDs in WooCommerce?

 May 09, 2022     arrays, php, product, woocommerce, wordpress     No comments   

Issue

I'm using the code below to change a product price to text (Contact for Pricing) when the price field is left empty:

add_filter( 'woocommerce_get_price_html', 'change_special_product_price', 10, 2 );
function change_special_product_price( $price_html, $product ) {
    if ( $product->id == 6917 ) {
        $price_html = '<span class="woocommerce-Price-amount amount">Contact for Pricing</span>';
    }

    return $price_html;
}

add_filter('woocommerce_empty_price_html', 'custom_call_for_empty_price_html');
function custom_call_for_empty_price_html() {
    return 'TBC';
}

function dequeue_woocommerce_styles_scripts() {
    if ( function_exists( 'is_woocommerce' ) ) {
        if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {
        }
    }
}

It works for one product, but I'm having trouble getting it to work for multiple products.

How do I target an array of product ids?


Solution

For an array of product IDs use in_array() conditional function as follows:

function change_special_product_price( $price_html, $product ) {
    $product_ids = array(6917, 6918, 6921);

    if ( in_array($product->get_id(), $product_ids ) ) {
        $price_html = '<span class="woocommerce-Price-amount amount">' . __("Contact for Pricing") . '</span>';
    } 
    return $price_html;
}


Answered By - LoicTheAztec
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing