【问题标题】:Custom Prices for WooCommerce Mini Cart Widget [duplicate]WooCommerce 迷你购物车小部件的自定义价格 [重复]
【发布时间】:2017-11-25 17:16:27
【问题描述】:

我有以下代码来生成自定义价格:

add_action( 'woocommerce_before_calculate_totals', 'update_custom_price', 1, 1 );
function update_custom_price( $cart_object ) {
    foreach ( $cart_object->cart_contents as $cart_item_key => $value ) {
      $price = my_custom_calculate_func($value);
      $value['data']->set_price($price);
    }
}

它在购物车页面上效果很好,但在 WooCommerce 迷你购物车小部件上,它不会显示正确的价格,但会计算正确的小计。

我相信此代码作为模板存在,因此我已将文件从 ../wp-content/plugins/woocommerce/templates/cart/mini-cart.php 复制到 ../wp-content/mytheme /woocommerce/cart/mini-cart.php 但更改此文件无济于事。我已删除此文件中的所有内容,但它保持不变。

任何指导表示赞赏。

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce


    【解决方案1】:

    产品价格在 mini-cart.php 第 39 行计算:

    $product_price     = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
    

    您可以在主题文件夹中的 mini-cart.php 中编辑此行,或在 functions.php 中使用过滤器:

    add_filter( 'woocommerce_cart_item_price', 'woocommerce_cart_item_price_filter', 10, 3 );
    function woocommerce_cart_item_price_filter( $price, $cart_item, $cart_item_key ) {
        // your code to calculate $new_price
    
        return $new_price;
    }
    

    【讨论】:

    • 谢谢..它对我有用..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    相关资源
    最近更新 更多