Php 将属性与opencart中的产品关联以生成xml文件

Php 将属性与opencart中的产品关联以生成xml文件,php,xml,opencart,Php,Xml,Opencart,在过去的几个小时里,我试图生成这样的xml文件 <?xml version="1.0" encoding="UTF-8"?> <mywebstore> <created_at>2010-04-08 12:32</created_at> <products> <product> <id>322233</id> <name><

在过去的几个小时里,我试图生成这样的xml文件

<?xml version="1.0" encoding="UTF-8"?>
<mywebstore>
   <created_at>2010-04-08 12:32</created_at>
   <products>
      <product>
          <id>322233</id>
          <name><![CDATA[MadBiker 600 Black Polarized]]></name>
          <link><![CDATA[http://www.mywebstore.gr/product/322233]]></link>
          <image><![CDATA[http://www.mywebstore.gr/product/322233.jpg]]></image>
          <category id="23"><![CDATA[Sports > Extreme Sports]]></category>
          <price_with_vat>322.33</price_with_vat>
          <manufacturer><![CDATA[SuperGlasses]]></manufacturer>
          <description><![CDATA[This is the description.....]]></description>
          <weight>350</weight>
          <mpn>ZHD332</mpn>
          <instock>N</instock>
          <availability>Pre-order</availability>
      </product>
      <product>
       ...
      </product>
   </products>
</mywebstore>

2010-04-08 12:32
322233
极限运动]]>
322.33
350
ZHD332
N
预购
...
从opencart

我已经写了这段代码

<?php
class ControllerFeedSkroutzXml extends Controller {
    public function index() {
        $this->language->load('feed/skroutz_xml');

        if ($this->config->get('skroutz_xml_status')) {
            $output  = '<?xml version="1.0" encoding="UTF-8"?>';
            $output .= '<mywebstore>';
            $output .= '<created_at>' . date('Y-m-d H:i') . '</created_at>';
            $output .= '<products>';

            $this->load->model('catalog/product');

            $products = $this->model_catalog_product->getProducts();

            foreach ($products as $product) {

                $attribute_groups = $this->model_catalog_product->getProductAttributes($product['product_id']);
                //print_r($attribute_groups);
                if (!empty($attribute_groups)) {
                    foreach ($attribute_groups as $attribute_group) {
                        if (!empty($attribute_group)) {
                            foreach ($attribute_group['attribute'] as $attribute) {
                                $attribute = array_filter($attribute);
                                if (!empty($attribute)) {
                                    // [attribute_id] => 13, Color
                                    if ($attribute['attribute_id'] == 13 && $attribute['text'] != '') {
                                        $attribute_color = $attribute['text'];
                                    }
                                    // [attribute_id] => 16, Lens Technology
                                    if ($attribute['attribute_id'] == 16 && $attribute['text'] != '') {
                                        $attribute_lens_technology = $attribute['text'];
                                    }
                                }
                            }                           
                        }
                    }
                }

                if ($product['special']) {
                    $final_price = number_format((float)$product['special'], 2, '.', '');
                } else {
                    $final_price = number_format((float)$product['price'], 2, '.', '');
                }
                if ($product['quantity'] > 0) {
                    $instock = $this->language->get('instock_Y');
                } else {
                    $instock = $this->language->get('instock_N');
                }

                $output .= '<product>';
                $output .= '<id>' . $product['product_id'] . '</id>';
                $output .= '<name><![CDATA[' . $this->language->get('category_name') . ' ' . $product['name'] . ' ' . $attribute_color . ' ' . $attribute_lens_technology . ']]></name>';
                $output .= '<link><![CDATA[' . $this->url->link('product/product', 'product_id=' . $product['product_id']) . ']]></link>';
                $output .= '<image><![CDATA['. HTTP_IMAGE . $product['image'] . ']]></image>';
                $output .= '<category id="' . $product['manufacturer_id'] . '"><![CDATA[ ' . $this->language->get('category_name') . ' > ' . $product['manufacturer'] . ' ]]></category>';
                $output .= '<price_with_vat>' . $final_price . '</price_with_vat>';
                $output .= '<manufacturer><![CDATA[' . $product['manufacturer'] . ']]></manufacturer>';
                $output .= '<description><![CDATA[' . $product['meta_description'] . ']]></description>';
                $output .= '<instock>' . $instock . '</instock>';
                $output .= '<availability>' . $product['stock_status'] . '</availability>';
                $output .= '</product>';
            }

            $output .= '</products>';
            $output .= '</mywebstore>';

            $this->response->addHeader('Content-Type: application/xml');
            $this->response->setOutput($output);
        }
    }
}
?>
13,彩色
如果($attribute['attribute_id']==13&&$attribute['text']!=''){
$attribute_color=$attribute['text'];
}
//[attribute_id]=>16,镜头技术
如果($attribute['attribute_id']==16&&$attribute['text']!=''){
$attribute_lens_technology=$attribute['text'];
}
}
}                           
}
}
}
如果($product[“特殊”]){
$final_price=number_格式((浮动)$product['special',2',','');
}否则{
$final_price=数字_格式((浮动)$product['price',2',','');
}
如果($product['QUOTE']>0){
$instock=$this->language->get('instock_Y');
}否则{
$instock=$this->language->get('instock_N');
}
$output.='';
$output.=''$产品['product_id']。';
$output.='';
$output.='';
$output.='';
$output.=''$产品['manufacturer'].]]>';
$output.=''$最终价格;
$output.='';
$output.='';
$output.=''$"因斯托克",;
$output.=''$产品[“库存状态]”;
$output.='';
}
$output.='';
$output.='';
$this->response->addHeader('Content-Type:application/xml');
$this->response->setOutput($output);
}
}
}
?>
但是生成属性的代码块没有按预期工作。
我的很多产品都没有属性(至少还没有),所以我想要完成的是在产品名称旁边显示属性

