无法使用属性值生成Woocommerce产品XML Feed

时间:2018-06-17 11:36:05

标签: xml wordpress woocommerce

我有一个插件(没有支持),然而它成功地生成了一个xml feed。

插件设置中有一个选项,我可以使用自己创建的属性“可用性”。

我创建属性,我将值放在属性中,然后更新我的产品。

我创建了xml Feed,但在可用性字段中有一个名为"属性"的默认值,而不是例如"在1-3天内可用"

function generate_products_xml_data() {

$xml_rows = array();
$instockavailability = get_option('instockavailability');
$avaibilities = array("Άμεση παραλαβή / Παράδoση 1 έως 3 ημέρες", "Παράδοση σε 1 - 3 ημέρες", "Παράδοση σε 4 - 10 ημέρες", "attribute");
$availabilityST = $avaibilities[$instockavailability];
$ifoutofstock = get_option('ifoutofstock');
$format_price = false;

if (function_exists('wc_get_price_decimal_separator') && 
function_exists('wc_get_price_thousand_separator') && function_exists

('wc_get_price_decimals')) {
    $decimal_separator = wc_get_price_decimal_separator();
    $thousand_separator = wc_get_price_thousand_separator();
    $decimals = wc_get_price_decimals();
    $format_price = true;
}

$result = wc_get_products(array('status' => array('publish'), 'limit' => -1));

foreach ($result as $index => $prod) {
    $available_variations = array();
    if($prod->get_type()=='variable'){
        $available_variations = $prod->get_available_variations(); 
    }

    $attributes = $prod->get_attributes();

    $skroutz_atts_color = get_option('skroutz_atts_color', 'pa_color');
    $skroutz_atts_size = get_option('skroutz_atts_size', 'pa_size');
    $skroutz_atts_manuf = get_option('skroutz_atts_manuf', 'pa_brand');
    $variation_atts = array($skroutz_atts_color=>array(),$skroutz_atts_size=>array());

foreach($available_variations as $var){
        if(isset($var['stock_status']) && $var['stock_status']=='outofstock') continue;
        $atts = $var['attributes'];
        if(isset($atts['attribute_'.$skroutz_atts_size]) && $atts['attribute_'.$skroutz_atts_size]!=''){
            $variation_atts[$skroutz_atts_size][]=$atts['attribute_'.$skroutz_atts_size];
        }
        if(isset($atts['attribute_'.$skroutz_atts_color]) && $atts['attribute_'.$skroutz_atts_color]!=''){
            $variation_atts[$skroutz_atts_color][]=$atts['attribute_'.$skroutz_atts_color];
        }
    }

    $stockstatus_ds = $prod->get_stock_status();
    if ((strcmp($stockstatus_ds, "outofstock") == 0) & ($ifoutofstock == 1)) {
        continue;
    }
    $onfeed = $prod->get_meta('onfeed');
    if (strcmp($onfeed, "no") == 0) {
        continue;
    }
    $xml_rows[$prod->get_id()] = array(
        'onfeed' => $onfeed,
        'stockstatus' => $stockstatus_ds,
        'attributes' => $attributes
    );

    switch ($instockavailability) {
        case 3:
            //_product_attributes
            $_product_attributes_ser_ds = $attributes;
            if (is_serialized($_product_attributes_ser_ds)) {
                $_product_attributes = unserialize($_product_attributes_ser_ds);
                foreach ($_product_attributes as $key => $attr) {
                    if ($attr['name'] == 'Availability') {
                        $availabilityST = $attr['value'];
                        break;
                    }
                }
            }
            break;
        case 4:
            //_product_attributes            
            $tmp_availability = $prod->get_meta('_custom_availability');
            if ($tmp_availability != '') {
                $availabilityST = $tmp_availability;
            }
            break;
        default:
            break;
    }
    $xml_rows[$prod->get_id()]['availabilityST'] = $availabilityST;
    $price = $prod->get_price();
    $xml_rows[$prod->get_id()]['price_raw'] = $price;
    if ($format_price && $price!='') {
        $price = number_format(floatval($price), $decimals, $decimal_separator, $thousand_separator);
    }
    $xml_rows[$prod->get_id()]['price'] = addslashes($price);
    $xml_rows[$prod->get_id()]['image_ds'] = $image_ds; 
    $image_big = get_the_post_thumbnail_url($prod->get_id(), 'shop_single_image_size');
    $xml_rows[$prod->get_id()]['image_ds'] = $image_big;       
    $skus_ds = $prod->get_sku();        
    $xml_rows[$prod->get_id()]['skus_ds'] = $skus_ds;
    $categories_ds = $prod->get_category_ids();
    $attributes = $prod->get_attributes();
    if ( $prod->has_weight() ) {

        $_weight_ds = $prod->get_weight();
        $xml_rows[$prod->get_id()]['_weight_ds'] = $_weight_ds;
    }
    $sizestring = '';
    $xml_rows[$prod->get_id()]['sizes'] = array();
    if(count($variation_atts[$skroutz_atts_size])>0){

        foreach ($variation_atts[$skroutz_atts_size] as $size_term) {                      
            $sizestring .= format_number_skroutz($size_term) . ', ';
            $xml_rows[$prod->get_id()]['sizes'][] = format_number_skroutz($size_term);
        }
    }
    else{
        if (isset($attributes[$skroutz_atts_size]) && $attributes[$skroutz_atts_size] != null) {
            $sizes = $attributes[$skroutz_atts_size]->get_terms();
            foreach ($sizes as $i => $size_term) {
                $sizestring .= format_number_skroutz($size_term->name) . ', ';
                $xml_rows[$prod->get_id()]['sizes'][] = format_number_skroutz($size_term->name);
            }
        }
    }
    if (strlen($sizestring) > 2) {
        $sizestring = substr($sizestring, 0, -2);
    }
    $xml_rows[$prod->get_id()]['sizestring'] = $sizestring;
    $man = '';
    if (isset($attributes[$skroutz_atts_manuf]) && $attributes[$skroutz_atts_manuf] != null) {
        $brands = $attributes[$skroutz_atts_manuf]->get_terms();
        foreach ($brands as $brand_term) {
            $man = $brand_term->name;
        }
    }
    $xml_rows[$prod->get_id()]['manufacturer'] = $man;
    $colorRes = '';
    $xml_rows[$prod->get_id()]['colors'] = array();

  if(count($variation_atts[$skroutz_atts_color])>0){
        foreach ($variation_atts[$skroutz_atts_color] as $color_term) {                    
            $xml_rows[$prod->get_id()]['colors'][] = format_number_skroutz($color_term);
            $colorRes .= $color_term . ', ';
        }
    }
    else{
        if (isset($attributes[$skroutz_atts_color]) && $attributes[$skroutz_atts_color] != null) {
            $colors = $attributes[$skroutz_atts_color]->get_terms();
            foreach ($colors as $color_term) {
                $colorRes .= $color_term->name . ', ';
                $xml_rows[$prod->get_id()]['colors'][] = $color_term->name;
            }
        }
    }
    if (strlen($colorRes) > 2) {
        $colorRes = substr($colorRes, 0, -2);
    }
    $xml_rows[$prod->get_id()]['colorstring'] = $colorRes;
    $xml_rows[$prod->get_id()]['terms'] = array();

 foreach ($attributes as $att_key => $prod_att) {
        $xml_rows[$prod->get_id()]['terms'][$att_key] = array();
        $prod_terms = $prod_att->get_terms();
        //print_r($prod_terms);
        if(is_array($prod_terms)){
            foreach ($prod_terms as $the_term) {
                $xml_rows[$prod->get_id()]['terms'][$att_key][] = $the_term->name;
            }
        }

    }
    $prod_category_tree = array_map('get_term', array_reverse(wc_get_product_cat_ids($prod->get_id())));
    $xml_rows[$prod->get_id()]['categories'] = array();
    $category_path = '';
    for ($i = 0; $i < count($prod_category_tree); $i++) {
        if ($i == 0) {
            $xml_rows[$prod->get_id()]['category_id'] = $prod_category_tree[$i]->term_id;
        }
        $category_path.=$prod_category_tree[$i]->name;
        $xml_rows[$prod->get_id()]['categories'][] = $prod_category_tree[$i]->name;
        if ($i < count($prod_category_tree) - 1)
            $category_path.=', ';
    }
    $xml_rows[$prod->get_id()]['category_path'] = $category_path;
    $title = str_replace("'", " ", $prod->get_title());
    $title = str_replace("&", "+", $title);
    $title = strip_tags($title);
    $xml_rows[$prod->get_id()]['title'] = $title;
    $backorder = $prod->get_backorders();
    $xml_rows[$prod->get_id()]['backorder'] = $backorder;
    $xml_rows[$prod->get_id()]['descr'] = $prod->get_short_description();
}
return $xml_rows;

}

到目前为止我做了什么:

我试过并清空缓存:没有结果 我检查了属性标题的拼写:没有结果 我unistalled插件并再次安装

xml的部分显示以下内容

<price>92.90</price>
<instock>Y</instock>
<availability>attribute</availability>

为什么这个部分

if ($attr['name'] == 'Availability') 
{
    $availabilityST = $attr['value'];
    break;
}

没有获取正确的值??

0 个答案:

没有答案