WooCommerce Cart - Dynamic Price variable pass into custom price hook

只谈情不闲聊 提交于 2019-12-01 09:13:04

To make it work you just need to define $custom_price variables as global in your function, this way:

$custom_price = 200; 

add_action( 'woocommerce_before_calculate_totals', 'add_custom_item_price', 10, 1 );
function add_custom_item_price( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    global $custom_price;

    foreach (  $cart_object->get_cart() as $item_values ) {
        $item_values['data']->price = $custom_price;
    }
} 

This code is tested and working (for woocommerce versions 2.5+ and 2.6+).

Naturally this code goes on function.php file of your active child theme or theme.

To add Price in your woocommerce cart before calculate total -

 add_action( 'woocommerce_before_calculate_totals', 'add_custom_total_price');

  function add_custom_total_price($cart_object) {
     global $woocommerce,$add,$custom_price; 
     $add=200;//Dynamic Price variable
     $custom_price =$add;//Dynamic Price variable pass to custom price
    foreach ( $cart_object->cart_contents as $key => $value ) {
       $value['data']->price = $custom_price;
    }
 } 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!