Skip to content

Instantly share code, notes, and snippets.

@lucasstark
Created September 14, 2018 16:23
Show Gist options
  • Save lucasstark/2fec88fb6442b034efb73abdc1415caf to your computer and use it in GitHub Desktop.
Save lucasstark/2fec88fb6442b034efb73abdc1415caf to your computer and use it in GitHub Desktop.
Manually Enqueue Gravity Forms scripts for Gravity Forms Product Addons
<?php
add_action( 'wp_enqueue_scripts', 'add_gravity_forms_scripts' );
function add_gravity_forms_scripts() {
if ( ! class_exists( 'WC_GFPA_Main' ) ) {
return;
}
$PRODUCT_ID = 100;
if (is_page()) {
$product = wc_get_product( $PRODUCT_ID );
$gravity_form_data = wc_gfpa()->get_gravity_form_data( $PRODUCT_ID );
if ( $gravity_form_data && is_array( $gravity_form_data ) ) {
wp_enqueue_style( 'wc-gravityforms-product-addons', WC_GFPA_Main::plugin_url() . '/assets/css/frontend.css', null );
gravity_form_enqueue_scripts( $gravity_form_data['id'], false );
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
if ( WC_GFPA_Compatibility::is_wc_version_gte_2_5() ) {
wp_register_script( 'accounting', WC()->plugin_url() . '/assets/js/accounting/accounting' . $suffix . '.js', array( 'jquery' ), '0.4.2' );
} else {
wp_register_script( 'accounting', WC()->plugin_url() . '/assets/js/admin/accounting' . $suffix . '.js', array( 'jquery' ), '0.4.2' );
}
wp_enqueue_script( 'wc-gravityforms-product-addons', WC_GFPA_Main::plugin_url() . '/assets/js/gravityforms-product-addons.js', array(
'jquery',
'accounting'
), '3.2.4', true );
$prices = array(
$product->get_id() => wc_get_price_to_display( $product ),
);
if ( $product->has_child() ) {
foreach ( $product->get_children() as $variation_id ) {
$variation = wc_get_product( $variation_id );
$prices[ $variation_id ] = wc_get_price_to_display( $variation );
}
}
// Accounting
wp_localize_script( 'accounting', 'accounting_params', array(
'mon_decimal_point' => wc_get_price_decimal_separator()
) );
$wc_gravityforms_params = array(
'currency_format_num_decimals' => wc_get_price_decimals(),
'currency_format_symbol' => get_woocommerce_currency_symbol(),
'currency_format_decimal_sep' => esc_attr( wc_get_price_decimal_separator() ),
'currency_format_thousand_sep' => esc_attr( wc_get_price_thousand_separator() ),
'currency_format' => esc_attr( str_replace( array( '%1$s', '%2$s' ), array(
'%s',
'%v'
), get_woocommerce_price_format() ) ), // For accounting JS
'prices' => $prices,
'price_suffix' => array( $product->get_id() => $product->get_price_suffix() ),
'use_ajax' => array( $product->get_id() => apply_filters( 'woocommerce_gforms_use_ajax', isset( $gravity_form_data['use_ajax'] ) ? ( $gravity_form_data['use_ajax'] == 'yes' ) : false ) )
);
wp_localize_script( 'wc-gravityforms-product-addons', 'wc_gravityforms_params', $wc_gravityforms_params );
}
}
}
@NickGreen
Copy link

Note: $PRODUCT_ID = 100; needs to be changed to the product ID that you're using this for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment