Exemplo n.º 1
0
function product_render(&$IDS, &$attributes, $op, &$df = null)
{
    global $wpdb, $custom_fileds;
    $fcols = array();
    foreach ($custom_fileds as $cfname => $cfield) {
        if ($cfield->type == "post") {
            $fcols[] = $cfield->source;
        }
    }
    $id_list = implode(",", $IDS);
    if (!$id_list) {
        $id_list = 9999999;
    }
    $raw_data = $wpdb->get_results("select ID, post_name " . (!empty($fcols) ? "," . implode(",", $fcols) : "") . " from {$wpdb->posts} where ID in (" . $id_list . ")", OBJECT_K);
    $p_n = 0;
    foreach ($IDS as $id) {
        $prod = new stdClass();
        $prod->id = $id;
        if (!isset($_REQUEST["do_export"])) {
            $prod->type = get_post_type($id);
            $prod->parent = get_ancestors($id, 'product');
            if (!empty($prod->parent)) {
                $prod->parent = $prod->parent[0];
            } else {
                $prod->parent = null;
            }
        }
        if (fn_show_filed('sku')) {
            $prod->sku = get_post_meta($id, '_sku', true);
        }
        if (fn_show_filed('slug')) {
            $prod->slug = urldecode($raw_data[$id]->post_name);
        }
        $name_suffix = '';
        if (fn_show_filed('name')) {
            if ($prod->parent) {
                $prod->name = get_the_title($prod->parent) . " (";
                $name_suffix = " variation #" . $id . ')';
            } else {
                $prod->name = get_the_title($id);
            }
        }
        if (fn_show_filed('stock')) {
            $prod->stock = get_post_meta($id, '_stock', true);
        }
        //_manage_stock - null if not
        if (fn_show_filed('stock_status')) {
            if (isset($_REQUEST["do_export"])) {
                $prod->stock_status = get_post_meta($id, '_stock_status', true);
            } else {
                $prod->stock_status = get_post_meta($id, '_stock_status', true) == "instock" ? true : false;
            }
        }
        if (fn_show_filed('categories')) {
            $prod->categories = wp_get_object_terms($id, 'product_cat', array('fields' => 'ids'));
        }
        if (fn_show_filed('shipping_class')) {
            $prod->shipping_class = wp_get_object_terms($id, 'product_shipping_class', array('fields' => 'ids'));
        }
        if (!isset($_REQUEST["do_export"]) && $prod->parent) {
            if (fn_show_filed('categories')) {
                $prod->categories = wp_get_object_terms($prod->parent, 'product_cat', array('fields' => 'ids'));
            }
            if (fn_show_filed('shipping_class')) {
                if ($prod->shipping_class == -1) {
                    $prod->shipping_class = wp_get_object_terms($prod->parent, 'product_shipping_class', array('fields' => 'ids'));
                }
            }
        }
        if (isset($_REQUEST["do_export"])) {
            if (fn_show_filed('categories')) {
                $prod->categories_names = implode(",", wp_get_object_terms($id, 'product_cat', array('fields' => 'names')));
                unset($prod->categories);
            }
            if (fn_show_filed('shipping_class')) {
                $prod->shipping_class_name = implode(",", wp_get_object_terms($id, 'product_shipping_class', array('fields' => 'names')));
                unset($prod->shipping_class);
            }
            if (fn_show_filed('stock_status')) {
                unset($prod->stock_status);
            }
        }
        if (fn_show_filed('price')) {
            $prod->price = get_post_meta($id, '_regular_price', true);
        }
        if (fn_show_filed('override_price')) {
            $prod->override_price = get_post_meta($id, '_sale_price', true);
        }
        if (fn_show_filed('attributes')) {
            $pa = get_post_meta($id, '_product_attributes', true);
            $att_info = array();
            foreach ($attributes as $att) {
                if (!isset($_REQUEST["do_export"])) {
                    if (isset($pa['pa_' . $att->name])) {
                        $inf = new stdClass();
                        $inf->n = $pa['pa_' . $att->name]["name"];
                        $inf->s = $pa['pa_' . $att->name]["is_visible"];
                        $inf->v = $pa['pa_' . $att->name]["is_variation"];
                        $inf->t = $pa['pa_' . $att->name]["is_taxonomy"];
                        $att_info['pattribute_' . $att->id] = $inf;
                    }
                }
                if ($prod->parent) {
                    $att_value = explode(",", get_post_meta($id, 'attribute_pa_' . $att->name, true));
                    $tnames = array();
                    $tids = array();
                    foreach ($att_value as $tslug) {
                        $term = null;
                        foreach ($att->values as $trm) {
                            if ($trm->slug == $tslug) {
                                $term = $trm;
                                break;
                            }
                        }
                        if ($term) {
                            $tnames[] = $term->name;
                            $tids[] = $term->id;
                        }
                    }
                    if (fn_show_filed('name') && !empty($tnames)) {
                        $prod->name .= " " . implode(",", $tnames);
                    }
                    if (isset($_REQUEST["do_export"])) {
                        $prod->{'pattribute_' . $att->id} = implode(",", $tnames);
                    } else {
                        $prod->{'pattribute_' . $att->id} = $tids;
                    }
                } else {
                    if (isset($_REQUEST["do_export"])) {
                        $prod->{'pattribute_' . $att->id} = wp_get_object_terms($id, 'pa_' . $att->name, array('fields' => 'names'));
                    } else {
                        $prod->{'pattribute_' . $att->id} = wp_get_object_terms($id, 'pa_' . $att->name, array('fields' => 'ids'));
                    }
                }
            }
            if (!isset($_REQUEST["do_export"])) {
                $prod->att_info = $att_info;
            }
        }
        if (fn_show_filed('name')) {
            $prod->name .= $name_suffix;
        }
        foreach ($custom_fileds as $cfname => $cfield) {
            if ($cfield->type == "term") {
                if (isset($_REQUEST["do_export"])) {
                    $prod->{$cfname} = implode(",", wp_get_object_terms($id, $cfield->source, array('fields' => 'names')));
                } else {
                    if ($prod->parent) {
                        $prod->{$cfname} = wp_get_object_terms($prod->parent, $cfield->source, array('fields' => 'ids'));
                    } else {
                        $prod->{$cfname} = wp_get_object_terms($id, $cfield->source, array('fields' => 'ids'));
                    }
                }
            } elseif ($cfield->type == "meta") {
                $prod->{$cfname} = fn_get_meta_by_path($id, $cfield->source);
            } elseif ($cfield->type == "post") {
                $prod->{$cfname} = $raw_data[$id]->{$cfield->source};
            }
        }
        if (fn_show_filed('status')) {
            $prod->status = get_post_status($id);
        }
        $ptrems = get_the_terms($id, 'product_tag');
        if (fn_show_filed('tags')) {
            if (isset($_REQUEST["do_export"])) {
                $prod->tags_names = null;
                if ($ptrems) {
                    foreach ((array) $ptrems as $pt) {
                        if (!isset($prod->tags_names)) {
                            $prod->tags_names = array();
                        }
                        $prod->tags_names[] = $pt->name;
                    }
                    $prod->tags_names = implode(",", $prod->tags_names);
                }
            } else {
                $prod->tags = null;
                if ($ptrems) {
                    foreach ((array) $ptrems as $pt) {
                        if (!isset($prod->tags)) {
                            $prod->tags = array();
                        }
                        $prod->tags[] = $pt->term_id;
                    }
                }
            }
        }
        if (fn_show_filed('weight')) {
            $prod->weight = get_post_meta($id, '_weight', true);
        }
        if (fn_show_filed('length')) {
            $prod->length = get_post_meta($id, '_length', true);
        }
        if (fn_show_filed('width')) {
            $prod->width = get_post_meta($id, '_width', true);
        }
        if (fn_show_filed('height')) {
            $prod->height = get_post_meta($id, '_height', true);
        }
        if (fn_show_filed('featured')) {
            if (isset($_REQUEST["do_export"])) {
                $prod->featured = get_post_meta($id, '_featured', true);
            } else {
                $prod->featured = get_post_meta($prod->parent ? $prod->parent : $id, '_featured', true) == "yes" ? true : false;
            }
        }
        if (fn_show_filed('tax_status')) {
            $prod->tax_status = get_post_meta($prod->parent && !isset($_REQUEST["do_export"]) ? $prod->parent : $id, '_tax_status', true);
        }
        if (fn_show_filed('tax_class')) {
            $prod->tax_class = get_post_meta($prod->parent && !isset($_REQUEST["do_export"]) ? $prod->parent : $id, '_tax_class', true);
        }
        if (fn_show_filed('backorders')) {
            $prod->backorders = get_post_meta($prod->parent && !isset($_REQUEST["do_export"]) ? $prod->parent : $id, '_backorders', true);
        }
        if (fn_show_filed('image') && !isset($_REQUEST["do_export"])) {
            if (has_post_thumbnail($id)) {
                $thumb_id = get_post_thumbnail_id($id);
                $prod->image = get_post_meta($thumb_id, '_wp_attached_file', true);
            } else {
                $prod->image = null;
            }
        }
        if ($op == "json") {
            if ($p_n > 0) {
                echo ",";
            }
            $out = json_encode($prod);
            if ($out) {
                echo $out;
            } else {
                echo "/*ERROR json_encode product ID {$id}*/";
            }
        } elseif ($op == "export") {
            if ($p_n == 0) {
                $pprops = (array) $prod;
                $props = array();
                foreach ($pprops as $key => $pprop) {
                    $props[] = $key;
                }
                fputcsv($df, $props);
            }
            fputcsv($df, (array) $prod);
        }
        $p_n++;
        unset($prod);
    }
}
Exemplo n.º 2
0
function product_render(&$IDS, $op, &$df = null)
{
    global $wpdb, $custom_fileds;
    $fcols = array();
    foreach ($custom_fileds as $cfname => $cfield) {
        if ($cfield->type == "post") {
            $fcols[] = $cfield->source;
        }
    }
    $id_list = implode(",", $IDS);
    if (!$id_list) {
        $id_list = 9999999;
    }
    $raw_data = $wpdb->get_results("select ID, post_name " . (!empty($fcols) ? "," . implode(",", $fcols) : "") . " from {$wpdb->posts} where ID in (" . $id_list . ")", OBJECT_K);
    $p_n = 0;
    foreach ($IDS as $id) {
        $prod = new stdClass();
        $prod->id = $id;
        if (!isset($_REQUEST["do_export"])) {
            $prod->type = get_post_type($id);
            $prod->parent = get_ancestors($id, 'wpsc-product');
            if (!empty($prod->parent)) {
                $prod->parent = $prod->parent[0];
            } else {
                $prod->parent = null;
            }
        }
        if (fn_show_filed('sku')) {
            $prod->sku = get_post_meta($id, '_wpsc_sku', true);
        }
        if (fn_show_filed('slug')) {
            $prod->slug = urldecode($raw_data[$id]->post_name);
        }
        if (fn_show_filed('categories')) {
            $prod->categories = wp_get_object_terms($id, 'wpsc_product_category', array('fields' => 'ids'));
        }
        if (!isset($_REQUEST["do_export"]) && $prod->parent) {
            if (fn_show_filed('categories')) {
                $prod->categories = wp_get_object_terms($prod->parent, 'wpsc_product_category', array('fields' => 'ids'));
            }
        }
        if (isset($_REQUEST["do_export"])) {
            if (fn_show_filed('categories')) {
                $prod->categories_names = implode(",", wp_get_object_terms($id, 'wpsc_product_category', array('fields' => 'names')));
                unset($prod->categories);
            }
        }
        if (fn_show_filed('name')) {
            $prod->name = get_the_title($id);
        }
        if (fn_show_filed('stock')) {
            $prod->stock = get_post_meta($id, '_wpsc_stock', true);
            if (!$prod->stock) {
                $prod->stock = '';
            }
        }
        if (fn_show_filed('price')) {
            $prod->price = get_post_meta($id, '_wpsc_price', true);
        }
        if (fn_show_filed('override_price')) {
            $prod->override_price = get_post_meta($id, '_wpsc_special_price', true);
        }
        foreach ($custom_fileds as $cfname => $cfield) {
            if ($cfield->type == "term") {
                if (isset($_REQUEST["do_export"])) {
                    $prod->{$cfname} = implode(",", wp_get_object_terms($id, $cfield->source, array('fields' => 'names')));
                } else {
                    if ($prod->parent) {
                        $prod->{$cfname} = wp_get_object_terms($prod->parent, $cfield->source, array('fields' => 'ids'));
                    } else {
                        $prod->{$cfname} = wp_get_object_terms($id, $cfield->source, array('fields' => 'ids'));
                    }
                }
            } elseif ($cfield->type == "meta") {
                $prod->{$cfname} = fn_get_meta_by_path($id, $cfield->source);
            } elseif ($cfield->type == "post") {
                $prod->{$cfname} = $raw_data[$id]->{$cfield->source};
            }
        }
        if (fn_show_filed('status')) {
            $prod->status = get_post_status($id);
        }
        $ptrems = get_the_terms($id, 'product_tag');
        if (fn_show_filed('tags')) {
            if (isset($_REQUEST["do_export"])) {
                $prod->tags_names = null;
                if ($ptrems) {
                    foreach ((array) $ptrems as $pt) {
                        if (!isset($prod->tags_names)) {
                            $prod->tags_names = array();
                        }
                        $prod->tags_names[] = $pt->name;
                    }
                    $prod->tags_names = implode(",", $prod->tags_names);
                }
            } else {
                $prod->tags = null;
                if ($ptrems) {
                    foreach ((array) $ptrems as $pt) {
                        if (!isset($prod->tags)) {
                            $prod->tags = array();
                        }
                        $prod->tags[] = $pt->term_id;
                    }
                }
            }
        }
        $pr_meta = get_post_meta($id, '_wpsc_product_metadata', true);
        $dimensions =& $pr_meta['dimensions'];
        if (fn_show_filed('weight')) {
            $prod->weight = isset($pr_meta['weight']) ? round(fn_convert_unit($pr_meta['weight'], 'pound', $pr_meta['weight_unit']), 2) . ' ' . $pr_meta['weight_unit'] : "";
        }
        if (fn_show_filed('height')) {
            $prod->height = isset($dimensions['height']) ? $dimensions['height'] . ' ' . $dimensions['height_unit'] : "";
        }
        if (fn_show_filed('width')) {
            $prod->width = isset($dimensions['width']) ? $dimensions['width'] . ' ' . $dimensions['width_unit'] : "";
        }
        if (fn_show_filed('length')) {
            $prod->length = isset($dimensions['length']) ? $dimensions['length'] . ' ' . $dimensions['length_unit'] : "";
        }
        if (fn_show_filed('taxable')) {
            $prod->taxable = isset($pr_meta['wpec_taxes_taxable_amount']) ? $pr_meta['wpec_taxes_taxable_amount'] : "";
        }
        if (fn_show_filed('loc_shipping')) {
            $prod->loc_shipping = isset($pr_meta['shipping']) ? $pr_meta['shipping']['local'] : "";
        }
        if (fn_show_filed('int_shipping')) {
            $prod->int_shipping = isset($pr_meta['shipping']) ? $pr_meta['shipping']['international'] : "";
        }
        if (fn_show_filed('image') && !isset($_REQUEST["do_export"])) {
            if (has_post_thumbnail($id)) {
                $thumb_id = get_post_thumbnail_id($id);
                $prod->image = get_post_meta($thumb_id, '_wp_attached_file', true);
            } else {
                $prod->image = null;
            }
        }
        if ($op == "json") {
            if ($p_n > 0) {
                echo ",";
            }
            $out = json_encode($prod);
            if ($out) {
                echo $out;
            } else {
                echo "/*ERROR json_encode product ID {$id}*/";
            }
        } elseif ($op == "export") {
            if ($p_n == 0) {
                $pprops = (array) $prod;
                $props = array();
                foreach ($pprops as $key => $pprop) {
                    $props[] = $key;
                }
                fputcsv($df, $props);
            }
            fputcsv($df, (array) $prod);
        }
        $p_n++;
        unset($prod);
    }
}