WooCommerce Dynamic Pricing – Import a export slev

WooCommerce Dynamic Pricing je skvělý plugin pro WordPress, který umožňuje nastavovat různé slevy a ceny pro jednotlivé skupiny uživatelů, role, kategorie, i jednotlivé produkty. Je také možné definovat spoustu pravidel typu: „Pokud uživatel ze skupina A koupí 3ks produktu z kategorie B, má slevu 10 procent na celou objednávku“.

Pro web gsklub.sk společnosti GREEN-SWAN PHARMACEUTICALS SR, pro niž jsem již realizoval spoustu úprav a napojení jejich webů, bylo potřeba napsat plugin, díky kterému bude možné ceny produktů pro jednotlivé skupiny importovat. Na kód, který to zařídí, se můžete podívat níže.

Přidal jsem stránku, kde je možné vyexportovat a následně importovat CSV.
Plugin podporuje jen fixní ceny produtků (není tedy aktuálně možné zadat procentuelní slevu), to by v případě potřeby nebyl problém přidat. Administrátorům eshopu gsklub.sk se díky možnosti importu slev výrazně zjednodušila správa produktů.

Pokud budete potřebovat pomoci s importem slev do WooCommerce Dynamic Pricing nebo s jinými úpravami WooCommerce, neváhejte se na mě obrátit.

[php]
<?php
namespace GS_Club;
use League\Csv\Reader;
use League\Csv\Writer;

