dongnan1989 2017-07-20 10:05
浏览 280

Woocommerce根据数量和变化改变价格

I am currently creating my first site using woocommerce for a client, and they are requesting for me to have a price increase per variation and quantity, however i am not sure how to do it. Here is an example of what they are asking for:

Product Type: Limestone

Variations:

  • Product Size:
    • 10kg bag
    • 20kg bag
    • 40kg bag
  • Quantity:
    • 1-20

TL;DR if the customer wants multiple 10kg bags, the price increases by £36 at a time, if they want multiple 20kg bags, the price increases by £30 at a time, and if they want multiple 40kg bags, the price doubles every time.

I have used woocommerce variations and attributes so far but now it would require modifying having 3x20 variations all of which require a different price, which would be easier to change if a statement is possible as follows:

if ($productsize === '10kg') {
   $quantity = 2; //get quantity from textbox $_POST
   $initial_price = 36; //get inital price from woocommerce global variable

   $total_price = $inital_price * $quantity;

   update_price($total_price); //a function to update the price in woocommerce when adding to cart

}

Obviously the code above does not use any woocommerce variables, however doing this for only 3 variations would be easier than manually entering 60 variations on one product for which we have >60 on our site.

Thanks in advance for any help.

  • 写回答

1条回答 默认 最新

  • douluoqiu4538 2018-06-28 12:28
    关注

    I don't know if you need it anymore, however try it

    add_action( 'woocommerce_before_calculate_totals', 'IG_recalculate_price',5,1 );
    
    function IG_recalculate_price( $cart_object ) {
    
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
        $quantity = 0;
    
    
        foreach ( $cart_object->get_cart() as $key => $value ) {
                // count q.ty
                $quantity += $value['quantity'];
             // delta q.ty  
            if( $quantity > 24 ) {
               // get price by custom field but you can use a simple var
                $newprice = get_post_meta( $value['data']->get_id(), 'custom_field2', true);
                $value['data']->set_price( $newprice );
               // reset q.ty for check every item in the cart
                $quantity = 0;
    
                        }else{
    
                $newprice = get_post_meta( $value['data']->get_id(), 'custom_field', true);
                $value['data']->set_price( $newprice );
                $quantity = 0;
    
                }
            }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题