Ad

Remove WooCommerce Cart Quantity Selector From Cart Page Only If Cart Item Has Specific Meta Data

I have found this answer which can remove the cart quantity selector as needed, however I only need to do this if a cart item has specific meta data. As I need to check for cart item meta rather than product meta, I can't use answers like this one.

How can I update the below code to only apply if the item has the cart meta data 'Ticketnumber'?

add_filter( 'woocommerce_cart_item_quantity', 'wc_cart_item_quantity', 10, 3 );
function wc_cart_item_quantity( $product_quantity, $cart_item_key, $cart_item ){
    if( is_cart() ){
        $product_quantity = sprintf( '%2$s <input type="hidden" name="cart[%1$s][qty]" value="%2$s" />', $cart_item_key, $cart_item['quantity'] );
    }
    return $product_quantity;
}

I thought I could use some code from this answer for retrieving the cart item meta, but I can't figure it out.

function kia_add_subtitle_to_cart_product( $title, $cart_item ){
    $_product = $cart_item['data'];
    $meta     = $_product->get_meta( 'product_card_beschrijving', true ); // THIS PART
    if( $meta ) {
        $title .= '<span class="meta">' . $meta . '</span>';
    }
    return $title;
}
add_filter( 'woocommerce_cart_item_name', 'kia_add_subtitle_to_cart_product', 10, 2 );
Ad

Answer

You can use $cart_item['data']->get_meta()

So you get:

function filter_woocommerce_cart_item_quantity( $product_quantity, $cart_item_key, $cart_item ) {
    // Get meta
    $ticketnumber = $cart_item['data']->get_meta( 'Ticketnumber', true );   

    if ( $ticketnumber ) {
        $product_quantity = sprintf( '%2$s <input type="hidden" name="cart[%1$s][qty]" value="%2$s" />', $cart_item_key, $cart_item['quantity'] );
    }
    
    return $product_quantity;
}
add_filter( 'woocommerce_cart_item_quantity', 'filter_woocommerce_cart_item_quantity', 10, 3 );
Ad
source: stackoverflow.com
Ad