Conditional product prices cart issue in WooCommerce 3

前端 未结 1 1730
深忆病人
深忆病人 2021-01-23 00:42

I modified a function to create custom prices for some of my members i.e. the normal price is $1 but if you\'re a bronze member it\'s $2, a silver member $3, etc.

The pr

相关标签:
1条回答
  • 2021-01-23 01:10

    With your code you are just changing the displayed variation price range. So you will need a bit more for this:

    // 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 ramge
    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 ) {
        // Only logged in users
        if ( ! is_user_logged_in() ) return $price; 
    
        // HERE the defined plan ID
        $plan_id = 1628;
    
        if ( wc_memberships_is_user_member( get_current_user_id(), $plan_id )  ) {
            $price *= 2; // set price x 2
        }
        return $price;
    }
    
    function custom_variation_price( $price, $variation, $product ) {
        // Only logged in users
        if ( ! is_user_logged_in() ) return $price; 
    
        // HERE the defined plan ID
        $plan_id = 1628;
    
        if ( wc_memberships_is_user_member( get_current_user_id(), $plan_id )  ) {
            $price *= 2; // set price x 2
        }
        return $price;
    }
    

    Code goes in function.php file of the active child theme (or active theme).

    Tested and works on woocommerce 3+

    Now custom prices in cart will be also reflected

    0 讨论(0)
提交回复
热议问题