运行代码时在网站上出现500错误

I have tried to write a code in order to change the price of the products dynamically when a user tries to change the quantity of the product. After placing the code in the functions file I am getting a 500 error on my website my website is not accessible. Can someone please check where am I wrong doing this.

function return_custom_price($price, $product) {       
    $product =wc_get_product( $post->ID );
    //global $post, $woocommerce;

    $price = get_post_meta($product->id, '_price', true);

    global $woocommerce;
    $items = $woocommerce->cart->get_cart();

    foreach ($items as $cart_item_key => $values) {

        $quantity = $values['quantity'];
        $cartProduct = $values['data'];

        if ($quantity < 10) 
            return $price;

        // do the ranges
        if ($quantity >= 10 && $quantity <= 24)
            return $product->get_attribute('10-24');

        if ($quantity >= 25 && $quantity <= 49)
            return $product->get_attribute('25-49');

        if ($quantity >= 50) // originally 50-99
            return $product->get_attribute('50-99');

        /* 
        if ($quantity >= 100 && $quantity <= 249)
            return $product->get_attribute('100-249');

        if ($quantity >= 250 && $quantity <= 499)
            return $product->get_attribute('250-499');

        if ($quantity >= 500 && $quantity <= 999)
            return $product->get_attribute('500-999');

        if ($quantity >= 1000 && $quantity <= 2499)
            return $product->get_attribute('1000-2499');

        if ($quantity >= 2500)
            return $product->get_attribute('2500');
         */

    }
    return $price;
}      
if (!is_admin() || (defined('DOING_AJAX') && DOING_AJAX)) {
    add_filter('woocommerce_get_price', 'return_custom_price', $product, 2);
}

The hook woocommerce_get_price is deprecated in WooCommerce 3+ and replaced by woocommerce_product_get_price

There is many errors in your code: You don't need to get the $product (WC_Product object) as it's already an argument in your function… You can get the price from the $product object…

So the correct code is:

add_filter('woocommerce_product_get_price ', 'return_custom_price', 10, 2);
function return_custom_price( $price, $product ){

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // $product object already exist as it's an argument in this hooked function

    // Get the current product price
    $price = method_exists( $product, 'get_price' ) ? $product->get_price() : $product->price;

    // Iterating though each cart items
    foreach (WC()->cart->get_cart() as $cart_item) {

        // Product quatity of the cart items
        $qty = $cart_item['quantity'];

        if ($qty > 50)
            $price = $product->get_attribute('50-99');
        elseif ($qty >= 25 && $qty < 50)
            $price = $product->get_attribute('25-49');
        elseif ($qty >= 10 && $qty < 25)
            $price = $product->get_attribute('10-24');
    }
    return $price;
}

BUT this code will not do what you are expecting:

Because it goes through each cart items (but not your current product), so that means that you will get many different quantities (related to each cart item) but not for your product as it's not yet added to cart. Same thing for the price that is returned many times (for each cart items)…


Dynamic pricing based on product quantities is a real development.

There is existing plugins for that like: