【问题标题】:Reflect default price and strikethrough in cart item price in WooCommerce在 WooCommerce 中的购物车商品价格中反映默认价格和删除线
【发布时间】:2020-11-04 14:52:06
【问题描述】:

我添加了基于每个购物车商品数量的累进定价。我正在努力的是从产品中反映$default_price(而不是现在手动添加的价格),并像这样设置新价格的布局(default_price 有删除线):

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 20, 1);
function add_custom_price( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    foreach ( $cart->get_cart() as $item ) {
        $quantity = $item['quantity'];
        $discount_price = 0.008333333333;
        $default_price = 4.25;
        $minimum = 10;
        $maximum = 100;
        if( $quantity > $minimum ) {
            if( $quantity > $maximum ) {
                $new_price =  $default_price - ($discount_price * ($maximum - $minimum));
            }
            else {
                $new_price = $default_price - ($discount_price * ($quantity - $minimum));   
            }
        $item['data']->set_price( '<del>' . $default_price . '</del> <strong>' . $new_price . '</strong>');
        }
    }
}

编码的$item['data']-&gt;set_price( '&lt;del&gt;' . $default_price . '&lt;/del&gt; &lt;strong&gt;' . $new_price . '&lt;/strong&gt;'); 是错误的,但是我该如何反映它以便它可以接受html元素?

【问题讨论】:

    标签: php wordpress woocommerce cart strikethrough


    【解决方案1】:

    对于您希望获得的内容,您可以使用以下内容:

    • 虽然woocommerce_before_calculate_totals 操作挂钩用于计算总数

    • woocommerce_cart_item_price 过滤钩确保删除原始价格。

    • 通过代码中添加的注释标签进行解释

    function action_woocommerce_before_calculate_totals( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        // Settings
        $discount_price = 0.008333333333;
        $minimum = 10;
        $maximum = 100;
    
        // Iterating though each cart items
        foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
            // Product quantity in cart
            $quantity = $cart_item['quantity'];
          
            // Quantity greater than minimum
            if ( $quantity > $minimum ) {
                // Default price
                $default_price = $cart_item['data']->get_price();
                
                // Quantity greater than maximum
                if ( $quantity > $maximum ) {
                    $new_price =  $default_price - ( $discount_price * ( $maximum - $minimum ) );
                } else {
                    $new_price = $default_price - ( $discount_price * ( $quantity - $minimum ) );   
                }
                
                // Set price
                $cart_item['data']->set_price( $new_price );
            }
        }
    }
    add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
    
    function filter_woocommerce_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
        // Get the product object
        $product = $cart_item['data'];
        
        // Is a WC product
        if ( is_a( $product, 'WC_Product' ) ) {
            // Get reqular price
            $regular_price = $product->get_regular_price();
            
            // New price
            $new_price = $cart_item['data']->get_price();
            
            // NOT empty and NOT equal
            if ( ! empty ( $regular_price ) && $regular_price != $new_price ) {
                // Output
                $price_html = '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $new_price ) . '</ins>';        
            }
        }
    
        return $price_html;
    }
    add_filter( 'woocommerce_cart_item_price', 'filter_woocommerce_cart_item_price', 10, 3 );
    

    【讨论】:

    • 有效!我最初的信息是一个错误的结论。非常感谢!你能给我解释一下if ( did_action( 'woocommerce_before_calculate_totals' ) &gt;= 2 ),为什么需要它?我觉得这是从另一个问题复制的代码?
    • 这是为了避免重复钩子(例如在使用价格计算时)
    猜你喜欢
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 2017-08-07
    相关资源
    最近更新 更多