0

my knowledge of php is limited, here is the scenario and my question: I am importing a json file in order to programmatically generate products. This works all fine. The only problem is, that i have two json fields that i want to plug both into the product description.

This the function that i use:

function insert_product ($product_data) {
$title = $product_data['title'];
//$subtitle = $product_data['title']['b029'];

if (!get_page_by_title($title, 'OBJECT', 'product')){
$post = array( // Set up the basic post data to insert products

    'post_author'  => 1,
    'post_content' => $product_data['note'],
    //instead of putting note2 into post_excerpt i'd prefer to have it in post_content
    'post_excerpt' => $product_data['note2'],
    'post_status'  => 'publish',
    'post_title'   => $product_data['title'],
    'post_type'    => 'product',
);


$post_id = wp_insert_post($post); // Insert the post returning the new post id

if (!$post_id) // If there is no post id something has gone wrong so don't proceed
{
    return false;
}


    update_post_meta($post_id, '_price', $product_data['price']);
    update_post_meta($post_id, '_regular_price', $product_data['price']);
    update_post_meta( $post_id,'_visibility','visible'); // Set the product to visible, if not it won't show on the front end
    $cat_ids = array( 40 );
    wp_set_object_terms($post_id, $cat_ids, 'product_cat'); // Set up its categories
    wp_set_object_terms($post_id, 'single', 'product_type'); // Set it to a single product type

    }

function insert_products ($products)  {
if (!empty($products)) // No point proceeding if there are no products
{
    array_map('insert_product', $products);
}
}

function do_products(){
    $json_file = file_get_contents('file.json');
    strip_tags($json_file, '.');
    $products_data = json_decode($json_file, true);


    insert_products($products_data);

}
add_action('init','do_products',100);

How can i merge two json fields, or whatever they are called, into one :

"note": "fdsfsdfdsfsdfsd",
"note2": "dsadsadasdasd,

i want to add them both to:

post_content

i've tried:

'post_content' => array($product_data['note'],$product_data['note2']),

this returns a warning:

Warning: strpos() expects parameter 1 to be string, array given in /Applications/MAMP/htdocs/wordpress/wp-includes/formatting.php on line 2137

and then:

'post_content' => $product_data[array_merge(['note1'] ,['note2'])],

next warning:

 Warning: Illegal offset type in /Applications/MAMP/htdocs/wordpress/wp-content/plugins/insert books/insert_database_books.php on line 25   

Is there a way to merge "note" and "note2" and then add it to post_content? Thanks for your interest. theo

4
  • you are most likely "doing it wrong", but it might be better if you edit the question and give some context like what is it that you are trying to do – Mark Kaplun Jan 11 '17 at 13:50
  • post_content must be a string ( humans read text, not arrays) but you're trying to pass it an array/list, what you want to do is string concatenation, not array merging. As an aside, PHP sees your JSON as a JSON string, not a data structure as javascript would. You can turn it into an array using json_decode but I don't think that's useful to you here – Tom J Nowell Jan 11 '17 at 13:58
  • how can i concatenate note and note2? – timholz Jan 11 '17 at 14:10
  • here is the solution: 'post_content' => $product_data['note'] . '<br>'. $product_data['note2'], – timholz Jan 11 '17 at 15:51
0

this is the solution:

'post_content' => $product_data['note'] . '<br>'. $product_data['note2'],

this concatenates strings 'note' and 'note2'. Thanks for the hints.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.