diff --git a/README.md b/README.md index 1f7955d..5d98b08 100644 --- a/README.md +++ b/README.md @@ -130,12 +130,15 @@ SKU, GTIN (WooCommerce 9.2+), currency, product type, prices, sale end date, stock, and rating with review count (when ratings are enabled and the product has reviews); the post author is left out. It also includes product categories, tags, and registered brands; a price/availability summary; the formatted short -description; visible attributes; and available -variations. Password protection and publication checks still apply. +description under `Summary`; the long description under `Description`; visible +attributes; and available variations. Password protection and publication checks +still apply. The body uses WooCommerce's displayed prices, including tax settings, currency, -price ranges, active sales, and pricing filters. Numeric frontmatter prices are -catalog values before display-tax adjustments, so they do not depend on who +price ranges, active sales, and pricing filters. The product's price line adds +`(incl. tax)` when shop prices include tax and the product is taxable, except for +VAT-exempt customers. Numeric frontmatter prices are catalog values before +display-tax adjustments, so they do not depend on who requests the page; `prices_include_tax` says whether those values include tax. Variable products use `price` and `price_max` for the minimum and maximum catalog prices. A future sale is not reported as an active sale. SKUs in the body are diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 895030d..5c6a595 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,5 +1,17 @@ parameters: ignoreErrors: + - + message: '#^Call to method is_taxable\(\) on an unknown class WC_Product\.$#' + identifier: class.notFound + count: 1 + path: src/Integration/WooCommerce.php + + - + message: '#^Function WC not found\.$#' + identifier: function.notFound + count: 2 + path: src/Integration/WooCommerce.php + - message: '#^Call to function is_array\(\) with array will always evaluate to true\.$#' identifier: function.alreadyNarrowedType diff --git a/readme.txt b/readme.txt index dfd6170..dacd83d 100644 --- a/readme.txt +++ b/readme.txt @@ -103,6 +103,7 @@ Yes, as an opt-in. With `add_filter( 'markdown_alternate_skip_noindexed', '__ret == Changelog == = Unreleased = +* Label tax-inclusive displayed product prices and give long product descriptions their own heading. * Kept author and date archives in HTML instead of redirecting Markdown requests to missing pages * Fixed cover block images running into the first inner heading * Omitted aria-hidden and data-URI images from Markdown diff --git a/src/Integration/WooCommerce.php b/src/Integration/WooCommerce.php index 7e95773..64087c9 100644 --- a/src/Integration/WooCommerce.php +++ b/src/Integration/WooCommerce.php @@ -278,7 +278,7 @@ private function format_date( $date ): string { } /** - * Add the summary, short description, attributes and variations. + * Add product details and label the long description. * * @param array $sections Existing sections. * @param WP_Post $post The post being rendered. @@ -293,6 +293,9 @@ public function add_sections( $sections, $post ): array { $rows = array(); $price = $this->format_price( $product ); if ( '' !== $price ) { + if ( 'incl' === get_option( 'woocommerce_tax_display_shop' ) && $product->is_taxable() && ( ! WC()->customer || ! WC()->customer->get_is_vat_exempt() ) ) { + $price .= ' ' . __( '(incl. tax)', 'markdown-alternate' ); + } $rows[] = '**' . __( 'Price:', 'markdown-alternate' ) . '** ' . $price; } if ( '' !== $product->get_sku() ) { @@ -313,6 +316,9 @@ public function add_sections( $sections, $post ): array { 'markdown' => '## ' . __( 'Summary', 'markdown-alternate' ) . "\n\n" . $description, ); } + if ( isset( $sections['content']['markdown'] ) && is_string( $sections['content']['markdown'] ) && '' !== trim( $sections['content']['markdown'] ) ) { + $sections['content']['markdown'] = '## ' . __( 'Description', 'markdown-alternate' ) . "\n\n" . $sections['content']['markdown']; + } $attributes = $this->build_attributes( $product ); if ( '' !== $attributes ) { $sections['woo_attributes'] = array( diff --git a/tests/woocommerce-integration.php b/tests/woocommerce-integration.php index 0c9c8ff..4b3b197 100644 --- a/tests/woocommerce-integration.php +++ b/tests/woocommerce-integration.php @@ -53,6 +53,7 @@ function md_alt_woo_render( $id ) { update_option( 'woocommerce_currency', 'EUR' ); update_option( 'woocommerce_calc_taxes', 'no' ); update_option( 'woocommerce_prices_include_tax', 'no' ); + update_option( 'woocommerce_tax_display_shop', 'incl' ); md_alt_woo_assert( PostTypeSupport::is_supported( 'product' ) && ! PostTypeSupport::is_supported( 'product_variation' ), 'Products enabled; standalone variations excluded' ); $md_alt_product = new WC_Product_Simple(); @@ -109,6 +110,34 @@ function md_alt_woo_render( $id ) { md_alt_woo_assert( false !== strpos( $md_alt_output, '[link](https://example.com)' ) && false !== strpos( $md_alt_output, '- One' ), 'Short description keeps links and lists' ); md_alt_woo_assert( false === strpos( $md_alt_output, '## Attributes' ) && false === strpos( $md_alt_output, '## Variations' ), 'No empty product sections' ); md_alt_woo_assert( false === get_transient( 'md_alt_cache_' . $md_alt_id ), 'Product output is not stored in the shared cache' ); + md_alt_woo_assert( false === strpos( $md_alt_output, '(incl. tax)' ), 'Disabled taxes do not add a tax-inclusive label' ); + md_alt_woo_assert( false !== strpos( $md_alt_output, "## Summary\n\nShort" ) && false !== strpos( $md_alt_output, "## Description\n\nLong **description**." ), 'Short and long descriptions have separate headings and preserve formatting' ); + $md_alt_short_description = $md_alt_product->get_short_description(); + $md_alt_product->set_short_description( '' ); + $md_alt_product->save(); + $md_alt_output = md_alt_woo_render( $md_alt_id ); + md_alt_woo_assert( false === strpos( $md_alt_output, '## Summary' ) && false !== strpos( $md_alt_output, '## Description' ), 'Long description has a heading without a short description' ); + $md_alt_product->set_description( '' ); + $md_alt_product->save(); + md_alt_woo_assert( false === strpos( md_alt_woo_render( $md_alt_id ), '## Description' ), 'Empty long description has no heading' ); + $md_alt_product->set_description( '
Long description.
' ); + $md_alt_product->set_short_description( $md_alt_short_description ); + $md_alt_product->save(); + $md_alt_content_filter = static function ( $sections ) { + $sections['content'] = array( + 'priority' => 125, + 'markdown' => 'Custom description.', + ); + return $sections; + }; + add_filter( 'markdown_alternate_content_sections', $md_alt_content_filter, 5 ); + $md_alt_sections = apply_filters( 'markdown_alternate_content_sections', array(), get_post( $md_alt_id ) ); + remove_filter( 'markdown_alternate_content_sections', $md_alt_content_filter, 5 ); + md_alt_woo_assert( 125 === $md_alt_sections['content']['priority'] && "## Description\n\nCustom description." === $md_alt_sections['content']['markdown'], 'Description heading preserves custom rendered content and priority' ); + $md_alt_sections = apply_filters( 'markdown_alternate_content_sections', array(), get_post( $md_alt_id ) ); + md_alt_woo_assert( ! isset( $md_alt_sections['content'] ), 'Removed long description is not restored' ); + $md_alt_sections = apply_filters( 'markdown_alternate_content_sections', array( 'content' => array( 'markdown' => " \n\t" ) ), get_post( $md_alt_id ) ); + md_alt_woo_assert( " \n\t" === $md_alt_sections['content']['markdown'], 'Whitespace-only long description has no heading' ); $md_alt_product->set_date_on_sale_from( time() + DAY_IN_SECONDS ); $md_alt_product->set_price( '20' ); @@ -223,7 +252,17 @@ function md_alt_woo_render( $id ) { WC()->customer->set_billing_country( 'NL' ); WC()->customer->set_shipping_country( 'NL' ); $md_alt_output = md_alt_woo_render( $md_alt_id ); - md_alt_woo_assert( false !== strpos( $md_alt_output, '24.20' ), 'Displayed price includes shop tax settings' ); + md_alt_woo_assert( false !== strpos( $md_alt_output, '**Price:** €24.20 (incl. tax)' ) && false !== strpos( $md_alt_output, "prices_include_tax: false\n" ), 'Tax-inclusive displayed price is labeled independently of catalog tax settings' ); + update_option( 'woocommerce_tax_display_shop', 'excl' ); + $md_alt_output = md_alt_woo_render( $md_alt_id ); + md_alt_woo_assert( false !== strpos( $md_alt_output, '**Price:** €20.00' ) && false === strpos( $md_alt_output, '(incl. tax)' ), 'Tax-exclusive displayed price has no inclusive label' ); + update_option( 'woocommerce_tax_display_shop', 'incl' ); + foreach ( array( 'none', 'shipping' ) as $md_alt_tax_status ) { + $md_alt_product->set_tax_status( $md_alt_tax_status ); + $md_alt_product->save(); + md_alt_woo_assert( false === strpos( md_alt_woo_render( $md_alt_id ), '(incl. tax)' ), 'Non-taxable product price has no inclusive label: ' . $md_alt_tax_status ); + } + $md_alt_product->set_tax_status( 'taxable' ); // Tax-inclusive catalog prices exercise exemption consistently on older WooCommerce too. update_option( 'woocommerce_prices_include_tax', 'yes' ); $md_alt_product->set_regular_price( '24.20' ); @@ -231,7 +270,7 @@ function md_alt_woo_render( $id ) { $md_alt_product->save(); WC()->customer->set_is_vat_exempt( true ); $md_alt_output = md_alt_woo_render( $md_alt_id ); - md_alt_woo_assert( false !== strpos( $md_alt_output, '**Price:** €20.00' ), 'VAT-exempt customer receives their own displayed price' ); + md_alt_woo_assert( false !== strpos( $md_alt_output, '**Price:** €20.00' ) && false === strpos( $md_alt_output, '(incl. tax)' ), 'VAT-exempt customer receives their own displayed price without an inclusive label' ); md_alt_woo_assert( false !== strpos( $md_alt_output, "prices_include_tax: true\n" ) && false !== strpos( $md_alt_output, "price: 24.20\n" ), 'Tax-inclusive catalog prices are flagged in frontmatter' ); $md_alt_post_id = wp_insert_post( @@ -244,6 +283,7 @@ function md_alt_woo_render( $id ) { $md_alt_woo_ids[] = $md_alt_post_id; $md_alt_output = md_alt_woo_render( $md_alt_post_id ); md_alt_woo_assert( false !== get_transient( 'md_alt_cache_' . $md_alt_post_id ) && false === strpos( $md_alt_output, 'product_type:' ), 'Ordinary post output still cached without product metadata' ); + md_alt_woo_assert( false === strpos( $md_alt_output, '## Description' ), 'Ordinary posts do not receive a product description heading' ); if ( getenv( 'MD_ALT_TEST_HTTP' ) ) { $md_alt_permalink = get_permalink( $md_alt_id ); $md_alt_md_url = untrailingslashit( $md_alt_permalink ) . '.md';