2

What is wrong in this code? I have some issue with this code.

function my_woocommerce_get_price($price, $_product) {
         $kurs = 1.67;
         $new_price = $price / $kurs;       
         $product_categories = array('laminat');
         if( has_term( $product_categories, 'product_cat', $product->get_id() ) ) {
                 return $new_price; // новая цена
         } else {
                 return $price; 
         }
}
add_filter('woocommerce_get_price', 'my_woocommerce_get_price', 100, 2);

I had this Error when I try to save code.

Uncaught Error: Call to a member function get_id() on null in wp-content/themes/mrDoor/functions.php:128
Stack trace:
#0 wp-includes/class-wp-hook.php(287): my_woocommerce_get_price('919', Object(WC_Product_Simple))
#1 wp-includes/plugin.php(255): WP_Hook->apply_filters('919', Array)
#2 wp-content/plugins/woocommerce/includes/class-wc-deprecated-filter-hooks.php(142): apply_filters_ref_array('woocommerce_get...', Array)
#3 wp-content/plugins/woocommerce/includes/class-wc-deprecated-filter-hooks.php(129): WC_Deprecated_Filter_Hooks->trigger_hook('woocommerce_get...', Array)
#4 wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-deprecated-hooks.php(75): WC_Deprecated_Filter_Hooks->handle_deprecated_hook('woocommerce_pro...', 'woocommerce_get...', Array, '919')
#5 wp-include
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    Describe *"some issue"*, then we can help you. If you have an error message, provide the full error message and the corresponding line. – Definitely not Rafal Dec 18 '20 at 10:06
  • 2
    `$product->get_id()` should be `$_product->get_id()` **OR** `$_product` should be `$product`. In short, these are not equal and hence you will receive an error message. As indicated in the commentary above, please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [Stack Overflow question checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). Then write your question – 7uc1f3r Dec 18 '20 at 10:12
  • I like how you write `новая цена` after `$new_price`... they both mean the same ... – KIKO Software Dec 18 '20 at 10:17
  • try to print value of $_product and as mentioned by @7uc1f3r , it will be $_product->get_id(). $_product must not be null. – armin Dec 18 '20 at 10:36
  • The hook you are using is obsolete and deprecated *(see the answer below to change prices for specific product category)* – LoicTheAztec Dec 18 '20 at 12:55

1 Answers1

2

There are multiple mistakes in your code and since WooCommerce 3, the hook woocommerce_get_price is obsolete, deprecated and replaced by:

  • woocommerce_product_get_price for product post type
  • woocommerce_product_variation_get_price for product_variation post type

Based on Change product prices via a hook in WooCommerce 3+, here is the way to change product prices for specific product category (that requires much more code):

// Simple, grouped and external products (and product variations)
add_filter( 'woocommerce_product_get_price', 'custom_product_price', 100, 2 );
add_filter( 'woocommerce_product_get_regular_price', 'custom_product_price', 100, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'custom_product_price', 100, 2 );
add_filter( 'woocommerce_product_variation_get_regular_price', 'custom_product_price', 100, 2 );
function custom_product_price( $price, $product ) {
    $category_terms = array('laminat');

    // Get the parent variable product for product variations
    $product_id     = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id();

    if( has_term( $category_terms, 'product_cat', $product_id ) ) {
        $price *= 1.67; // новая цена
    }
    return $price;
}

// Variable products (price range)
add_filter('woocommerce_variation_prices_price', 'custom_variable_price_range', 100, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variable_price_range', 100, 3 );
function custom_variable_price_range( $price, $variation, $product ) {
    $category_terms = array('laminat');
    
    if( has_term( $category_terms, 'product_cat', $product->get_id() ) ) {
        $price *= 1.67; // новая цена
    }
    return $price;
}

// Handling price caching, for better performances
add_filter( 'woocommerce_get_variation_prices_hash', 'custom_variable_product_prices_hash', 100, 3 );
function custom_variable_product_prices_hash( $price_hash, $product, $for_display ) {
    $category_terms = array('laminat');
    
    if( has_term( $category_terms, 'product_cat', $product->get_id() ) ) {
        $price_hash[] = '1.67';
    }
    return $price_hash;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Related: Change product prices via a hook in WooCommerce 3+

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399