简体   繁体   中英

Change Cart Item Price After Added To cart

I have two products A and B. If A is added to the cart alone, it costs $10 dollars. If B is then added to the cart, there is a $5 additional charge for A (for the whole line, regardless of quantity).

Is there a way to modify line totals after the item has been added to the cart?

I'm new to Woocommerce. But I think I can help you a little, because I have to research the same topic recently.

Try this. All the credit go to WooCommerce - Adding a custom price to each product in cart woocommerce, how can i add additional cost in cart product total price? .

I only made a little modifications (Already tested. 13558 is the id of Product B).

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

function calculate_discounted_price( $price, $values, $cart_object ) {
    $terms = wc_get_product_terms( $product->id, $attribute_name, array( 'fields' => 'all' ) );

    foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];

        if( $_product->id == '13558' ) {
            //print_r("Producto B en Carrito");
            $price +=5;
        }
    }
    return $price;
}
function display_discounted_price( $values, $item ) {

    return wc_price( $item[ 'line_total' ] );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM