WooCommerce Subscriptions: Hide one variation subscription

Viewed 108

WooCommerce subscriptions | All Products for WooCommerce Subscriptions

Does anyone know if there's a way to disable the subscribe option for a specific product variation?

i.e. product with 2x attributes: attr-1, attr-2

All of which can be bought singly or via a sub on the PDP, is there a way to disable the subscribe option for one of the attributes?

There's no succinct way using the wcsatt_product_subscription_scheme filter without a lot of JS logic.

Have reached out to WC and would love to give back if there's a more a elegant solution

1 Answers
add_filter('woocommerce_subscription_variation_is_purchasable', 'conditional_variation_subscription_is_purchasable', 20, 2);

function conditional_variation_subscription_is_purchasable($purchasable, $product) {

    $check_attributes = [
        'attribute_pa_color' => 'black',
        'attribute_pa_slow-connect' => 'very-slow',
    ];

    $matching_variation = find_matching_product_variation_id($product->get_parent_id(), $check_attributes);
    if ($matching_variation == $product->get_id()) {
        $purchasable = false;
    }
    return $purchasable;
}

function find_matching_product_variation_id($product_id, $attributes) {
    return (new \WC_Product_Data_Store_CPT())->find_matching_product_variation(
                    new \WC_Product($product_id),
                    $attributes
    );
}

Add the code in to the active theme functions.php file. Change the attribute names and value as required.

Related