How to exclude product category from php code in woocommerce

Viewed 113

I'm using this piece of code to display the range price of variable product from € - €€€ to Starting From €. I'm taking the max price and divide it by 10 because my max variation is a pack of 10.

add_filter( 'eha_variable_sale_price_html',
'pugliami_variation_price_format_min', 9999, 4 );

function pugliami_variation_price_format_min( $price, $min_price, $maxprices, $regular_price ) {

    $maxprices = sprintf( __( 'Starting from %1$s', 'woocommerce' ),
    wc_price( $maxprices /10) );

    return $maxprices ;
}

But the problem with this code is that if I've a variable product with volume instead of pack this will take the highest price of the volume and divide it by 10 and I don't want it, I want only variable product with packs.

product with pack : https://poppersplanet.com/produit/rush-original-10ml/?lang=en

product without pack : https://poppersplanet.com/produit/lubricant-gel-x-man-silicone/?lang=en

Does there is a way to remove special product for a php code or a way to use this code only for a product category.

Thx in advance for any help :)

1 Answers

It looks like you're using the ELEX WooCommerce Dynamic Pricing and Discounts Plugin or similar, as eha_variable_sale_price_html is not a standard WooCommerce hook name.

In any case, you should be able to access the global $product object, which you can query to get its id or its product categories. Based on that, you can then build your check to determine whether you need to divide the max price by 10 or not.

For example, a criteria based on the product category would look like this (you will need to adapt the Quantity Product category name to your case):

add_filter( 'eha_variable_sale_price_html', 'pugliami_variation_price_format_min', 9999, 4 );

function pugliami_variation_price_format_min( $price, $min_price, $maxprices, $regular_price ) {
  global $product;
  $categories = get_the_terms( $product->get_id(), 'product_cat' );
  $is_quantity_product = false;
  foreach ( $categories as $category ) {
    if ( 'Quantity Product' === $category->name ) {
      $is_quantity_product = true;
      break;
    }
  }
  if ( $is_quantity_product ) {
    return sprintf( __( 'Starting from %1$s', 'woocommerce' ), wc_price( $maxprices / 10 ) );
  }
  return sprintf( __( 'Starting from %1$s', 'woocommerce' ), wc_price( $min_price ) );
}

Similarly, you can check if the id of the product is included in an array of ids of products whose price is based on quantity.

Related