| 1 |
<?php namespace TierPricingTable\Addons\RoleBasedPricing\Export; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\RoleBasedPricing\RoleBasedPriceManager; |
| 4 |
use TierPricingTable\TierPricingTablePlugin; |
| 5 |
use WC_Product; |
| 6 |
|
| 7 |
class RoleBasedPricingExport { |
| 8 |
|
| 9 |
/** |
| 10 |
* Export constructor. |
| 11 |
*/ |
| 12 |
public function __construct() { |
| 13 |
add_filter( 'woocommerce_product_export_column_names', array( $this, 'addExportColumn' ), 999 ); |
| 14 |
add_filter( 'woocommerce_product_export_product_default_columns', array( $this, 'addExportColumn' ), 999 ); |
| 15 |
|
| 16 |
add_action( 'init', function () { |
| 17 |
|
| 18 |
if ( ! is_admin() ) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
$columns = array(); |
| 23 |
$disabled_roles = apply_filters( 'tiered_pricing_table/role_based_rules/import_export_disabled_roles', array( 'editor', 'author', 'contributor', 'shop_manager' ) ); |
| 24 |
|
| 25 |
foreach ( wp_roles()->roles as $WPRole => $role_data ) { |
| 26 |
if ( in_array( $WPRole, $disabled_roles, true ) ) { |
| 27 |
continue; |
| 28 |
} |
| 29 |
|
| 30 |
$columns[] = array( |
| 31 |
'column_title' => $WPRole . '_tiered_price_pricing_type', |
| 32 |
'export_callback' => function ( $value, WC_Product $product ) use ( $WPRole ) { |
| 33 |
return RoleBasedPriceManager::getProductPricingType( $product->get_id(), $WPRole, 'edit' ); |
| 34 |
}, |
| 35 |
); |
| 36 |
|
| 37 |
$columns[] = array( |
| 38 |
'column_title' => $WPRole . '_tiered_price_regular_price', |
| 39 |
'export_callback' => function ( $value, WC_Product $product ) use ( $WPRole ) { |
| 40 |
return RoleBasedPriceManager::getProductRegularRolePrice( $product->get_id(), $WPRole, 'edit' ); |
| 41 |
}, |
| 42 |
); |
| 43 |
|
| 44 |
$columns[] = array( |
| 45 |
'column_title' => $WPRole . '_tiered_price_sale_price', |
| 46 |
'export_callback' => function ( $value, WC_Product $product ) use ( $WPRole ) { |
| 47 |
return RoleBasedPriceManager::getProductSaleRolePrice( $product->get_id(), $WPRole, 'edit' ); |
| 48 |
}, |
| 49 |
); |
| 50 |
|
| 51 |
$columns[] = array( |
| 52 |
'column_title' => $WPRole . '_tiered_price_discount', |
| 53 |
'export_callback' => function ( $value, WC_Product $product ) use ( $WPRole ) { |
| 54 |
return RoleBasedPriceManager::getProductDiscount( $product->get_id(), $WPRole, 'edit' ); |
| 55 |
}, |
| 56 |
); |
| 57 |
|
| 58 |
$columns[] = array( |
| 59 |
'column_title' => $WPRole . '_tiered_price_fixed', |
| 60 |
'export_callback' => function ( $value, WC_Product $product ) use ( $WPRole ) { |
| 61 |
$fixedRules = RoleBasedPriceManager::getPriceRules( $product->get_id(), $WPRole, 'fixed' ); |
| 62 |
|
| 63 |
$str = ''; |
| 64 |
$separator = TierPricingTablePlugin::getRulesSeparator(); |
| 65 |
|
| 66 |
foreach ( $fixedRules as $quantity => $price ) { |
| 67 |
$str .= $quantity . ':' . $price . $separator; |
| 68 |
} |
| 69 |
|
| 70 |
return mb_strlen( $str ) > 0 ? trim( $str, $separator ) : null; |
| 71 |
}, |
| 72 |
); |
| 73 |
|
| 74 |
$columns[] = array( |
| 75 |
'column_title' => $WPRole . '_tiered_price_percentage', |
| 76 |
'export_callback' => function ( $value, WC_Product $product ) use ( $WPRole ) { |
| 77 |
$fixedRules = RoleBasedPriceManager::getPriceRules( $product->get_id(), $WPRole, 'percentage' ); |
| 78 |
|
| 79 |
$str = ''; |
| 80 |
$separator = TierPricingTablePlugin::getRulesSeparator(); |
| 81 |
|
| 82 |
foreach ( $fixedRules as $quantity => $discount ) { |
| 83 |
$str .= $quantity . ':' . $discount . $separator; |
| 84 |
} |
| 85 |
|
| 86 |
return mb_strlen( $str ) > 0 ? trim( $str, $separator ) : null; |
| 87 |
}, |
| 88 |
); |
| 89 |
|
| 90 |
$columns[] = array( |
| 91 |
'column_title' => $WPRole . '_tiered_price_type', |
| 92 |
'export_callback' => function ( $value, WC_Product $product ) use ( $WPRole ) { |
| 93 |
return RoleBasedPriceManager::getPricingType( $product->get_id(), $WPRole, 'edit' ); |
| 94 |
}, |
| 95 |
); |
| 96 |
|
| 97 |
$columns[] = array( |
| 98 |
'column_title' => $WPRole . '_tiered_price_minimum', |
| 99 |
'export_callback' => function ( $value, WC_Product $product ) use ( $WPRole ) { |
| 100 |
return RoleBasedPriceManager::getProductQtyMin( $product->get_id(), $WPRole, 'edit' ); |
| 101 |
}, |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
foreach ( $columns as $column ) { |
| 106 |
add_filter( 'woocommerce_product_export_product_column_' . $column['column_title'], |
| 107 |
$column['export_callback'], 20, 2 ); |
| 108 |
} |
| 109 |
|
| 110 |
} ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Register the 'Fixed tiered price' column in the exporter. |
| 115 |
*/ |
| 116 |
public function addExportColumn( array $columns ): array { |
| 117 |
|
| 118 |
global $wp_roles; |
| 119 |
$disabled_roles = apply_filters( 'tiered_pricing_table/role_based_rules/import_export_disabled_roles', array( 'editor', 'author', 'contributor', 'shop_manager' ) ); |
| 120 |
|
| 121 |
foreach ( wp_roles()->roles as $WPRole => $role_data ) { |
| 122 |
if ( in_array( $WPRole, $disabled_roles, true ) ) { |
| 123 |
continue; |
| 124 |
} |
| 125 |
|
| 126 |
$roleName = isset( $wp_roles->role_names[ $WPRole ] ) ? translate_user_role( $wp_roles->role_names[ $WPRole ] ) : $WPRole; |
| 127 |
|
| 128 |
$columns[ $WPRole . '_tiered_price_pricing_type' ] = 'Tiered Pricing — ' . ' [' . $roleName . '] ' . __( 'Regular pricing type', |
| 129 |
'tier-pricing-table' ); |
| 130 |
$columns[ $WPRole . '_tiered_price_regular_price' ] = 'Tiered Pricing — ' . ' [' . $roleName . '] ' . __( 'Regular price', |
| 131 |
'tier-pricing-table' ); |
| 132 |
$columns[ $WPRole . '_tiered_price_sale_price' ] = 'Tiered Pricing — ' . ' [' . $roleName . '] ' . __( 'Sale price', |
| 133 |
'tier-pricing-table' ); |
| 134 |
$columns[ $WPRole . '_tiered_price_discount' ] = 'Tiered Pricing — ' . ' [' . $roleName . '] ' . __( 'Percentage discount', |
| 135 |
'tier-pricing-table' ); |
| 136 |
$columns[ $WPRole . '_tiered_price_fixed' ] = 'Tiered Pricing — ' . ' [' . $roleName . '] ' . __( 'Fixed pricing rules', |
| 137 |
'tier-pricing-table' ); |
| 138 |
$columns[ $WPRole . '_tiered_price_percentage' ] = 'Tiered Pricing — ' . ' [' . $roleName . '] ' . __( 'Percentage pricing rules', |
| 139 |
'tier-pricing-table' ); |
| 140 |
$columns[ $WPRole . '_tiered_price_type' ] = 'Tiered Pricing — ' . ' [' . $roleName . '] ' . __( 'Tiered pricing type', |
| 141 |
'tier-pricing-table' ); |
| 142 |
$columns[ $WPRole . '_tiered_price_minimum' ] = 'Tiered Pricing — ' . ' [' . $roleName . '] ' . __( 'Minimum order quantity', |
| 143 |
'tier-pricing-table' ); |
| 144 |
} |
| 145 |
|
| 146 |
return $columns; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|