在 WooCommerce 结帐问题中显示产品销售价格

时间:2021-02-15 18:51:30

标签: php wordpress woocommerce price discount

我正在使用此代码片段在 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 这是什么意思以及如何摆脱它?

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

1 个答案:

答案 0 :(得分:2)

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

所以你可以像这样优化它:

// 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. 我发布的函数应该可以修复这个错误。

相关问题