Untitled
unknown
plain_text
2 years ago
4.4 kB
10
Indexable
function b2bkingdownloadpricelist(){
// Check security nonce.
if ( ! check_ajax_referer( 'b2bking_security_nonce', 'security' ) ) {
wp_send_json_error( 'Invalid security token sent.' );
wp_die();
}
// Capability check
if (!current_user_can( apply_filters('b2bking_backend_capability_needed', 'manage_woocommerce') )){
wp_send_json_error( 'Failed capability check.' );
wp_die();
}
// build and download list
global $wpdb;
$tableprefix = $wpdb->prefix;
$table_name = $tableprefix.'posts';
if (apply_filters('b2bking_export_price_list_all_products', false)){
$queryresult = $wpdb->get_results(
"
SELECT `id` FROM $table_name WHERE (post_type = 'product' OR post_type = 'product_variation')
"
, ARRAY_N);
} else {
$queryresult = $wpdb->get_results(
"
SELECT `id` FROM $table_name WHERE post_status = 'publish' AND (post_type = 'product' OR post_type = 'product_variation')
"
, ARRAY_N);
}
// get all groups
$groups = get_posts( array( 'post_type' => 'b2bking_group','post_status'=>'publish','numberposts' => -1) );
if (defined('B2BKINGLABEL_DIR')){
$filename = strtolower(get_option('b2bking_whitelabel_pluginname_setting', 'B2BKing')).'_price_list.csv';
} else {
$filename = 'b2bking_price_list.csv';
}
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=".$filename);
header("Pragma: no-cache");
header("Expires: 0");
$output = fopen("php://output", "wb");
// build header
$headerrow = array("Product or Variation ID / SKU");
// Regular and Sale Price and Tiered B2C:
array_push($headerrow, esc_html__('Regular Price'));
array_push($headerrow, esc_html__('Sale Price'));
array_push($headerrow, esc_html__('Tiered Price (Qty:Price;)'));
foreach ($groups as $group){
array_push($headerrow, $group->ID.': '.$group->post_title.' '.esc_html__('Regular Price'));
array_push($headerrow, $group->ID.': '.$group->post_title.' '.esc_html__('Sale Price'));
array_push($headerrow, $group->ID.': '.$group->post_title.' '.esc_html__('Tiered Price'));
}
fputcsv($output, $headerrow);
// build rows
foreach ($queryresult as $key => $value){
$id = intval($value[0]);
$offer_id = intval(get_option('b2bking_offer_product_id_setting', 0));
$credit_id = intval(get_option('b2bking_credit_product_id_setting', 0));
$mkcredit_id = intval(get_option('marketking_credit_product_id_setting', 0));
if ($id !== 0 && $id !== $mkcredit_id && $id !== $offer_id && $id !== $credit_id){ // deprecated offer nr
$temparray = array();
// set title
$product_title = get_the_title($value[0]);
$productobj = wc_get_product($value[0]);
if (is_a($productobj,'WC_Product_Variation')){
$attributes = $productobj->get_variation_attributes();
$number_of_attributes = count($attributes);
if ($number_of_attributes > 2){
$product_title = $productobj->get_name();
$product_title.=' - ';
foreach ($attributes as $attribute){
if (!empty($attribute)){
$product_title.=$attribute.', ';
}
}
$product_title = substr($product_title, 0, -2);
} else {
// remove –
$product_title = str_replace('–', '-', $product_title);
}
}
$skuval = $productobj->get_sku();
if (!empty($skuval)){
$product_title.=' (SKU: '.$skuval.' )';
}
// add title
array_push($temparray,$value[0].': '.$product_title);
// add regular and sale price and tiered price
$reg_price = get_post_meta($value[0],'_regular_price', true);
$sal_price = get_post_meta($value[0],'_sale_price', true);
$tie_price = get_post_meta($value[0],'b2bking_product_pricetiers_group_b2c', true);
array_push($temparray, $reg_price);
array_push($temparray, $sal_price);
array_push($temparray, $tie_price);
foreach ($groups as $group){
$group_price = get_post_meta($value[0],'b2bking_regular_product_price_group_'.$group->ID, true);
array_push($temparray, $group_price);
$group_price = get_post_meta($value[0],'b2bking_sale_product_price_group_'.$group->ID, true);
array_push($temparray, $group_price);
$tiered_price = get_post_meta($value[0],'b2bking_product_pricetiers_group_'.$group->ID, true);
array_push($temparray, $tiered_price);
}
fputcsv($output, $temparray);
}
}
fclose($output);
exit();
}Editor is loading...
Leave a Comment