Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active January 18, 2019 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/d534b41fc86c156578a9b0eea7ecf51b to your computer and use it in GitHub Desktop.
Save damiencarbery/d534b41fc86c156578a9b0eea7ecf51b to your computer and use it in GitHub Desktop.
<?php
// Include a Ninja Forms form in a product page. Different forms for simple and variable products.
add_filter( 'the_content', 'dcwd_add_ninja_to_all_products' );
function dcwd_add_ninja_to_all_products( $content ) {
if ( class_exists( 'woocommerce' ) && is_product() && is_main_query() ) { // Check suggested by: https://pippinsplugins.com/playing-nice-with-the-content-filter/
global $product;
if ( 'simple' == $product->get_type() ) {
ob_start();
Ninja_Forms()->display( 1 ); // Equivalent to shortcode: [ninja_form id=1]
return $content . ob_get_clean();
}
if ( 'variable' == $product->get_type() ) {
ob_start();
Ninja_Forms()->display( 2 ); // Equivalent to shortcode: [ninja_form id=2]
return $content . ob_get_clean();
}
}
return $content;
}
<?php
// Populate the specific list box with product variations.
add_filter( 'ninja_forms_render_options', 'dcwd_populate_ninja_forms_select_for_variations', 10, 2 );
function dcwd_populate_ninja_forms_select_for_variations( $options, $settings ) {
if ( 'select_product_attributes' == $settings[ 'key' ] ) {
global $product;
$options = array();
// Examine the variations.
foreach ( $product->get_available_variations() as $variation ) {
$variations = array();
// Record the attributes of each variation.
foreach ( $variation[ 'attributes' ] as $attribute ) {
$variations[] = $attribute;
}
// Then assemble this combination of attributes into a single variation option.
$options[] = array(
'label' => esc_html( implode( ' ', $variations ) ), // Readable version e.g "S White".
'value' => esc_attr( implode( '-', $variations ) ), // HTML entity version e.g. "S-White".
'calc' => 0,
'selected' => false,
);
// If viewing a submission get the submitted value in case it is no longer an option in the form.
if ( is_admin() && array_key_exists( 'post', $_GET ) ) {
$post_id = absint( $_GET[ 'post' ] );
$selected_value = get_post_meta( $post_id, '_field_' . $settings[ 'order' ], true );
// Check whether the selected value is already in $options (either part of the form or added above).
$key = array_search( $selected_value, array_column( $options, 'value' ) );
// Only add the selected value if it's not present.
if ( false === $key ) {
$options[] = array(
'label' => $selected_value, // The original display label is not available.
'value' => $selected_value,
'calc' => 0,
'selected' => true,
);
}
}
}
}
return $options;
}
<?php
/*
Plugin Name: Product Enquiry form with Ninja Forms
Plugin URI: https://www.damiencarbery.com/2018/05/product-enquiry-form-with-ninja-forms/
Description: Product enquiry form for simple and variable products.
Author: Damien Carbery
Version: 0.1
*/
// Include a Ninja Forms form in a product page. Different forms for simple and variable products.
add_filter( 'the_content', 'dcwd_add_ninja_to_all_products' );
function dcwd_add_ninja_to_all_products( $content ) {
if ( class_exists( 'woocommerce' ) && is_product() && is_main_query() ) { // Check suggested by: https://pippinsplugins.com/playing-nice-with-the-content-filter/
global $product;
if ( 'simple' == $product->get_type() ) {
ob_start();
Ninja_Forms()->display( 1 ); // Equivalent to shortcode: [ninja_form id=1]
return $content . ob_get_clean();
}
if ( 'variable' == $product->get_type() ) {
ob_start();
Ninja_Forms()->display( 2 ); // Equivalent to shortcode: [ninja_form id=2]
return $content . ob_get_clean();
}
}
return $content;
}
// Populate the specific list box with product variations.
add_filter( 'ninja_forms_render_options', 'dcwd_populate_ninja_forms_select_for_variations', 10, 2 );
function dcwd_populate_ninja_forms_select_for_variations( $options, $settings ) {
if ( 'select_product_attributes' == $settings[ 'key' ] ) {
global $product;
$options = array();
// Examine the variations.
foreach ( $product->get_available_variations() as $variation ) {
$variations = array();
// Record the attributes of each variation.
foreach ( $variation[ 'attributes' ] as $attribute ) {
$variations[] = $attribute;
}
// Then assemble this combination of attributes into a single variation option.
$options[] = array(
'label' => esc_html( implode( ' ', $variations ) ), // Readable version e.g "S White".
'value' => esc_attr( implode( '-', $variations ) ), // HTML entity version e.g. "S-White".
'calc' => 0,
'selected' => false,
);
// If viewing a submission get the submitted value in case it is no longer an option in the form.
if ( is_admin() && array_key_exists( 'post', $_GET ) ) {
$post_id = absint( $_GET[ 'post' ] );
$selected_value = get_post_meta( $post_id, '_field_' . $settings[ 'order' ], true );
// Check whether the selected value is already in $options (either part of the form or added above).
$key = array_search( $selected_value, array_column( $options, 'value' ) );
// Only add the selected value if it's not present.
if ( false === $key ) {
$options[] = array(
'label' => $selected_value, // The original display label is not available.
'value' => $selected_value,
'calc' => 0,
'selected' => true,
);
}
}
}
}
return $options;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment