2

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

mujuonly
  • 11,370
  • 5
  • 45
  • 75
Jamesjaxx
  • 21
  • 2

1 Answers1

0
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.

mujuonly
  • 11,370
  • 5
  • 45
  • 75
  • Thank you @mujuonly, I had previously tried a similar function but it doesn't work for me, not sure you can properly hook into woocommerce_subscription_variation_is_purchasable for this? – Jamesjaxx May 15 '22 at 23:11
  • @Jamesjaxx Tested with WC 6.4 – mujuonly May 17 '22 at 16:27