• Resolved reniak

    (@reniak)


    Hello,
    By importing products from a feed.xml file (External / Affiliate product) I want to simultaneously import attributes from url address.

    I am using this code to add custom attributes:

    function att_contents( $url ) {
       $response = wp_remote_get( $url );
    	$DOM = new DOMDocument();
    	$DOM->loadHTML($response);
    	
    	$Header = $DOM->getElementsByTagName('dt');
    	$Detail = $DOM->getElementsByTagName('dd');
    
    	foreach($Header as $NodeHeader) 
    	{
    		$aDataTableHeaderHTML[] = trim($NodeHeader->textContent);
    	}
    	
    	$i = 0;
    	$j = 0;
    	foreach($Detail as $sNodeDetail) 
    	{
    		$aDataTableDetailHTML[$j][] = trim($sNodeDetail->textContent);
    		$i = $i + 1;
    		$j = $i % count($aDataTableHeaderHTML) == 0 ? $j + 1 : $j;
    	}
    	
    	for($i = 0; $i < count($aDataTableDetailHTML); $i++)
    	{
    		for($j = 0; $j < count($aDataTableHeaderHTML); $j++)
    		{
    			$aTempData[$i][$aDataTableHeaderHTML[$j]] = $aDataTableDetailHTML[$i][$j];
    		}
    	}
    	$aDataTableDetailHTML = $aTempData; unset($aTempData);
    	print_r($aDataTableDetailHTML);
    }

    and have this result: screenshot

    How to split the result into separate fields: screenshot

    Thanks.

    • This topic was modified 6 years, 5 months ago by reniak.
    • This topic was modified 6 years, 5 months ago by reniak.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author WP All Import

    (@wpallimport)

    Hi @reniak

    How to split the result into separate fields: screenshot

    My suggestion for this would be to use the pmxi_saved_post hook to programmatically add the attributes to the product, rather than using our user interface: http://www.wpallimport.com/documentation/developers/action-reference/.

    If that’s not possible, you’d have to add all possible attributes to the import template and use functions in each name/value field to output the data. If you do this, I’d strongly suggest putting the results of the att_contents() call in an option (see: https://codex.ww.wp.xz.cn/Options_API) and extracting the data from that when possible (to avoid so many calls to the external URL). Be sure to clear the option as each post is saved/updated using the pmxi_saved_post hook.

    Thread Starter reniak

    (@reniak)

    Thanks for the reply,
    the most likely is to add the properties to the user interface.

    You might be a little more accurate when you say that:
    “I’d strongly suggest putting the results of the att_contents() call in an option (see: https://codex.ww.wp.xz.cn/Options_API) and extracting the data from that when possible”

    Thank you

    Plugin Author WP All Import

    (@wpallimport)

    Hi @reniak

    You might be a little more accurate when you say that:
    “I’d strongly suggest putting the results of the att_contents() call in an option (see: https://codex.ww.wp.xz.cn/Options_API) and extracting the data from that when possible”

    Basically I mean that you should store the results of this call for each record:

    $response = wp_remote_get( $url );

    Here’s some untested example code to explain:

    // See if the data has already been saved
    $response = get_option( 'my_saved_response' );
    
    if ( ! $data ) {
        // If it hasn't been, go ahead and make the call
        $response = wp_remote_get( $url );
    
        // store the result in an option so you don't have to retrieve it over and over
        update_option( 'my_saved_response', $response );
    }
    
    // do the rest of your operations in the function here

    Then you’d use our hook to erase the saved response as each record is saved so that you can re-do the request for each product:

    add_action( 'pmxi_saved_post', 'my_clear_response', 10, 1 );
    
    function my_clear_response( $id ) {
        delete_option( 'my_saved_response' );
    }

    Please keep in mind that this is untested and possibly broken code, it’s only meant to explain the idea.

    Thread Starter reniak

    (@reniak)

    Thanks again for the reply,
    The code indicates it works but i have this error:

    “Warning, mod_fcgid: stderr: PHP Notice: Undefined variable: data in ….wp-content/uploads/wpallimport/functions.php on line…”

    Thank you

    • This reply was modified 6 years, 4 months ago by reniak.
    • This reply was modified 6 years, 4 months ago by reniak.
    Plugin Author WP All Import

    (@wpallimport)

    Hi @reniak

    You can fix that warning by changing $data to $response.

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

The topic ‘Product Attributes’ is closed to new replies.