Jump to content

XML products export module. Need help.


Recommended Posts

Hello, 

I'm using B2B products export module, but I need to configure. I want that my category_name, category_id and category_link would be in father categories element. For example:

<categories>
	<category_name></category_name>
	<category_id></category_id>
	<category_link></category_link>
</categories>

What I need to change in this code?

$xmlProduct = $this->document->createElement('product');
$xmlProduct->setAttribute('id', $xmlProductId);
$xmlProduct->appendChild($this->getCDATAElement('title', $xmlProductName));
$xmlProduct->appendChild($this->document->createElement('price', $xmlProductPrice));
$xmlProduct->appendChild($this->document->createElement('stock', $product['quantity']));
$xmlProduct->appendChild($this->document->createElement('product_url', $xmlProductUrl));
$xmlProduct->appendChild($this->document->createElement('category_id', $product['id_category_default']));
$xmlProduct->appendChild($this->getCDATAElement('category_name', $product['category']));
$xmlProduct->appendChild($this->document->createElement('category_link', $productProvider->getCategoryLink($product['id_category_default'], $product['category_rewrite'])));

Thank you!

Link to comment
Share on other sites

It is a basic XML structure and there is a lot of help.
For example: https://www.php.net/manual/en/domnode.appendchild.php

1.

$xmlProduct = $this->document->createElement('product');

$xmlProduct->setAttribute('id', $xmlProductId);

$xmlProduct->appendChild(
    $this->getCDATAElement('title', $xmlProductName)
);

$xmlProduct->appendChild(
    $this->document->createElement('price', $xmlProductPrice)
);

$xmlProduct->appendChild(
    $this->document->createElement('stock', $product['quantity'])
);

$xmlProduct->appendChild(
    $this->document->createElement('product_url', $xmlProductUrl)
);

$xmlCategories = $xmlProduct->appendChild(
    $this->document->createElement('categories')
);

$xmlCategories->appendChild(
    $this->document->createElement('category_id', $product['id_category_default'])
);

$xmlCategories->appendChild(
    $this->getCDATAElement('category_name', $product['category'])
);

$xmlCategories->appendChild(
    $this->document->createElement('category_link', $productProvider->getCategoryLink($product['id_category_default'], $product['category_rewrite']))
);

 

2.

If you need to add all product categories, that is also not a problem.

The structure would expand.

A function call would have to be used to find all the associated categories.

...
...
$xmlCategories = $xmlProduct->appendChild(
    $this->document->createElement('categories')
);

$idLang = $this->context->language->id;
$getAllProductCategories = Product::getProductCategoriesFull($product['id_product'], $idLang);

foreach ($getAllProductCategories as $categories) {
    $xmlCategory = $xmlCategories->appendChild(
        $this->document->createElement('category')
    );

    if ($categories['id_category'] == $product['id_category_default']) {
        $xmlCategory->setAttribute('is_default', '1');
    } else {
        $xmlCategory->setAttribute('is_default', '0');
    }

    $xmlCategory->appendChild(
        $this->document->createElement('category_id', $product['id_category_default'])
    );

    $xmlCategory->appendChild(
        $this->getCDATAElement('category_name', $product['category'])
    );

    $xmlCategory->appendChild(
        $this->document->createElement('category_link', $productProvider->getCategoryLink($product['id_category_default'], $product['category_rewrite']))
    );
}

and XML output:

<categories>
	<category is_default="1">
		<category_name></category_name>
		<category_id></category_id>
		<category_link></category_link>
	</category>
	<category is_default="0">
		<category_name></category_name>
		<category_id></category_id>
		<category_link></category_link>
	</category>
</categories>

 

 

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...