• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wpseo-woocommerce / e461cd0639ed27e4f03947e76d5a18250ab665c2

16 Apr 2025 01:18PM UTC coverage: 51.845% (-3.3%) from 55.168%
e461cd0639ed27e4f03947e76d5a18250ab665c2

Pull #1053

github

web-flow
Merge 2570a097a into d6b2c99fb
Pull Request #1053: Drop compatibility with PHP 7.2 and 7.3

843 of 1626 relevant lines covered (51.85%)

4.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.73
/classes/woocommerce-utils.php
1
<?php
2
/**
3
 * WooCommerce Yoast SEO plugin file.
4
 *
5
 * @package WPSEO/WooCommerce
6
 */
7

8
/**
9
 * Class WPSEO_WooCommerce_Utils
10
 */
11
class WPSEO_WooCommerce_Utils {
12

13
        /**
14
         * Searches for the primary terms for given taxonomies and returns the first found primary term.
15
         *
16
         * @param array      $brand_taxonomies The taxonomies to find the primary term for.
17
         * @param WC_Product $product          The WooCommerce Product.
18
         *
19
         * @return string The term's name (if found). Otherwise an empty string.
20
         */
21
        public static function search_primary_term( array $brand_taxonomies, $product ) {
4✔
22
                foreach ( $brand_taxonomies as $taxonomy ) {
4✔
23
                        $primary_term       = new WPSEO_Primary_Term( $taxonomy, $product->get_id() );
4✔
24
                        $found_primary_term = $primary_term->get_primary_term();
4✔
25

26
                        if ( $found_primary_term ) {
4✔
27
                                $term = get_term_by( 'id', $found_primary_term, $taxonomy );
2✔
28

29
                                return $term->name;
2✔
30
                        }
31
                }
32

33
                return '';
2✔
34
        }
35

36
        /**
37
         * Get the product display price, using the correct decimals, and tax setting.
38
         *
39
         * @param WC_Product $product The product we're retrieving the price for.
40
         *
41
         * @return string Price ready for display.
42
         */
43
        public static function get_product_display_price( WC_Product $product ) {
6✔
44
                $decimals      = wc_get_price_decimals();
6✔
45
                $display_price = $product->get_price();
6✔
46
                $quantity      = $product->get_min_purchase_quantity();
6✔
47

48
                if ( wc_tax_enabled() ) {
6✔
49
                        // Taxes should be calculated.
50
                        if ( self::prices_should_include_tax() ) {
2✔
51
                                // Prices are stored **without** tax, add tax.
52
                                $display_price = wc_get_price_including_tax(
2✔
53
                                        $product,
2✔
54
                                        [
2✔
55
                                                'qty'   => $quantity,
2✔
56
                                                'price' => $display_price,
2✔
57
                                        ]
2✔
58
                                );
2✔
59
                        }
60
                        elseif ( self::prices_should_exclude_tax() ) {
×
61
                                // Prices are stored **with** tax, subtract tax.
62
                                $display_price = wc_get_price_excluding_tax(
×
63
                                        $product,
×
64
                                        [
×
65
                                                'qty'   => $quantity,
×
66
                                                'price' => $display_price,
×
67
                                        ]
×
68
                                );
×
69
                        }
70
                }
71

72
                return wc_format_decimal( $display_price, $decimals );
6✔
73
        }
74

75
        /**
76
         * Determines if tax should be added to the price stored in WooCommerce.
77
         *
78
         * @return bool True if prices should be displayed with tax added, false if not.
79
         */
80
        public static function prices_should_include_tax() {
4✔
81
                return (
4✔
82
                        ! wc_prices_include_tax()
4✔
83
                        && get_option( 'woocommerce_tax_display_shop' ) === 'incl'
4✔
84
                );
4✔
85
        }
86

87
        /**
88
         * Determines if tax should be subtracted from the price as stored in WooCommerce.
89
         *
90
         * @return bool True if prices should be displayed with tax subtracted, false if not.
91
         */
92
        public static function prices_should_exclude_tax() {
2✔
93
                return (
2✔
94
                        wc_prices_include_tax()
2✔
95
                        && get_option( 'woocommerce_tax_display_shop' ) === 'excl'
2✔
96
                );
2✔
97
        }
98

99
        /**
100
         * Determines if prices have tax included or not.
101
         *
102
         * @codeCoverageIgnore Wrapper method.
103
         *
104
         * @return bool True if prices have tax included, false if not.
105
         */
106
        public static function prices_have_tax_included() {
107
                return get_option( 'woocommerce_tax_display_shop' ) === 'incl';
108
        }
109

110
        /**
111
         * Determines the product type.
112
         *
113
         * @param WC_Product $product The WooCommerce Product.
114
         *
115
         * @return string The product type. Fallbacks to 'simple'.
116
         */
117
        public static function get_product_type( $product ) {
×
118
                if ( method_exists( $product, 'get_type' ) ) {
×
119
                        return $product->get_type();
×
120
                }
121

122
                return 'simple';
×
123
        }
124
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc