Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在电子商务结账问题中显示产品销售价格_Php_Wordpress_Woocommerce_Price_Discount - Fatal编程技术网

Php 在电子商务结账问题中显示产品销售价格

Php 在电子商务结账问题中显示产品销售价格,php,wordpress,woocommerce,price,discount,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; }

我使用此代码片段在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 );
截图:

但是,有三个问题:

1。我无法使用自定义CSS单独确定销售价格。有没有一种方法可以给销售价格一个CSS类

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

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


有人能帮您解决这些问题吗?

在您的功能中,您可以检查产品是否应纳税,以及价格是否必须不包括增值税或增值税此检查已在WooCommerce功能中完成
wc\u get\u price\u to\u display
:文档

所以你可以这样优化它:

// 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个问题,我将分几点回答:

  • 您可以使用
    ins
    元素作为销售价格的选择器。例如:
    tr.cart\u商品目录{…}

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

    • 找到已经声明的CSS规则并修改它
    • 添加
      !重要信息
      至新CSS规则末尾(最好避免)
  • 我发布的函数应该修复此错误