• Resolved dmunaretto

    (@dmunaretto)


    OK – any help would be appreciated, for some reason I’m not able to populate edd_variable_prices no matter how I structure the array. I need to be able to create a download within the context of another custom app –

    I’m able to create the download post, but not populate the array – here is my code —

    add_action(“gform_after_submission_27”, “create_download_from_submission”, 10, 2);
    function create_download_from_submission($entry, $form)
    {

    //First need to create the post in its basic form
    $new_download = array(
    ‘post_title’ => ucwords($entry[5]),
    ‘post_status’ => ‘publish’,
    ‘post_date’ => date(‘Y-m-d H:i:s’),
    ‘post_type’ => ‘download’
    );
    //From creating it, we now have its ID
    $theId = wp_insert_post($new_download);
    //Now we add the meta

    update_post_meta($theId, _variable_pricing, ($entry[9]));
    update_post_meta($theId, plan_property_id, ($entry[14]));

    //Create the Array for the Variable Product Prices

    $array = array(
    ‘variableprice’ => array(
    ‘1’ => array(
    ‘index’ => 1,
    ‘name’ => “PDF”,
    ‘amount’ =>($entry[15])
    ),

    ‘2’ => Array(
    ‘index’ => 2,
    ‘name’ => “CAD”,
    ‘amount’ =>($entry[17])
    )
    )
    );

    $array = $_POST[‘variableprice’];
    update_post_meta( $theID, _edd_variable_prices, $array );

    ;
    }

    Any help would be appreciated!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi @dmunaretto

    Below are the meta which is stored by EDD when we create the variable pricing.
    1) _variable_pricing value 1
    2) _edd_default_price_id value which variable price ID you want to make it as default.
    3) edd_price value the lowest variable price.
    4) edd_variable_prices value in array as given below

    [1] => Array
            (
                [index] => 1
                [name] => Gold
                [amount] => 4.00
            )

    Your code have the same storage but I think the issue is in the variable price code.

    First you have created the array `$array = array(
    ‘variableprice’ => array(` with varialbe name $array

    After this you have taken the same variable $array = $_POST[‘variableprice’]; $_POST argument.

    So it looks like that may be your $_POST[‘variableprice’] is not present and it is going to be override.

    Can you please confirm the cases?

    Thread Starter dmunaretto

    (@dmunaretto)

    OK so I think you are saying that because I have $array = $_POST[‘variableprice’] it is overwriting $array argument used earlier. So… if I comment out $_POST I would have expected it to write the array using update_post_meta — but that didn’t work. (I also changed $array to $downloadarray, and set edd_price to 1 and edd_default_price_id to 1 afterwards. Here’s updated code…. Should edd_price point to an index or an amount?

    add_action(“gform_after_submission_27”, “create_download_from_submission”, 10, 2);
    function create_download_from_submission($entry, $form)
    {
    //First need to create the post in its basic form

    $new_download = array(
    ‘post_title’ => ucwords($entry[5]),
    ‘post_status’ => ‘publish’,
    ‘post_type’ => ‘download’
    );

    //From creating it, we now have its ID
    $theId = wp_insert_post($new_download);
    //Now we add the meta
    update_post_meta($theId, _variable_pricing, ($entry[9]));
    update_post_meta($theId, plan_property_id, ($entry[14]));
    //Create the Array for the Variable Product Prices
    $downloadarray = array(
    ‘variableprice’ => array(
    ‘1’ => array(
    ‘index’ => 1,
    ‘name’ => “PDF”,
    ‘amount’ =>($entry[15])
    ),
    ‘2’ => Array(
    ‘index’ => 2,
    ‘name’ => “CAD”,
    ‘amount’ =>($entry[17])
    )
    )
    );
    //$array = $Post[‘variableprice’];
    update_post_meta( $theID,_edd_variable_prices, $downloadarray );
    update_post_meta($theId, edd_price, 1);
    update_post_meta($theId, _edd_default_price_id, 1)
    ;
    }

    Thread Starter dmunaretto

    (@dmunaretto)

    OK my code creates the array (actually both $array and $dlarray create the correct array), and when serialized it appears to be correct. It sets variable price to 1 and it shows up as selected when you open the download from the admin screen, but it doesn’t save data to edd_variable_prices. Any suggestions>?

    add_action(“gform_after_submission_27”, “create_download_from_submission”, 10, 2);
    function create_download_from_submission($entry, $form)
    {

    //First need to create the post in its basic form
    $new_download = array(
    ‘post_title’ => ucwords($entry[5]),
    ‘post_status’ => ‘publish’,
    ‘post_date’ => date(‘Y-m-d H:i:s’),
    ‘post_type’ => ‘download’
    );
    //From creating it, we now have its ID
    $theId = wp_insert_post($new_download);
    //Now we add the meta

    update_post_meta($theId, _variable_pricing, ($entry[9]));
    //This sets variable pricing to on and works.
    update_post_meta($theId, plan_property_id, ($entry[14]));
    //This associates the download product to the “Floor Plan” product within the real estate listing system

    //Create the Array for the Variable Product Prices

    $dlarray = array(

    ‘1’ => array(
    ‘index’ => 1,
    ‘name’ => “PDF”,
    ‘amount’ =>($entry[15])
    ),

    ‘2’ => Array(
    ‘index’ => 2,
    ‘name’ => “CAD”,
    ‘amount’ =>($entry[17])
    )

    );

    $array;
    $array[“1”];
    $array[“1”][“index”] = “1”;
    $array[“1”][“name”] = “PDF”;
    $array[“1”][“amount”] = ($entry[15]);

    $array[“2”];
    $array[“2”][“index”] = “2”;
    $array[“2”][“name”] = “CAD”;
    $array[“2”][“amount”] = ($entry[17]);

    $str = serialize($array);

    $message= print_r($str);

    update_post_meta($post_id, ‘edd_variable_prices’, $array);
    update_post_meta($theId, edd_price, ($entry[15]));
    update_post_meta($theId, _edd_default_price_id, 1);

    }

    FROM $MESSAGE ON CONFIRMATION SCREEN:

    a:2:{i:1;a:3:{s:5:”index”;s:1:”1″;s:4:”name”;s:3:”PDF”;s:6:”amount”;s:2:”36″;}i:2;a:3:{s:5:”index”;s:1:”2″;s:4:”name”;s:3:”CAD”;s:6:”amount”;s:2:”46″;}} (this is $str)

    Array ( [1] => Array ( [index] => 1 [name] => PDF [amount] => 36 ) [2] => Array ( [index] => 2 [name] => CAD [amount] => 46 ) ) (this is $array)

    Hi @dmunaretto

    Actually I can not judge what other process are going to be run with this code.

    First declarer the $array; as blank like $array = array();

    I suggest you to use this plugin which helps you to find the PHP error if there is anything.
    https://ww.wp.xz.cn/plugins/query-monitor/

    I also suggest to on WordPress debugging via adding this code to wp-config.php file. define( 'WP_DEBUG', true );

    Please read below article for better understanding
    https://ww.wp.xz.cn/support/article/debugging-in-wordpress/

    The above things will help you to find out the PHP error if there is anything. I hope this will help you.

    Thread Starter dmunaretto

    (@dmunaretto)

    THIS IS HOW I WAS ABLE TO CREATE A DOWNLOAD FROM FRONT END USING GRAVITY FORMS:

    Pratik – thank you for your help, this can be marked as closed, and I’m posting here for anyone who needs in future.

    Put this in your functions.php file or install snippets plugin…

    add_action(“gform_after_submission_27”, “create_download_from_submission”, 10, 2);
    function create_download_from_submission($entry, $form)
    {

    //Create the Array for the Variable Product Prices

    $dlarray = array(

    ‘1’ => array(
    ‘index’ => 1,
    ‘name’ => “PDF”,
    ‘amount’ =>($entry[15])
    ),

    ‘2’ => Array(
    ‘index’ => 2,
    ‘name’ => “CAD”,
    ‘amount’ =>($entry[17])
    )

    );
    //Create the array for the download files to associate to the variable price items in dlarray.
    $downloadfiles = array(
    ‘1’ => array(
    ‘index’ => “1”,
    ‘attachment_id’ => “0”,
    ‘thumbnail_size’ => “”,
    ‘name’ =>””,
    ‘file’ => ($entry[20]),
    ‘condition’ =>”1″,
    ),

    ‘2’ => array(
    ‘index’ => 2,
    ‘attachment_id’ => “0”,
    ‘thumbnail_size’ => “”,
    ‘name’ =>””,
    ‘file’ =>($entry[21]),
    ‘condition’ =>”2″,

    ));

    // create the post in its basic form
    $new_download = array(
    ‘post_title’ => ucwords($entry[5]),
    ‘post_status’ => ‘publish’,
    ‘post_date’ => date(‘Y-m-d H:i:s’),
    ‘post_type’ => ‘download’,
    //USE META_INPUT TO SET META DATA. UPDATE_POST_META did not want to play nice with our variable price array. This worked like a charm.
    ‘meta_input’ => array(
    ‘_variable_pricing’ => ($entry[9]),
    ‘plan_property_id’ => ($entry[14]),
    ‘edd_variable_prices’ => $dlarray,
    ‘edd_download_files’ => $downloadfiles
    )
    );

    //From creating it, we now have its ID
    $theId = wp_insert_post($new_download);

    //update_post_meta($post_id, ‘edd_variable_prices’, $array);
    update_post_meta($theId, edd_price, ($entry[15]));
    //entry 15 is the lowest price -item 1 in my price options. you can make this more complicated.
    update_post_meta($theId, _edd_default_price_id, 1);
    //this sets default price item to whatever item ID you want to be default.

    }

    Thread Starter dmunaretto

    (@dmunaretto)

    Thanks everyone. Marked as resolved.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Creating Variable Price Product from Gravity’ is closed to new replies.