Change all product prices from a specific product

2019-07-14 19:33发布

I used a function to be able to markup all product automatically without having to use SQL or manually do it as they get re-uploaded and updated very frequently.

The function works perfectly other than the has_term if statements. They work in the Wordpress backend and apply the pricing rules as soon as I add the tag, but when I try to add the product to cart it reverts back to the original price in the cart and checkout pages. If I remove that if statement it works with no issues. I need a way to have the function only apply to products tagged 'ama'.

// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'custom_price', 90, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 90, 2 );

// Product variations (of a variable product)
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 90, 2 );

// Variable product price range
add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 90, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 90, 3 );

function custom_price( $price, $product ) {

    if ( has_term( 'ama', 'product_tag' ) ) {

        if ($price > 0.01 && $price < 4.99) {

            $price *= 2.5;  

            $price = ceil($price + 0.01) - 0.01;

        }

        elseif ($price > 5 && $price < 9.99) {

            $price *= 2;  

            $price = ceil($price + 0.01) - 0.01;

        }

        elseif ($price > 10 && $price < 19.99) {

            $price *= 1.75;  

            $price = ceil($price + 0.01) - 0.01;

        }

        elseif ($price > 20 && $price < 39.99) {

            $price *= 1.5;  

            $price = ceil($price + 0.01) - 0.01;

        }

        elseif ($price > 40 && $price < 59.99) {

            $price *= 1.35;  

            $price = ceil($price + 0.01) - 0.01;

        }

        elseif ($price > 60 && $price < 79.99) {

            $price *= 1.25;  

            $price = ceil($price + 0.01) - 0.01;

        }

        elseif ($price > 80 && $price < 999.99) {

            $price *= 1.20;  

            $price = ceil($price + 0.01) - 0.01;

        }

    }

    return $price;
}

function custom_variation_price( $price, $variation, $product ) {

    if ( has_term( 'ama', 'product_tag' ) ) {

        if ($price > 0.01 && $price < 4.99) {

            $price *= 2.5;  

            $price = ceil($price + 0.01) - 0.01;

        }

        elseif ($price > 5 && $price < 9.99) {

            $price *= 2;  

            $price = ceil($price + 0.01) - 0.01;

        }

        elseif ($price > 10 && $price < 19.99) {

            $price *= 1.75;  

            $price = ceil($price + 0.01) - 0.01;

        }

        elseif ($price > 20 && $price < 39.99) {

            $price *= 1.5;  

            $price = ceil($price + 0.01) - 0.01;

        }

        elseif ($price > 40 && $price < 59.99) {

            $price *= 1.35;  

            $price = ceil($price + 0.01) - 0.01;

        }

        elseif ($price > 60 && $price < 79.99) {

            $price *= 1.25;  

            $price = ceil($price + 0.01) - 0.01;

        }

        elseif ($price > 80 && $price < 999.99) {

            $price *= 1.20;  

            $price = ceil($price + 0.01) - 0.01;

        }

    }

    return $price;
}

1条回答
狗以群分
2楼-- · 2019-07-14 20:29

I have revisited your code… I have merged the pricing updates in a separated utility function…

To avoid your problems is necessary to define the product ID (checking the product type before).
Then we set this correct defined product ID in WordPress has_term() conditional function.

Now your prices will work on cart and checkout pages too…

Your revisited code:

// Utility pricing function
function filtering_product_prices( $price, $product ) {
    // Get the product ID
    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    // Only for Woocomerce Product Tag "ama"
    if ( ! has_term( 'ama', 'product_tag', $product_id ) ) return $price; // Exit

    if ( $price < 5 ) {
        $price *= 2.5;
    } elseif ( $price >=  5 && $price < 10 ) {
        $price *= 2;
    } elseif ( $price >= 10 && $price < 20 ) {
        $price *= 1.75;
    } elseif ( $price >= 20 && $price < 40 ) {
        $price *= 1.5;
    } elseif ( $price >= 40 && $price < 60 ) {
        $price *= 1.35;
    } elseif ( $price >= 60 && $price < 80 ) {
        $price *= 1.25;
    } elseif ( $price >= 80 && $price < 1000 ) {
        $price *= 1.20;
    }
    return ceil($price + 0.01) - 0.01;
}

// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'custom_price', 90, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 90, 2 );

// Product variations (of a variable product)
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 90, 2 );

function custom_variation_price( $price, $variation, $product ) {
    return filtering_product_prices( $price, $product );
}

// Variable product price range
add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 90, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 90, 3 );

function custom_price( $price, $product ) {
    return filtering_product_prices( $price, $product );
}

Code goes in function.php file of your active child theme (or active theme). Tested and work.

Related answer: Conditional product prices cart issue in WooCommerce 3

查看更多
登录 后发表回答