Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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
You can’t perform that action at this time.