Also change WooCommerce Minicart item price based on product custom field

泄露秘密 提交于 2020-12-31 15:02:46

问题


Based on Allow customer to set the product price (giftcard) and add to cart if sum is minimum 100 in WooCommerce, which answers my initial question - I am left with one small problem regarding the WooCommerce minicart.

The product price is not updated accordingly to what the customer submits using the giftcard field. So I have two different solutions whereof both fail.

This is what I tried:

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'] );
        }
    }
}

It doesn't work: Minicart go blank. Then I tried also:

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;
}

Any help I can get would be grateful.


回答1:


The right hook to be used is woocommerce_cart_item_price this way:

add_filter( 'woocommerce_cart_item_price', 'giftcard_cart_item_price', 10, 3 );
function giftcard_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
    $giftcard_key = 'giftcard_product_price';

    if( isset( $cart_item[$giftcard_key] ) ) {
        $args = array( 'price' => floatval( $cart_item[$giftcard_key] ) );

        if ( WC()->cart->display_prices_including_tax() ) {
            $product_price = wc_get_price_including_tax( $cart_item['data'], $args );
        } else {
            $product_price = wc_get_price_excluding_tax( $cart_item['data'], $args );
        }
        return wc_price( $product_price );
    }
    return $price_html;
}

Now the custom price will appear correctly in minicart...




回答2:


Full variant for free price in the basket for variable products.

/**
 * Initialize Session cart
 */
add_action('init', 'init_cart_session', 1);
function init_cart_session() {
    if (!session_id()) {
        session_start();
    }
    if (!isset($_SESSION['cart'])) {
        $_SESSION['cart'] = [];
    }
}

/**
 * Custom size correct
 */
add_action('woocommerce_before_calculate_totals', 'woo_size_correct', 10, 6);
function woo_size_correct() {
    global $woocommerce;

    if (isset($_POST['attribute_pa_size']) && $_POST['attribute_pa_size'] == 'custom') 
{
        $_SESSION['cart'][$_POST['variation_id']] = $_POST['custom'];
    }

    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
        if (array_key_exists($cart_item['data']->variation_id, $_SESSION['cart'])) {
            $cart_item['data']->set_price($_SESSION['cart'][$cart_item['data']->variation_id]);
        }
    }
}
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) {
    global $woocommerce;

    if (array_key_exists($cart_item['data']->variation_id, $_SESSION['cart'])) {
        $args = array('price' => floatval($_SESSION['cart'][$cart_item['data']->variation_id]));

        if (WC()->cart->display_prices_including_tax()) {
            $product_price = wc_get_price_including_tax($cart_item['data'], $args);
        } else {
            $product_price = wc_get_price_excluding_tax($cart_item['data'], $args);
        }
        return wc_price($product_price);
    }
    return $price_html;
}


来源:https://stackoverflow.com/questions/63550433/also-change-woocommerce-minicart-item-price-based-on-product-custom-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!