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

时间:2017-11-23 10:51:25

标签: php wordpress woocommerce

我有自定义产品,我已将产品添加到购物车中,但应该是基于数量的产品计算。如何改变woocomerce的产品小计价格。产品小计应该覆盖我的价格。如何覆盖它。请查看我的以下代码

 $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 个答案:

答案 0 :(得分:0)

有一个名为“woocommerce_cart_subtotal”的过滤器。它是最终字符串的过滤器(货币+小计+增值文本)。如果您有一些小计的自定义计算,请将其注入此过滤器。

首先测试它并确保它有效。

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

如果一切正常,请将自定义计算放入此过滤器并返回您自己的小计:

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