Woocommerce如何在颜色选择上显示产品变化价格

时间:2017-12-19 10:15:40

标签: php wordpress woocommerce

我正在使用Porto WordPress主题。如果访客选择不同的颜色,我想显示产品变化价格,每种颜色都有价格。例如。

产品正常价格: 常规:200美元 销售:180美元

在黑色选择上:这将额外增加20美元 正常价格:220美元 销售价格:200美元

我尝试了很多插件,但它无法正常工作。

示例页面:https://www.leatherjacketmaster.com/mens-leather-jackets/mens-biker-leather-jackets/elegant-men-s-red-leather-jacket-voteporix

如果您点击任何颜色,它将更新价格并额外增加20美元,并且还有通过。

如何在WordPress中实现该功能?

1 个答案:

答案 0 :(得分:1)

您可以从以下挂钩中应用自定义价格,请查看下面的代码示例:

function opal_varient_price( $price, $variation ) {

    if (  $variation->product_type == 'variation'  ) {

        $user = $user ? new WP_User( $user ) : wp_get_current_user();
        $role = $user->roles[0];
        if($role == 'detailer'){$pricex = get_post_meta( $variation->variation_id, 'dist',true);}
        else if($role == 'reseller'){$pricex = get_post_meta( $variation->variation_id, 'res',true);}  
        else{ $pricex = $price;}
        }
    return $pricex;
    }

    add_filter( 'woocommerce_product_variation_get_regular_price', 'opal_varient_price' , 99, 2 );
    add_filter( 'woocommerce_product_variation_get_sale_price', 'opal_varient_price' , 99, 2 );
    add_filter( 'woocommerce_product_variation_get_price', 'opal_varient_price', 99, 2 );
相关问题