class DynamicPricing {
function initialize() {
add_action(‚init‘,array($this,’route‘));
add_action( ‚admin_menu‘, array($this,’add_admin_page‘) );
}

/**
* Check if we should do the import or export
*/
function route() {
if (!is_admin() || !current_user_can(‚administrator‘))
return;

if (isset($_GET[‚dp-export‘])) {
$this->export_rules_to_csv();
} else if (isset($_POST[‚dp-import‘])) {
$target_file = GS_PLUGIN_DIR . ‚tmp/‘.basename($_FILES["dp_csv"]["name"]);

move_uploaded_file($_FILES["dp_csv"]["tmp_name"], $target_file);
$this->import_rules_from_csv($target_file);
unlink($target_file);
add_action( ‚admin_notices‘, array($this,’import_success_notice‘));

}
}

/**
* Add submenu page to WooCommerce
*/
function add_admin_page() {
add_submenu_page( ‚woocommerce‘, ‚Dynamic Pricing Import / Export‘, ‚Dynamic Pricing Import / Export‘, ‚manage_options‘, ‚dp-import-export‘, array($this,’render_admin_page‘ ));
}

/**
* Display success notice on import
*/
function import_success_notice() {
?>
<div class="notice notice-success is-dismissible">
<p><?php _e( ‚Import finished!‘, ‚gsc‘ ); ?></p>
</div>
<?php
}

/**
* Render the admin page
*/
function render_admin_page() { ?>
<div class="wrap">
<h2>Dynamic Pricing Import / Export</h2>
<h3>Export</h3>
<p>Click on the Export button to export pricing rules to CSV</p>
<a target="_blank" href="<?php echo admin_url();?>?dp-export" class="button">Export CSV</a>
<h3>Import</h3>
<p>Select CSV file and click the button to import the prices from CSV</p>
<form action="" enctype="multipart/form-data" method="post">
<input type="file" name="dp_csv"/>
<input type="hidden" name="dp-import"/>
<input type="submit" value="Import" class="button"/>
</form>
</div>

<?php }

/**
* Export Dynamic Pricing Rules to CSV
*/
function export_rules_to_csv () {
// Initialize the CSV writer
$csv = Writer::createFromString(“);
$csv->setDelimiter(‚;‘);

// Get all products
$products = wc_get_products(array(‚limit‘ => -1));
$gr = new Groups();
$groups = $gr->get_all_groups();

// Build the header row
$header[‚description‘] = array(‚Product name‘,’Product ID‘);
$header[‚header‘] = array(‚title‘,’product_id‘);
foreach ($groups as $group) {
$header[‚description‘][] = ‚Price for group ‚.$group->name;
$header[‚header‘][] = ‚discount_‘.$group->group_id;
}
// Insert the header to CSV

$csv->insertAll($header);

$rows = [];

foreach ($products as $product) {
/** @var $product \WC_Product */
$rules = $product->get_meta(‚_pricing_rules‘);
// Start building the row data
$product_row = [];
$product_row[‚title‘] = $product->get_title();
$product_row[‚product_id‘] = $product->get_id();
// Check if we got some rules
if ($rules) {
// If so, scrub through the rules and find out Amount and GroupID
// Warning: this works ONLY for single group per rule selected AND fixed amount
// If you select more groups per rule in admin, or select different method of dicsount (ie. percentage), this won’t work
foreach ($rules as $rule) {
$group_id = false;
$amount = false;

// Find out the amount
foreach ($rule[‚rules‘] as $single_rule) {
if (isset($single_rule[‚amount‘])) {
$amount = $single_rule[‚amount‘];
}
}

// Find out the group ID
foreach ($rule[‚conditions‘] as $condition) {
if (isset($condition[‚args‘][‚groups‘][0])) {
$group_id = $condition[‚args‘][‚groups‘][0];
break;
}
}

// If we got group ID and Amount, add it to the row
if ($group_id && $amount) {
$product_row[‚discount_‘.$group_id] = $amount;
}

}
}

// Sort the values to havr them in correct order in the CSV
$sorted_product_row = [];
foreach ($header[‚header‘] as $header_key) {
if (isset($product_row[$header_key])) {
$sorted_product_row[$header_key] = $product_row[$header_key];
} else {
$sorted_product_row[$header_key] = “;
}
}

// Add the row to the rows array
$rows[] = $sorted_product_row;
}

// Insert all products to csv and download it
$csv->insertAll($rows);
header(‚Content-Type: text/csv; charset=UTF-8‘);
header(‚Content-Description: File Transfer‘);
header(‚Content-Disposition: attachment; filename="name-for-your-file.csv"‘);
$csv->output(‚dynamic_pricing_‘.date(‚Ymdhi‘).‘.csv‘);
die;
}

/**
* Import Dynamic pricing rules from CSV
* This overwrites all the rules that have been set!
* Supports ONLY the CSV format that you can get by exporting the rules
* Supports ONLY fixed prices per Group
*/
function import_rules_from_csv($file) {
// Read the CSV
$csv = Reader::createFromPath($file)->setHeaderOffset(1)->setDelimiter(‚;‘);
$i = 0;
// Scrub through the records
foreach ($csv as $record) {
$i++;
// Skip the first record, that is description
if ($i == 1)
continue;
$rules = [];

foreach ($record as $key => $column) {
// Check ig this is a discount column and if there’s some price
if (strpos($key,’discount_‘) !== FALSE && !empty($column)) {
// If so, get a single rule and add it to the rules array
$rules[uniqid( ‚set_‘ )] = $this->get_dynamic_pricing_single_rule(str_replace(‚discount_‘,“,$key), $column);
}
}

// Update product rules
update_post_meta($record[‚product_id‘],’_pricing_rules‘,$rules);
}
}

/**
* Get the rule array
* This is copied from an existing Dynamic pricing rule and the structure might change in the future!
* @param $group_id
* @param $price
* @return array
*/
function get_dynamic_pricing_single_rule($group_id,$price) {
return array (
‚conditions_type‘ => ‚all‘,
‚conditions‘ =>
array (
1 =>
array (
‚type‘ => ‚apply_to‘,
‚args‘ =>
array (
‚applies_to‘ => ‚groups‘,
‚groups‘ =>
array (
0 => $group_id,
),
),
),
),
‚collector‘ =>
array (
‚type‘ => ‚product‘,
),
‚mode‘ => ‚continuous‘,
‚date_from‘ => “,
‚date_to‘ => “,
‚rules‘ =>
array (
1 =>
array (
‚from‘ => ‚0‘,
‚to‘ => “,
‚type‘ => ‚fixed_price‘,
‚amount‘ => $price,
),
),
‚blockrules‘ =>
array (
1 =>
array (
‚from‘ => “,
‚adjust‘ => “,
‚type‘ => ‚fixed_adjustment‘,
‚amount‘ => “,
‚repeating‘ => ‚no‘,
),
)

);
}
}

$dp = new DynamicPricing();
$dp->initialize();

[/php]

1 Comment

  1. hosting wordpress on 13.2.2018 at 12:59

    Hello!

    Thanks for share your plugin

    when i try to export i get an 500 error in the url /wp-admin/?dp-export

Přidat komentář