admin管理员组文章数量:1130349
I'm not good in WP and plugins and will need some help here. I've read that is possible to alter some WC core function with filters and action but probably I didn't understand this correctly. I want to simply delete the shipping price from my cart page Total price field.
The function which is in woocommerce/includes/class-wc-cart-totals.php is:
protected function calculate_totals() {
$this->set_total( 'total', round( $this->get_total( 'items_total', true ) + $this->get_total( 'fees_total', true ) + $this->get_total( 'shipping_total', true ) + array_sum( $this->get_merged_taxes( true ) ), 0 ) );
$this->cart->set_total_tax( array_sum( $this->get_merged_taxes( false ) ) );
// Allow plugins to hook and alter totals before final total is calculated.
if ( has_action( 'woocommerce_calculate_totals' ) ) {
do_action( 'woocommerce_calculate_totals', $this->cart );
}
// Allow plugins to filter the grand total, and sum the cart totals in case of modifications.
$this->cart->set_total( max( 0, apply_filters( 'woocommerce_calculated_total', $this->get_total( 'total' ), $this->cart ) ) );
}
I've tried to copy it to my child theme function.php file like this:
add_action('calculate_totals', 'my_calculate_totals');
function my_calculate_totals()
{
$this->set_total( 'total', round( $this->get_total( 'items_total', true ) + $this->get_total( 'fees_total', true ) + array_sum( $this->get_merged_taxes( true ) ), 0 ) );
$this->cart->set_total_tax( array_sum( $this->get_merged_taxes( false ) ) );
// Allow plugins to hook and alter totals before final total is calculated.
if ( has_action( 'woocommerce_calculate_totals' ) ) {
do_action( 'woocommerce_calculate_totals', $this->cart );
}
// Allow plugins to filter the grand total, and sum the cart totals in case of modifications.
$this->cart->set_total( max( 0, apply_filters( 'woocommerce_calculated_total', $this->get_total( 'total' ), $this->cart ) ) );
}
It doesn't work. Is this possible?
My goal is to just remove this from first line
$this->get_total( 'shipping_total', true )
UPDATE:
I have tried like this
add_action( 'woocommerce_calculate_totals', 'my_calculate_totals');
function my_calculate_totals($cart = null)
{
$cart = new WC_Cart_Totals();
$this->set_total( 'total', round( $this->get_total( 'items_total', true ) + $this->get_total( 'fees_total', true ) + array_sum( $this->get_merged_taxes( true ) ), 0 ) );
$this->cart->set_total_tax( array_sum( $this->get_merged_taxes( false ) ) );
}
Got this error
A valid WC_Cart object is required
I'm not good in WP and plugins and will need some help here. I've read that is possible to alter some WC core function with filters and action but probably I didn't understand this correctly. I want to simply delete the shipping price from my cart page Total price field.
The function which is in woocommerce/includes/class-wc-cart-totals.php is:
protected function calculate_totals() {
$this->set_total( 'total', round( $this->get_total( 'items_total', true ) + $this->get_total( 'fees_total', true ) + $this->get_total( 'shipping_total', true ) + array_sum( $this->get_merged_taxes( true ) ), 0 ) );
$this->cart->set_total_tax( array_sum( $this->get_merged_taxes( false ) ) );
// Allow plugins to hook and alter totals before final total is calculated.
if ( has_action( 'woocommerce_calculate_totals' ) ) {
do_action( 'woocommerce_calculate_totals', $this->cart );
}
// Allow plugins to filter the grand total, and sum the cart totals in case of modifications.
$this->cart->set_total( max( 0, apply_filters( 'woocommerce_calculated_total', $this->get_total( 'total' ), $this->cart ) ) );
}
I've tried to copy it to my child theme function.php file like this:
add_action('calculate_totals', 'my_calculate_totals');
function my_calculate_totals()
{
$this->set_total( 'total', round( $this->get_total( 'items_total', true ) + $this->get_total( 'fees_total', true ) + array_sum( $this->get_merged_taxes( true ) ), 0 ) );
$this->cart->set_total_tax( array_sum( $this->get_merged_taxes( false ) ) );
// Allow plugins to hook and alter totals before final total is calculated.
if ( has_action( 'woocommerce_calculate_totals' ) ) {
do_action( 'woocommerce_calculate_totals', $this->cart );
}
// Allow plugins to filter the grand total, and sum the cart totals in case of modifications.
$this->cart->set_total( max( 0, apply_filters( 'woocommerce_calculated_total', $this->get_total( 'total' ), $this->cart ) ) );
}
It doesn't work. Is this possible?
My goal is to just remove this from first line
$this->get_total( 'shipping_total', true )
UPDATE:
I have tried like this
add_action( 'woocommerce_calculate_totals', 'my_calculate_totals');
function my_calculate_totals($cart = null)
{
$cart = new WC_Cart_Totals();
$this->set_total( 'total', round( $this->get_total( 'items_total', true ) + $this->get_total( 'fees_total', true ) + array_sum( $this->get_merged_taxes( true ) ), 0 ) );
$this->cart->set_total_tax( array_sum( $this->get_merged_taxes( false ) ) );
}
Got this error
A valid WC_Cart object is required
本文标签: Override woocommerce wcclass function
版权声明:本文标题:Override woocommerce wc-class function 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749112208a2317582.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论