Set programmatically product sale price and cart item prices in Woocommerce 3

前端 未结 4 1815
走了就别回头了
走了就别回头了 2021-01-28 06:26

This is the continuation of : Set product sale price programmatically in WooCommerce 3

The answer works, however once a user adds the product to cart, the old price stil

相关标签:
4条回答
  • 2021-01-28 07:08

    Hope this code is helpful for you

    add_filter( 'woocommerce_get_price_html', 'bbloomer_alter_price_display', 9999, 2 );
    
    function bbloomer_alter_price_display( $price_html, $product ) {
    
      // ONLY ON FRONTEND
      if ( is_admin() ) return $price_html;
    
      // ONLY IF PRICE NOT NULL
      if ( '' === $product->get_price() ) return $price_html;
    
      // IF CUSTOMER LOGGED IN, APPLY 20% DISCOUNT   
      if ( wc_current_user_has_role( 'customer' ) ) {
        $orig_price = wc_get_price_to_display( $product );
        $price_html = wc_price( $orig_price * 0.80 );
      }
      return $price_html;
    }
    
    add_action( 'woocommerce_before_calculate_totals', 'bbloomer_alter_price_cart', 9999 );
    
    function bbloomer_alter_price_cart( $cart ) {
    
      if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    
      if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
    
      // IF CUSTOMER NOT LOGGED IN, DONT APPLY DISCOUNT
      if ( ! wc_current_user_has_role( 'customer' ) ) return;
    
      // LOOP THROUGH CART ITEMS & APPLY 20% DISCOUNT
      foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $product = $cart_item['data'];
        $price = $product->get_price();
        $cart_item['data']->set_price( $price * 0.80 );
      }
    }
    
    0 讨论(0)
  • 2021-01-28 07:09

    The missing part to get it work for for cart and checkout pages (and also Orders and email notifications too) is a very simple trick:

    add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_sale_price', 20, 1 );
    function set_cart_item_sale_price( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        // Iterate through each cart item
        foreach( $cart->get_cart() as $cart_item ) {
            $price = $cart_item['data']->get_sale_price(); // get sale price
            $cart_item['data']->set_price( $price ); // Set the sale price
    
        }
    }
    

    Code goes in function.php file of your active child theme (active theme).

    Tested and works.

    So the code just set the product sale price as the product price in cart items and it works.

    0 讨论(0)
  • 2021-01-28 07:22

    @LoicTheAztec answers works very nicely, but is not required.

    You need to filter at least woocommerce_product_get_price and woocommerce_product_variation_get_price with the dynamic_sales_price_function.

    To make it work really smoothly, you need some more filters as well.

    0 讨论(0)
  • 2021-01-28 07:26

    The accepted answer did not work for me. Here is what worked:

    function get_active_price($price, $product) {
            if ($product->is_on_sale()) {
                return $product->get_sale_price();
            }
            return $product->get_regular_price();
        }
    
    add_filter('woocommerce_product_get_price', 'get_active_price'));
    

    This worked with custom sale and regular prices.

    0 讨论(0)
提交回复
热议问题