Reputation: 9
I am trying to move the variations drop down above this form I have inserted into my short description, the form developers helped me with this snippet:
add_action('woocommerce_after_add_to_cart_form', function () {
echo do_shortcode('[quform id="1"]');
});
It does work but then it inserts that specific form into every product on the site, but each product has a different form.
Or is there a way to insert that snippet into the product page rather than the whole site?
Upvotes: 0
Views: 33
Reputation: 254363
As each product can have its own different QuForm defined by an ID, what you can do is to add a custom input field to admin single product pages, where you will define that ID, which is different for each product.
If you don't define that QuForm ID, no form will be displayed.
The code:
// Display a custom input field in admin single product data "General" section
add_action( 'woocommerce_product_options_general_product_data', 'admin_product_quform_id_custom_field' );
function admin_product_quform_id_custom_field() {
woocommerce_wp_text_input( array(
'id' => '_quform_id',
'label' => esc_html__( 'QuForm ID', 'woocommerce' ),
'description' => __( 'Define the QuForm ID integer to display the related QuForm on the product page.', 'woocommerce' ),
'desc_tip' => 'true'
) );
}
// Admin product: Save QuForm ID submitted value
add_action( 'woocommerce_admin_process_product_object', 'save_product_quform_id_custom_field_value' );
function save_product_quform_id_custom_field_value( $product ) {
if ( isset($_POST['_quform_id']) ) {
$product->update_meta_data( '_quform_id', sanitize_text_field($_POST['_quform_id']) );
}
}
// Display the form dynamically on single product page
add_action('woocommerce_after_add_to_cart_form', 'display_quform_on_single_product_page');
function display_quform_on_single_product_page() {
global $product;
if ( $quform_id = $product->gete_meta('_quform_id') ) {
echo do_shortcode("[quform id='{$quform_id}']");
}
}
Code goes in functions.php file of your child theme (or in a plugin).
Admin product settings:
Upvotes: 0