还可以根据产品自定义字段更改 WooCommerce Minicart 商品价格

基于允许客户设置产品价格(礼品卡)并在 WooCommerce 中金额至少为 100 时添加到购物车,这回答了我的最初问题 - 我剩下一个关于 WooCommerce 迷你车的小问题。

产品价格不会根据客户使用礼品卡字段提交的价格进行更新。所以我有两种不同的解决方案,但都失败了。

这是我尝试过的:

add_filter('woocommerce_widget_cart_item_quantity', 'custom_wc_widget_cart_item_quantity', 10, 3 );

function custom_wc_widget_cart_item_quantity( $cart, $cart_item, $cart_item_key ) {


    foreach ( $cart->get_cart() as $cart_item ) {


        if ( isset ( $cart_item['giftcard_product_price'] ) ) {


        $cart_item['data']->set_price( $cart_item['giftcard_product_price'] );


        return sprintf( '<span class="quantity">%s &times; <span class="woocommerce-Price-amount amount">%s <span class="woocommerce-Price-currencySymbol">%s</span></span></span>', $cart_item['quantity'], $cart_item['giftcard_product_price'] );

        }

    }

}

它不起作用:小车一片空白。然后我也尝试过:


add_filter('woocommerce_cart_item_price','modify_cart_product_price',10,3);

function modify_cart_product_price( $price, $cart_item, $cart_item_key){

    $price = $cart_item['data']->set_price($cart_item['giftcard_product_price']);

    return $price;

}

我能得到的任何帮助将不胜感激。


撒科打诨
浏览 134回答 2
2回答

一只斗牛犬

使用的正确钩子是woocommerce_cart_item_price这样的:add_filter( 'woocommerce_cart_item_price', 'giftcard_cart_item_price', 10, 3 );function giftcard_cart_item_price( $price_html, $cart_item, $cart_item_key ) {&nbsp; &nbsp; $giftcard_key = 'giftcard_product_price';&nbsp; &nbsp; if( isset( $cart_item[$giftcard_key] ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $args = array( 'price' => floatval( $cart_item[$giftcard_key] ) );&nbsp; &nbsp; &nbsp; &nbsp; if ( WC()->cart->display_prices_including_tax() ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $product_price = wc_get_price_including_tax( $cart_item['data'], $args );&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $product_price = wc_get_price_excluding_tax( $cart_item['data'], $args );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return wc_price( $product_price );&nbsp; &nbsp; }&nbsp; &nbsp; return $price_html;}现在自定义价格将正确显示在迷你车中......

倚天杖

可变产品篮子中的完整变体免费。/**&nbsp;* Initialize Session cart&nbsp;*/add_action('init', 'init_cart_session', 1);function init_cart_session() {&nbsp; &nbsp; if (!session_id()) {&nbsp; &nbsp; &nbsp; &nbsp; session_start();&nbsp; &nbsp; }&nbsp; &nbsp; if (!isset($_SESSION['cart'])) {&nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['cart'] = [];&nbsp; &nbsp; }}/**&nbsp;* Custom size correct&nbsp;*/add_action('woocommerce_before_calculate_totals', 'woo_size_correct', 10, 6);function woo_size_correct() {&nbsp; &nbsp; global $woocommerce;&nbsp; &nbsp; if (isset($_POST['attribute_pa_size']) && $_POST['attribute_pa_size'] == 'custom')&nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['cart'][$_POST['variation_id']] = $_POST['custom'];&nbsp; &nbsp; }&nbsp; &nbsp; foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {&nbsp; &nbsp; &nbsp; &nbsp; if (array_key_exists($cart_item['data']->variation_id, $_SESSION['cart'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $cart_item['data']->set_price($_SESSION['cart'][$cart_item['data']->variation_id]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}add_filter('woocommerce_cart_item_price', 'woo_size_mini_cart_correct', 10, 6);function woo_size_mini_cart_correct($price_html, $cart_item, $cart_item_key) {&nbsp; &nbsp; global $woocommerce;&nbsp; &nbsp; if (array_key_exists($cart_item['data']->variation_id, $_SESSION['cart'])) {&nbsp; &nbsp; &nbsp; &nbsp; $args = array('price' => floatval($_SESSION['cart'][$cart_item['data']->variation_id]));&nbsp; &nbsp; &nbsp; &nbsp; if (WC()->cart->display_prices_including_tax()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $product_price = wc_get_price_including_tax($cart_item['data'], $args);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $product_price = wc_get_price_excluding_tax($cart_item['data'], $args);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return wc_price($product_price);&nbsp; &nbsp; }&nbsp; &nbsp; return $price_html;}
打开App,查看更多内容
随时随地看视频慕课网APP