Created
April 24, 2019 15:13
-
-
Save lucasstark/a2e4b11b9f3f30d4b4ddbdb88122e4ab to your computer and use it in GitHub Desktop.
Support Grouped Products for Gravity Forms Product Addons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ES_GravityForms_Grouped_Product_Add_To_Cart_Handler { | |
private static $instance; | |
public static function register() { | |
if ( self::$instance == null ) { | |
self::$instance = new ES_GravityForms_Grouped_Product_Add_To_Cart_Handler(); | |
} | |
} | |
protected $adding_to_cart; | |
protected $is_grouped_product; | |
public function __construct() { | |
add_filter( 'woocommerce_add_to_cart_product_id', [ $this, 'on_woocommerce_add_to_cart_product_id' ] ); | |
} | |
public function on_woocommerce_add_to_cart_product_id( $product_id ) { | |
$adding_to_cart = wc_get_product( $product_id ); | |
if ( ! $adding_to_cart ) { | |
return $adding_to_cart; | |
} | |
$add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->get_type(), $adding_to_cart ); | |
if ( 'grouped' === $add_to_cart_handler ) { | |
$this->adding_to_cart = $product_id; | |
add_filter( 'woocommerce_gforms_get_product_form_data', [ $this, 'get_product_form_data' ], 10, 3 ); | |
} | |
return $adding_to_cart; | |
} | |
public function get_product_form_data( $data, $product_id, $context ) { | |
if ( empty( $this->adding_to_cart ) || $product_id == $this->adding_to_cart ) { | |
return $data; | |
} | |
return WC_GFPA_Main::instance()->get_gravity_form_data( $this->adding_to_cart ); | |
} | |
} | |
ES_GravityForms_Grouped_Product_Add_To_Cart_Handler::register(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment