【问题标题】:Display product sale price in WooCommerce checkout issues在 WooCommerce 结帐问题中显示产品销售价格
【发布时间】:2021-05-18 16:15:28
【问题描述】:

我正在使用此代码 sn-p 在 WooCommerce 结帐时显示产品销售价格:

function show_sale_price_at_checkout( $subtotal, $cart_item, $cart_item_key ) {
    
    $product = $cart_item['data'];
    $quantity = $cart_item['quantity'];
    if ( ! $product ) {
        return $subtotal;
    }
    $regular_price = $sale_price = $suffix = '';
    if ( $product->is_taxable() ) {
        if ( 'excl' === WC()->cart->tax_display_cart ) {
            $regular_price = wc_get_price_excluding_tax( $product, array( 'price' => $product->get_regular_price(), 'qty' => $quantity ) );
            $sale_price    = wc_get_price_excluding_tax( $product, array( 'price' => $product->get_sale_price(), 'qty' => $quantity ) );
            if ( WC()->cart->prices_include_tax && WC()->cart->tax_total > 0 ) {
                $suffix .= ' ' . WC()->countries->ex_tax_or_vat() . '';
            }
        } else {
            $regular_price = wc_get_price_including_tax( $product, array( 'price' => $product->get_regular_price(), 'qty' => $quantity ) );
            $sale_price = wc_get_price_including_tax( $product, array( 'price' => $product->get_sale_price(), 'qty' => $quantity ) );
            if ( ! WC()->cart->prices_include_tax && WC()->cart->tax_total > 0 ) {
                $suffix .= ' ' . WC()->countries->inc_tax_or_vat() . '';
            }
        }
    } else {
        $regular_price    = $product->get_price() * $quantity;
        $sale_price       = $product->get_sale_price() * $quantity;
    }
    if ( $product->is_on_sale() && ! empty( $sale_price ) ) {
        $price = wc_format_sale_price(
                     wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price(), 'qty' => $quantity ) ),
                     wc_get_price_to_display( $product, array( 'qty' => $quantity ) )
                 ) . $product->get_price_suffix();
    } else {
        $price = wc_price( $regular_price ) . $product->get_price_suffix();
    }
   
    $price = $price . $suffix;
    return $price;
}
add_filter( 'woocommerce_cart_item_subtotal', 'show_sale_price_at_checkout', 10, 3 );

截图:https://ibb.co/xLB60Px

但是,存在 3 个问题:

1.我无法使用自定义 CSS 单独定位销售价格。有没有办法给销售价格一个 CSS 类?

2. 我无法正确更改促销价的颜色。将颜色设置为#000000 不会导致纯黑色,它保持灰色。所以我认为销售价格有点透明,但添加opacity: 1; 并没有帮助,它保持灰色。我不能增加不透明度,只能减少它。我很困惑。

3. 当结帐页面加载时,它会在没有销售价格的产品旁边显示这条丑陋的长消息:https://ibb.co/PzJztck 这是什么意思以及如何摆脱它?

有人可以帮忙解决这些问题吗?

【问题讨论】:

    标签: php wordpress woocommerce price discount


    【解决方案1】:

    在您的函数中,您检查产品是否应纳税,以及价格是否必须不含增值税或包含增值税。 此检查已在 WooCommerce 功能中完成 wc_get_price_to_display: here 文档。

    所以你可以这样优化它:

    // shows the product price on sale (if any) in the checkout table
    add_filter( 'woocommerce_cart_item_subtotal', 'show_sale_price_at_checkout', 10, 3 );
    function show_sale_price_at_checkout( $subtotal, $cart_item, $cart_item_key ) {
        
        // gets the product object
        $product = $cart_item['data'];
        // get the quantity of the product in the cart
        $quantity = $cart_item['quantity'];
    
        // check if the object exists
        if ( ! $product ) {
            return $subtotal;
        }
    
        // check if the product is on sale
        if ( $product->is_on_sale() && ! empty( $product->get_sale_price() ) ) {
            // shows sale price and regular price       
            $price = wc_format_sale_price (
                // regular price
                wc_get_price_to_display(
                    $product, array(
                        'price' => $product->get_regular_price(),
                        'qty' => $quantity
                        )
                    ),
                // sale price
                wc_get_price_to_display( $product, array (
                    'price' => $product->get_sale_price(),
                    'qty' => $quantity
                    )
                )
            ) . $product->get_price_suffix();
        } else {
            // shows regular price
            $price = wc_price (
                // regular price
                wc_get_price_to_display(
                    $product, array (
                        'price' => $product->get_regular_price(),
                        'qty' => $quantity
                    )
                )
            ) . $product->get_price_suffix();
        }
       
        return $price;
    }
    

    代码已经过测试并且可以运行。将其添加到活动主题的 functions.php 中。

    关于这3个问题我分点回答你:

    1. 您可以使用ins 元素作为促销价格的选择器。例如:tr.cart_item ins { ... }

    2. 前一点应该解决这一点。如果它不起作用,它可能取决于另一个先前声明的选择器的特殊性。在这种情况下,您有两种选择:

      • 找到已经声明的CSS规则并修改
      • !important 添加到新 CSS 规则的末尾(最好避免使用它)
    3. 我发布的函数应该可以修复这个错误。

    【讨论】:

      猜你喜欢
      • 2019-09-16
      • 1970-01-01
      • 2017-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      相关资源
      最近更新 更多