Support » Plugin: WooCommerce » Passing product data into a custom page/form

Viewing 3 replies - 1 through 3 (of 3 total)
  • The way I would probably tackle this issue is send the product ID in the url. On the page that receives the product ID, load the $product variable ($product = new WC_Product($_GET['product_id']);) output the details of the product to hidden form fields at the bottom of the page (assuming these are post meta values):

    <input type="hidden" name="material" value="<?php echo esc_attr(get_post_meta($product->get_id(), 'material', true)); ?>" />
    <input type="hidden" name="colour" value="<?php echo esc_attr(get_post_meta($product->get_id(), 'colour', true)); ?>" />

    Add a class to each form field you want to populate.

    Then add javascript that pulls the value from the hidden fields and updates the form fields.

    • This reply was modified 2 years, 1 month ago by agraddy. Reason: updated name field

    Thanks for that. I’ll give it a try and come back to you πŸ˜‰

    So, I used your suggestion and echo’d out product data out like so:

    $product = new WC_Product($_GET['product_id']);
    // Product  name
    echo $product->get_name();
    // Product  price
    echo $product->get_price();
    // Parent category
    $id = $product->get_category_ids()[0];
    if( $term = get_term_by( 'id', $id, 'product_cat' ) ){
      echo $term->name;
    // Main image
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $product->get_id() ), 'single-post-thumbnail' );?>
    <img src="<?php  echo $image[0]; ?>" class="img-responsive">
    // Hidden fields
    <input type="hidden" name="product-id" value="<?php echo $product->get_id(); ?>" />
    <input type="hidden" name="product-name" value="<?php echo $product->get_name(); ?>" />

    Thanks again πŸ™‚

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Passing product data into a custom page/form’ is closed to new replies.