Disallow to remove a specific product ID from the cart if another product is added in WooCommerce

Disallow to remove a specific product ID from the cart if another product is added in WooCommerce on child sites.

Snippet Type

Execute on Child Sites

Snippet

function filter_woocommerce_cart_item_remove_link( $link, $cart_item_key ) {
    // Settings (multiple settings arrays can be added/removed if desired)
    $settings = array(
        array(
            'product_id_1'  => 100,
            'product_id_2'  => 101,
        ),
        array(
            'product_id_1'  => 30,
            'product_id_2'  => 813,
        ),
        array(
            'product_id_1'  => 53,
            'product_id_2'  => 817,
        ),
    );
    
    // Get cart
    $cart = WC()->cart;
    
    // If cart
    if ( $cart ) {
        // Get cart item
        $cart_item = $cart->get_cart()[$cart_item_key];
        
        // Get parent/real ID
        $product_id = $cart_item['data']->get_parent_id() != 0 ? $cart_item['data']->get_parent_id() : $cart_item['data']->get_id();
        
        // Loop trough settings array
        foreach ( $settings as $key => $setting ) {
            // Compare, get the correct setting array
            if ( $product_id == $settings[$key]['product_id_1'] ) {
                // Cart id of the other product
                $product_cart_id = $cart->generate_cart_id( $settings[$key]['product_id_2'] );

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.