woocommerce / theme-customisations

313 stars 110 forks source link

Issue with custom function #25

Open xxedgexx opened 5 years ago

xxedgexx commented 5 years ago

After updating to woo 3.5.4, I noticed in the dashboard, only one of my products in my catalog was returning:

https://snag.gy/iDEmgy.jpg

It turns out this function, which I copied from some other site, was causing problems. The purpose of the function is to display the discounted price when a coupon code is applied:

add_filter('woocommerce_get_price_html', 'woocommerce_get_price_html', 10, 2 ); function woocommerce_get_price_html( $price, $product ) {

if ( WC()->cart->has_discount() ) {

    $values = array (
        'data'      => $product,
        'quantity'  => 1
    );

    $coupons = WC()->cart->get_coupons();

    $_price = $product->get_price();
    $undiscounted_price = $_price;

    if ( ! empty( $coupons ) ) {

        foreach ( $coupons as $code => $coupon ) {

            if ( $coupon->is_valid() && ( $coupon->is_valid_for_product( $product, $values ) || $coupon->is_valid_for_cart() ) ) {
                $discount_amount = $coupon->get_discount_amount( 'yes' === get_option( 'woocommerce_calc_discounts_sequentially', 'no' ) ? $_price : $undiscounted_price, $values, true );
                $discount_amount = min( $_price, $discount_amount );
                $_price          = max( $_price - $discount_amount, 0 );
            }

            if ( 0 >= $_price ) {
                break;
            }
        }
        if ( ( $product->get_price() > 0 ) && ( $undiscounted_price !== $_price ) )
            $price = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $undiscounted_price ) ), $_price ) . $product->get_price_suffix();
    }

}

return $price;

}

Any obvious issue or changes that need to be made after the 3.5.4 update?