douxuexiao1748 2017-11-23 10:51
浏览 27

如何在woocommerce购物车中覆盖产品小计(行小计)价格?

I have custom products and i have added the products in the cart but it should be quantity based calculation for those products. how to change the product subtotal price in woocomerce. the product subtotal should override with my price. how to override it. Please check with my below code

 $values = array();
         $post_values = array();
         $i=0;
    foreach($_POST['post_quantity'] as $key =>$value){

            $values[] = "('{$_POST['product_id'][$key]}', '{$_POST['post_quantity'][$key]}', 
            '{$_POST['product_price'][$key]}', 
            '{$_POST['product_duplicate_price'][$key]}',
            '{$_POST['product_description'][$key]}',
            '{$_POST['product_material'][$key]}')";


$wpdb->insert('wp_posts', array(
      'post_title'    => 'xxxx '.$_POST['quoted_sid'].'Quote ID: '.$_POST['quoted_sid'][$key].'  - Product ID: '.$_POST['product_id'][$key],
      'post_content'  => $_POST['product_description'][$key],
      'post_date' => date('Y-m-d H:i:s'),
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_type'     =>'product'
    ));
    $lastid[$i] = $wpdb->insert_id; 

            $date[$i] = date('Y-m-d H:i:s');

            //calculation for quantity 

                $regular_price =    
                $_POST['product_price'][$key] + $_POST['product_duplicate_price'][$key]*($_POST['post_quantity'][$key]-1);

             //$regular_price /$_POST['post_quantity'][$key];

            //echo number_format((float)$regular_price,2,'.','').'</br>';

        add_post_meta($lastid[$i], '_regular_price', number_format((float)$regular_price,2,'.',''));
        add_post_meta($lastid[$i], '_price', number_format((float)$_POST['product_price'][$key],2,'.',''));
        add_post_meta($lastid[$i],'_visibility','visible');

    add_post_meta($lastid[$i], '_stock_status', 'instock' );
    add_post_meta($lastid[$i], '_weight', '11' );
    add_post_meta($lastid[$i], '_sku', 'Quoted Stencil_'.$_POST['product_id'][$key] );
    add_post_meta($lastid[$i], '_duplicate_price', $_POST['product_duplicate_price'][$key] );
    add_post_meta($lastid[$i], '_material', $_POST['product_material'][$key] );

    $post_values['product_id'][] = $lastid[$i];
    $post_values['product_quantity'][] = $_POST['post_quantity'][$key];
    $i++;
    }

    //print_r($post_values);




        $j=0;



        foreach ( $post_values as $product_id ) {


$woocommerce->cart->add_to_cart( $post_values['product_id'][$j] ,$post_values['product_quantity'][$j],$cart_item_data);
            $j++;
        }
    //exit;

     exit( wp_redirect( home_url( "cart" ) ) );
  • 写回答

1条回答 默认 最新

  • doubi1928 2017-11-24 08:40
    关注

    There is a filter called "woocommerce_cart_subtotal" for this. It is a filter for final string ( currency+subtotal+vat text). If you have some custom calculation for subtotal, inject it to this filter.

    First test it and make sure it works.

    add_filter('woocommerce_cart_subtotal','subtotalchanger',0);
    function subtotalchanger($subtotal){
        return $subtotal.' - it worked';
    }
    

    If everything is ok, then place your custom calculation to this filter and return your own subtotal:

    add_filter('woocommerce_cart_subtotal','subtotalchanger',0);
    function subtotalchanger($subtotal){
        //your own calculations based on cart products and their quantities
    return $your_custom_subtotal
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来