Need custom Pricing for woocommerce shop page prod

2019-04-17 21:56发布

I want to get custom pricing from functions.php. For woocommerce shop page i want the output as follows. I created a custom taxonamy(team), and i am applying a custom price from the backend.

Eg :

 1. Taxonomy : Team1
   a)  Product Id : 17,  Price =222;    //Original price $45
   b) Product Id :  18,  Price = 444;   // Original price $55

 2. Taxonomy : Team2
    a) Product Id : 17,  Price =999;    //Original price $45
    b) Product Id :  18,  Price = 888;  // Original price $55

How can i get the my example output by using the following code? If any changes or modification please let me know.

I am using the following code :

add_filter( 'woocommerce_get_regular_price', function ($price, $productd ) {
    foreach($_SESSION['my_array'] as $id => $price1) :

            $regular_price = get_post_meta(get_the_ID(), '_regular_price');
            $price = $regular_price[0];


        if(get_the_ID() == $id) :
        return $price1;
        else :

        return $price;

        endif;

    endforeach;

}, 10, 2 );

Thanks, Satya

1条回答
女痞
2楼-- · 2019-04-17 22:29

Once I corrected your parse errors, the code from your comment works.

add_filter('woocommerce_get_price','change_price', 10, 2); 
add_filter('woocommerce_get_regular_price','change_price', 10, 2); 
add_filter('woocommerce_get_sale_price','change_price', 10, 2); 
function change_price($price, $productd){ 
    if($productd->id == 16): 
        $price = 160;   
    endif;

    return $price;
}

Modifying that to check for products in a certain taxonomy term, we'll just use has_term( $term, $taxonomy, $post ). You will need to adjust the term and tax slugs.

add_filter('woocommerce_get_price','change_price', 10, 2); 
add_filter('woocommerce_get_regular_price','change_price', 10, 2); 
add_filter('woocommerce_get_sale_price','change_price', 10, 2); 
function change_price($price, $productd){ 
    if( has_term( 'team1', 'teams', $productd->id ) ): 
        $price = 160;   
    endif;

    return $price;
}

Update

Reading your new question, it seems like you are going about this wrong (I could still be misunderstanding you though). If Product 1 is in both Team 1 and Team 2, you could change the price on the taxonomy page using is_taxonomy("team-A") but all add_to_cart() functionality wouldn't know to use the correct price because there will be no way to discern which price it should use based on term (since the product is in both terms).

If you want to display different prices to different groups of Users, then you would need a custom user capability and use that to check when the current user has that cap or not..ex: current_user_can('team-b').

查看更多
登录 后发表回答