ACF form post woocommerce product

Issue

i am developing a multivendor shop and i want the vendors to post products from a Template page. i am using Advance custom fields to achieve this functionality.
What i did by far is to create the page template and managed to display the form but i have some issue to validate the fields with the Product. i might need to create some functionalities ?

    <?php
/**
 * The template for displaying all pages.
 * Template Name: Add Product Vendor
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site will use a
 * different template.
 *
 * @package Bober
 */

?>
<?php

acf_form_head();

get_header();

?>
<div id="content">

    <?php
    global $woocommerce;

    acf_form(array(
        $post_title = 'field_5a60022b402e2',
        $post_categ = 'field_5a60028e402e3',
        $post_descrip = 'field_5a600384402e4',
        $post_img = 'field_5a6005e1402e7',
        $post_price = 'field_5a61207ce5226',
        /*'post_title' =>true,
        'post_content' => true,*/
        'uploader' => 'basic',

        'post_id' => 'new_post',
        'fields' => array($post_title, $post_descrip, $post_categ,$post_price, $post_img),
        'new_post' => array(
            'post_status' => 'draft',
            'post_type' =>'product',

        ),
        /*'fields' => array('field_5a60022b402e2', 'field_5a60028e402e3', 'field_5a600384402e4', 'field_5a6005e1402e7'),*/


        'submit_value' => 'Add product'
    ));

    ?>

</div>

<?php get_footer(); ?>

At this point i was able to asign the title of the product and the content of by using the code below based on ACF form documentation.

'post_title' =>true,
'post_content' => true,

How do i assign the Title value, the description value, price value and image to the product post ? I don’t necesary look for someone to provide me a code to replace, i want to know how to do it, give me some ideas, or where to read on how to do it.

Thank you in advance!

Solution

First, do an in-depth read of how acf_form() works.

The acf_form() code for adding a product in woocommerce could look like this:

$options = array(
    'post_id' => 'new_post',
    'post_title' => true,
    'post_content' => true,
    'new_post'      => array(
      'post_type'       => 'product',
      'post_status'     => 'draft'
     ),
    'field_groups' => [$field_group_id],
    'form' => true,
     'return' => '%post_url%', // so they could be redirected to the product page in a draft state - i imagine?!
    'html_submit_button'    => '<input type="submit" value="%s" />',
    'updated_message' => 'Product Created',
    'submit_value' => 'Submit Product'
);

acf_form($options);

Regarding the featured image of the product, create an image field and use the code below to sync the field with the featured image. You can read more about this by checking the ACF API reference for acf/update_value

add_filter('acf/update_value/name=featured_image', function ($value, $post_id, $field) {
if ($value != '') {
    update_post_meta($post_id, '_thumbnail_id', $value);
} else {
    delete_post_thumbnail($post_id);
}

return $value;
}, 10, 3);

Answered By – ciprian

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published