示例
名称:MadBiker 600
属性-颜色:黑色
属性-镜头技术:偏振

全部
MadBiker 600黑色极化

仅当产品具有属性时 上面的php代码生成
MadBiker 600 Black Polarized
以清空所有属性产品,直到找到下一个具有属性的产品

有人能指出问题出在哪里吗


谢谢大家!

使用编写xml文件比手动尝试输出自己的xml文件更容易

然而,这里有一个简单的缩写if语句来解决您的问题(如果属性颜色为空,它将附加一个空字符串):

$output .= !empty($attribute_color) ? '<name><![CDATA[' . $this->language->get('category_name') . ' ' . $product['name'] . ' ' . $attribute_color . ' ' . $attribute_lens_technology . ']]></name>' : '';
$output.=!empty($attribute_color)?“”;

使用编写xml文件比手动尝试输出自己的文件更容易

然而,这里有一个简单的缩写if语句来解决您的问题(如果属性颜色为空,它将附加一个空字符串):

$output .= !empty($attribute_color) ? '<name><![CDATA[' . $this->language->get('category_name') . ' ' . $product['name'] . ' ' . $attribute_color . ' ' . $attribute_lens_technology . ']]></name>' : '';
$output.=!empty($attribute_color)?“”;

您没有在foreach的每次迭代中重置
$attribute\u lens\u技术
$attribute\u color
。您需要在foreach循环定义之后重置这些

新的foreach循环:

        foreach ($products as $product) {
            $attribute_lens_technology = false;
            $attribute_color = false;

            $attribute_groups = $this->model_catalog_product->getProductAttributes($product['product_id']);
            //print_r($attribute_groups);
            if (!empty($attribute_groups)) {
                foreach ($attribute_groups as $attribute_group) {
                    if (!empty($attribute_group)) {
                        foreach ($attribute_group['attribute'] as $attribute) {
                            $attribute = array_filter($attribute);
                            if (!empty($attribute)) {
                                // [attribute_id] => 13, Color
                                if ($attribute['attribute_id'] == 13 && $attribute['text'] != '') {
                                    $attribute_color = $attribute['text'];
                                }
                                // [attribute_id] => 16, Lens Technology
                                if ($attribute['attribute_id'] == 16 && $attribute['text'] != '') {
                                    $attribute_lens_technology = $attribute['text'];
                                }
                            }
                        }                           
                    }
                }
            }

            if ($attribute_lens_technology === false || $attribute_color === false) {
                // Code here such as continue; if you want to skip products without both attributes
            }

            if ($product['special']) {
                $final_price = number_format((float)$product['special'], 2, '.', '');
            } else {
                $final_price = number_format((float)$product['price'], 2, '.', '');
            }
            if ($product['quantity'] > 0) {
                $instock = $this->language->get('instock_Y');
            } else {
                $instock = $this->language->get('instock_N');
            }

            $output .= '<product>';
            $output .= '<id>' . $product['product_id'] . '</id>';
            $output .= '<name><![CDATA[' . $this->language->get('category_name') . ' ' . $product['name'] . ' ' . $attribute_color . ' ' . $attribute_lens_technology . ']]></name>';
            $output .= '<link><![CDATA[' . $this->url->link('product/product', 'product_id=' . $product['product_id']) . ']]></link>';
            $output .= '<image><![CDATA['. HTTP_IMAGE . $product['image'] . ']]></image>';
            $output .= '<category id="' . $product['manufacturer_id'] . '"><![CDATA[ ' . $this->language->get('category_name') . ' > ' . $product['manufacturer'] . ' ]]></category>';
            $output .= '<price_with_vat>' . $final_price . '</price_with_vat>';
            $output .= '<manufacturer><![CDATA[' . $product['manufacturer'] . ']]></manufacturer>';
            $output .= '<description><![CDATA[' . $product['meta_description'] . ']]></description>';
            $output .= '<instock>' . $instock . '</instock>';
            $output .= '<availability>' . $product['stock_status'] . '</availability>';
            $output .= '</product>';
        }
foreach($products作为$product){
$attribute_lens_technology=false;
$attribute_color=false;
$attribute\u groups=$this->model\u catalog\u product->getProductAttributes($product['product\u id');
//打印(属性组);
如果(!空($attribute_groups)){
foreach($attribute\u group作为$attribute\u group){
如果(!空($attribute_group)){
foreach($attribute_group['attribute']作为$attribute){
$attribute=数组过滤器($attribute);
如果(!empty($attribute)){
//[属性id]=>13,颜色
如果($attribute['attribute_id']==13&&$attribute['text']!=''){
$attribute_color=$attribute['text'];
}
//[attribute_id]=>16,镜头技术
如果($attribute['attribute_id']==16&&$attribute['text']!=''){
$attribute_lens_technology=$attribute['text'];
}
}
}                           
}
}
}
if($attribute_lens_technology==false | |$attribute_color==false){
//如果要跳过没有这两个属性的产品,请在此处编写代码,例如“继续”
}
如果($product[“特殊”]){
$final_price=number_格式((浮动)$product['special',2',','');
}否则{
$final_price=数字_格式((浮动)$product['price',2',','');
}
如果($product['QUOTE']>0){
$instock=$this->language->get('instock_Y');
}否则{
$instock=$this->language->get('i