简体   繁体   English

可变产品的价格(woocommerce)

[英]Price of variable products (woocommerce)

Tell me please.请告诉我。 Where does the basket get the value of the variational product, with the subsequent multiplication by the number and the issuance of the TOTAL and the SUBTOTAL ?篮子在哪里获得变分乘积的价值,随后乘以数字并发出TOTALSUBTOTAL The problem is that you need to send the cost of the products from an custom field to the basket.问题是您需要将产品的成本从自定义字段发送到篮子。

For a simple product, everything turned out.对于一个简单的产品,一切都变成了。 The following function was added to the file abstract-wc-product.php :以下函数已添加到文件abstract-wc-product.php 中

public function get_rrp_price( $context = 'view' ) {
    return $this->get_meta( $key = 'rrp_price', $context = 'view' );
}

Where rrp_price - is the name of an arbitrary field with a new price.其中rrp_price - 是具有新价格的任意字段的名称。 Then in the template class-wc-cart.php , namely in the following functions:然后在模板class-wc-cart.php 中,即在以下函数中:

public function calculate_totals(
public function get_product_subtotal(

I am replaced $product->get_price() with $product->get_rrp_price() and for a simple product it all worked!我用$product->get_rrp_price()替换了$product->get_price()并且对于一个简单的产品,这一切都有效!

But when you add a variable product, the basket issues ZERO .但是当您添加可变产品时,篮子会发出 I can not understand in what place it can be fixed, I guess that in class-wc-product-variable.php , but if so, where and how?我不明白它可以在什么地方修复,我猜是在class-wc-product-variable.php 中,但如果是这样,在哪里以及如何修复?

I have faced this problem before but i found the solution please check below code i think from that you can get perfect idea.我以前遇到过这个问题,但我找到了解决方案,请检查下面的代码,我认为您可以从中获得完美的想法。

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

I've tried to this (changed 'dist' and 'rest' for 'rrp_price' ):我已经尝试过(为'rrp_price'更改了'dist''rest ' ):

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, 'rrp_price',true);}
    else if($role == 'reseller'){$pricex = get_post_meta( $variation->variation_id, 'rrp_price',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 );

But variable products still ZERO in a cart.但是购物车中的可变产品仍然为零

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM