John Paul
John Paul

Reputation: 97

Make a filter product specific

I have a woocommerce installation and have being trying to force the cart to return a fixed value regardless of the quantity. I have finally found the right filter to do this.

add_filter('woocommerce_cart_subtotal','subtotalchanger',0);
    function subtotalchanger($subtotal){
        $my_fixed_subtotal = 1.65;
        return $my_fixed_subtotal;
    }

But when trying to target the product concerned the filter will not work.

add_filter('woocommerce_cart_subtotal','subtotalchanger',0);
        function subtotalchanger($subtotal){
        if (is_single('15455')){
        $my_fixed_subtotal = 1.65;
        return $my_fixed_subtotal;
        }
    }

what am I doing wrong here?

Upvotes: 1

Views: 90

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29614

Do you mean this?

function subtotalchanger($subtotal, $compound, $cart ) {
    $cart = WC()->cart->get_cart();

    foreach( $cart as $cart_item ) {        
        $product = wc_get_product( $cart_item['product_id'] );

        // Get product id
        $product_id = $product->get_id();

        if ( $product_id == 15455 ) {
            $subtotal = 1.65;
        }
    }

    return $subtotal;
}
add_filter( 'woocommerce_cart_subtotal', 'subtotalchanger', 10, 3 ); 

Upvotes: 1

Related Questions