需要为来自functions.php的woocommerce商店页面产品定制定价 [英] Need custom Pricing for woocommerce shop page products from functions.php

查看:15
本文介绍了需要为来自functions.php的woocommerce商店页面产品定制定价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从functions.php 获取自定义定价.对于 woocommerce 商店页面,我希望输出如下.我创建了一个自定义分类(团队),我正在从后端应用自定义价格.

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.

例如:

 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.

我正在使用以下代码:

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 );

谢谢,萨提亚

推荐答案

一旦我更正了您的解析错误,您的评论中的代码就会起作用.

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;
}

修改它以检查特定分类术语中的产品,我们将只使用 has_term( $term, $taxonomy, $post ).您需要调整期限和税号.

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;
}

更新

阅读您的新问题,您似乎在解决这个问题(不过我仍然可能会误解您).如果产品 1 同时在团队 1 和团队 2 中,您可以使用 is_taxonomy("team-A") 更改分类页面上的价格,但所有 add_to_cart() 功能不知道使用正确的价格,因为没有办法根据条款来辨别它应该使用哪个价格(因为产品是两个条款).

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).

如果您想向不同的用户组显示不同的价格,那么您需要自定义用户功能并使用它来检查当前用户何时具有该上限.例如:current_user_can('team-b').

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').

这篇关于需要为来自functions.php的woocommerce商店页面产品定制定价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