【问题标题】:Adding Custom price with woocomerce product price in Cart & Checkout在购物车和结帐中使用 woocommerce 产品价格添加自定义价格
【发布时间】:2017-01-08 16:08:44
【问题描述】:

我在 woocommerce 中添加了一个自定义字段,该字段具有额外的图片框架价格,现在如果图片价格为 10 美元并且用户选择一个框架,它将加起来 5 美元,总价将是 15 美元。

现在,如果我添加其他产品,则应添加选择的自定义框架价格。 例如,产品 1 价格为:10 美元,其上选择的框架为:frame1 价格为 5 美元,因此该产品的总价为 15 美元,如果 产品 2 以 10 美元的价格添加并选择 frame2,其价格为 6 美元,该产品的总价为 16 美元,但 总计 为 31 美元
接近我想要做的解决方案是:

add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 2 );

// Display the line total price
add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );

function calculate_discounted_price( $price, $values ) 
{

    $price += $_SESSION['framed_price'];
    return $price;

}
    }

我将帧值存储在会话中,每次用户单击我正在使用 ajax 的帧时它都会更新,直到一切正常。我也得到了价值。
此功能基本上是对添加的产品进行迭代,并将最后一帧的价格添加到购物车中的每个产品。
我们如何将产品价格与自定义框架价格相加?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    我找到了答案,解决了这个问题:

    // Change the line total price
    add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 2 );
    // Display the line total price
    add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );
    
    function calculate_discounted_price( $price, $values ) {
        // You have all your data on $values;
        $price += 10;
        return $price;
    }
    
    // wc_price => format the price with your own currency
    function display_discounted_price( $values, $item ) {
        return wc_price( $item[ 'line_total' ] );
    }
    

    参考:woocommerce, how can i add additional cost in cart product total price?

    【讨论】:

    • 此代码非常适合设置自定义价格 - 我注意到虽然产品小计和购物车总计正确,但购物车小计仍显示默认价格。是否也可以更新以避免混淆?
    • 嘿@LouisW 对不起,我离开 wordpress 工作很久了,我记得我后来解决了一些问题
    猜你喜欢
    • 2018-10-31
    • 1970-01-01
    • 2017-11-06
    • 2015-11-28
    • 2013-02-25
    • 2017-04-06
    • 2012-09-01
    • 1970-01-01
    相关资源
    最近更新 更多