简体   繁体   English

添加到购物车后更改购物车项目价格

[英]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. 我有两个产品A和B.如果A单独添加到购物车,则需要10美元。 If B is then added to the cart, there is a $5 additional charge for A (for the whole line, regardless of quantity). 如果B然后被添加到购物车,则A的额外费用为5美元(对于整条生产线,无论数量多少)。

Is there a way to modify line totals after the item has been added to the cart? 有没有办法在项目添加到购物车后修改总计?

I'm new to Woocommerce. 我是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? 所有的功劳都归功于WooCommerce - 为购物车中的每个产品添加自定义价格 ,如何在购物车产品总价中增加额外费用? .

I only made a little modifications (Already tested. 13558 is the id of Product B). 我只做了一些修改(已经过测试.13558是产品B的ID)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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