Search code examples
xmldomdocumentcreateelement

createElement in DOMDocument using if else


i have this code:

$args = array( 'post_type' => 'product', 'posts_per_page' => 10 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); 
 global $product;
    $product = get_product( get_the_ID() );

  //create the title element
  $tutTag->appendChild(
    $xmlDoc->createElement("id", $loop->post->ID ));

  //create the date element
  $tutTag->appendChild(
    $xmlDoc->createElement("name", $loop->post->post_title ));

//create the date element
  $tutTag->appendChild(
    $xmlDoc->createElement("description", $loop->post->post_excerpt ));

//create the date element
  $tutTag->appendChild(
    $xmlDoc->createElement("category", $kategoria ));

endwhile; 

I need from $kategoria to do this:

global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
if (is_array($terms)) {
foreach ( $terms as $term ){
    $category_id = $term->term_id;
    break; 
}
}
    $wccolor    = get_woocommerce_term_meta( $term->term_id, 'podujatie_pricemania_kategorie_pricemania_pridat', true );
if ( $wccolor ){
        echo $wccolor;
    } else {
        echo get_option('product_type');
    }

I try to create function for $kategoria but have no luck.

Main idea of $kategoria is automatic choose preferer chois from plugin admin dashboard. Users can choose from more type of categories. But how to implement this code?

I created xml feed, but one of my customers need xml to be saved as xml file, so I am implementing feed to xml file. But have no luck at all.

Thank you.


Solution

  • This is solution for my problem

    $terms = get_the_terms( $post->ID, 'product_cat' );
    $typ = get_option('product_type');
    $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
    if ( $wccolor ){
            $category = $dom->createElement("category", $wccolor);
        } else {
            $category = $dom->createElement("category", $typ);
        }
        $product->appendChild($category);
    }