Skip to content

Instantly share code, notes, and snippets.

@LinzardMac
Created January 17, 2013 21:18
Show Gist options
  • Save LinzardMac/4559843 to your computer and use it in GitHub Desktop.
Save LinzardMac/4559843 to your computer and use it in GitHub Desktop.
function frontend_scripts() {
global $post;
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
$lightbox_en = get_option('woocommerce_enable_lightbox') == 'yes' ? true : false;
$chosen_en = get_option( 'woocommerce_enable_chosen' ) == 'yes' ? true : false;
$frontend_script_path = $this->plugin_url() . '/assets/js/frontend/';
// Register any scipts for later use, or used as dependencies
wp_register_script( 'chosen', $this->plugin_url() . '/assets/js/chosen/chosen.jquery' . $suffix . '.js', array( 'jquery' ), $this->version, true );
wp_register_script( 'jquery-plugins', $this->plugin_url() . '/assets/js/jquery-plugins' . $suffix . '.js', array( 'jquery' ), $this->version, true );
wp_register_script( 'wc-add-to-cart-variation', $frontend_script_path . 'add-to-cart-variation' . $suffix . '.js', array( 'jquery' ), $this->version, true );
wp_register_script( 'wc-single-product', $frontend_script_path . 'single-product' . $suffix . '.js', array( 'jquery' ), $this->version, true );
// Queue frontend scripts conditionally
if ( get_option( 'woocommerce_enable_ajax_add_to_cart' ) == 'yes' )
wp_enqueue_script( 'wc-add-to-cart', $frontend_script_path . 'add-to-cart' . $suffix . '.js', array( 'jquery' ), $this->version, true );
if ( is_cart() )
wp_enqueue_script( 'wc-cart', $frontend_script_path . 'cart' . $suffix . '.js', array( 'jquery' ), $this->version, true );
if ( is_checkout() )
wp_enqueue_script( 'wc-checkout', $frontend_script_path . 'checkout' . $suffix . '.js', array( 'jquery' ), $this->version, true );
if ( is_product() )
wp_enqueue_script( 'wc-single-product' );
if ( $lightbox_en && ( is_product() || ( ! empty( $post->post_content ) && strstr( $post->post_content, '[product_page' ) ) ) ) {
wp_enqueue_script( 'fancybox', $this->plugin_url() . '/assets/js/fancybox/fancybox' . $suffix . '.js', array( 'jquery' ), $this->version, true );
wp_enqueue_style( 'woocommerce_fancybox_styles', $this->plugin_url() . '/assets/css/fancybox.css' );
}
if ( $chosen_en && is_checkout() ) {
wp_enqueue_script( 'wc-chosen', $frontend_script_path . 'chosen-frontend' . $suffix . '.js', array( 'chosen' ), $this->version, true );
wp_enqueue_style( 'woocommerce_chosen_styles', $this->plugin_url() . '/assets/css/chosen.css' );
}
// Global frontend scripts
wp_enqueue_script( 'woocommerce', $frontend_script_path . 'woocommerce' . $suffix . '.js', array( 'jquery', 'jquery-plugins' ), $this->version, true );
// Variables for JS scripts
$woocommerce_params = array(
'countries' => json_encode( $this->countries->get_allowed_country_states() ),
'select_state_text' => __( 'Select an option…', 'woocommerce' ),
'plugin_url' => $this->plugin_url(),
'ajax_url' => $this->ajax_url(),
'ajax_loader_url' => apply_filters( 'woocommerce_ajax_loader_url', $this->plugin_url() . '/assets/images/ajax-loader.gif' ),
'required_rating_text' => esc_attr__( 'Please select a rating', 'woocommerce' ),
'review_rating_required' => get_option( 'woocommerce_review_rating_required' ),
'required_text' => esc_attr__( 'required', 'woocommerce' ),
'update_order_review_nonce' => wp_create_nonce( "update-order-review" ),
'apply_coupon_nonce' => wp_create_nonce( "apply-coupon" ),
'option_guest_checkout' => get_option( 'woocommerce_enable_guest_checkout' ),
'checkout_url' => add_query_arg( 'action', 'woocommerce-checkout', $this->ajax_url() ),
'is_checkout' => is_page( woocommerce_get_page_id( 'checkout' ) ) ? 1 : 0,
'update_shipping_method_nonce' => wp_create_nonce( "update-shipping-method" ),
'add_to_cart_nonce' => wp_create_nonce( "add-to-cart" )
);
if ( is_checkout() || is_cart() )
$woocommerce_params['locale'] = json_encode( $this->countries->get_country_locale() );
wp_localize_script( 'woocommerce', 'woocommerce_params', apply_filters( 'woocommerce_params', $woocommerce_params ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment