M
M
Mikhail Podroykin2016-01-26 09:40:55
WordPress
Mikhail Podroykin, 2016-01-26 09:40:55

How to cancel the work of a php function in one category?

Actually, here is the function of changing the price at the woocommerce rate:

add_action ('admin_menu', 'dynamic_price_button');
add_action ('woocommerce_get_price', 'change_price');
remove_action ('woocommerce_get_price', 'change_price2');
add_action( 'admin_init', 'register_mysettings' );

function register_mysettings () {
  register_setting( 'baw-settings-group', 'kurs' );
  register_setting( 'baw-settings-group', 'valuta' );
}

function change_price ($this) {
  $int = $this;
  $kurs=get_option('kurs');
  if ($kurs!='') {
    return $int*$kurs;}
    
  else  return  $int;
  
}
  
function dynamic_price_button () {
  add_submenu_page ('woocommerce', 'Курс сегодня', 'Курс сегодня', 'manage_options', 'dynamic_price', 'setting_page');
}

...

Next comes the input with get_option('kurs');
Determine the category for which you want to cancel the operation of the function - I can
if( has_term( '1489', 'product_cat' ,$product->ID) )

and here is how to apply a specific category to that piece of code
function change_price ($this) {
  $int = $this;
  $kurs=get_option('kurs');
  if ($kurs!='') {
    return $int*$kurs;}
    
  else  return  $int;
  
}

I don't understand. Tell.
If you insert a category at the beginning of the part of the code, then the changes according to the formula will only be in the category in which I say - that is:
function change_price ($this) {
        if( has_term( '1489', 'product_cat' ,$product->ID) ) /*категория */
  $int = $this;
  $kurs=get_option('kurs');
  if ($kurs!='') {
    return $int*$kurs;}
    
  else  return  $int;
  
}

and I need a function not to occur in it.
what am i doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mr Crabbz, 2016-01-26
@podroykin

function change_price ($this) {
        if( !has_term( '1489', 'product_cat' ,$product->ID) ) /*категория --вот тут в начале добавил !*/
  $int = $this;
  $kurs=get_option('kurs');
  if ($kurs!='') {
    return $int*$kurs;}
    
  else  return  $int;
  
}

S
Silm, 2016-01-26
@Silm

First, format the code humanly (read about codestyle).
Second, make sure that the category is NOT the one in which you want to disable the feature.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question