| 1 |
<?php namespace TierPricingTable\Addons\RoleBasedPricing\Import; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\RoleBasedPricing\RoleBasedPricingRule; |
| 4 |
use TierPricingTable\Addons\ImportExport\WoocommerceImportService; |
| 5 |
use WC_Product; |
| 6 |
|
| 7 |
class RoleBasedPricingImport { |
| 8 |
|
| 9 |
public function __construct() { |
| 10 |
add_filter( 'tiered_pricing_table/import/woocommerce/mapping_screen_columns', |
| 11 |
array( $this, 'addColumnsToMapper' ) ); |
| 12 |
add_filter( 'tiered_pricing_table/import/woocommerce/import_columns', array( $this, 'addColumnsToImporter' ) ); |
| 13 |
add_filter( 'woocommerce_product_import_pre_insert_product_object', array( $this, 'processImport' ), 10, 2 ); |
| 14 |
} |
| 15 |
|
| 16 |
public function processImport( WC_Product $product, array $data ): WC_Product { |
| 17 |
|
| 18 |
$disabled_roles = apply_filters( 'tiered_pricing_table/role_based_rules/import_export_disabled_roles', array( 'editor', 'author', 'contributor', 'shop_manager' ) ); |
| 19 |
|
| 20 |
foreach ( wp_roles()->roles as $WPRole => $role_data ) { |
| 21 |
|
| 22 |
if ( in_array( $WPRole, $disabled_roles, true ) ) { |
| 23 |
continue; |
| 24 |
} |
| 25 |
|
| 26 |
$roleBasedRule = new RoleBasedPricingRule( $product->get_id(), $WPRole ); |
| 27 |
$needUpdate = false; |
| 28 |
|
| 29 |
if ( isset( $data[ $WPRole . '_tiered_price_pricing_type' ] ) ) { |
| 30 |
$roleBasedRule->setPricingType( $data[ $WPRole . '_tiered_price_pricing_type' ] ); |
| 31 |
$needUpdate = true; |
| 32 |
} |
| 33 |
|
| 34 |
if ( isset( $data[ $WPRole . '_tiered_price_regular_price' ] ) ) { |
| 35 |
$regularPrice = floatval( $data[ $WPRole . '_tiered_price_regular_price' ] ); |
| 36 |
|
| 37 |
$roleBasedRule->setRegularPrice( $regularPrice ); |
| 38 |
$needUpdate = true; |
| 39 |
} |
| 40 |
|
| 41 |
if ( isset( $data[ $WPRole . '_tiered_price_sale_price' ] ) ) { |
| 42 |
|
| 43 |
$salePrice = floatval( $data[ $WPRole . '_tiered_price_sale_price' ] ); |
| 44 |
|
| 45 |
$roleBasedRule->setSalePrice( $salePrice ); |
| 46 |
$needUpdate = true; |
| 47 |
} |
| 48 |
|
| 49 |
if ( isset( $data[ $WPRole . '_tiered_price_discount' ] ) ) { |
| 50 |
|
| 51 |
$discount = floatval( $data[ $WPRole . '_tiered_price_discount' ] ); |
| 52 |
|
| 53 |
if ( $discount ) { |
| 54 |
$roleBasedRule->setDiscount( min( 100, $discount ) ); |
| 55 |
} |
| 56 |
|
| 57 |
$needUpdate = true; |
| 58 |
} |
| 59 |
|
| 60 |
if ( isset( $data[ $WPRole . '_tiered_price_fixed' ] ) ) { |
| 61 |
|
| 62 |
$fixedPricingRules = WoocommerceImportService::decodeExport( $data[ $WPRole . '_tiered_price_fixed' ] ); |
| 63 |
$roleBasedRule->setFixedTieredPricingRules( $fixedPricingRules ); |
| 64 |
$needUpdate = true; |
| 65 |
} |
| 66 |
|
| 67 |
if ( isset( $data[ $WPRole . '_tiered_price_percentage' ] ) ) { |
| 68 |
|
| 69 |
$percentagePricingRules = WoocommerceImportService::decodeExport( $data[ $WPRole . '_tiered_price_percentage' ] ); |
| 70 |
|
| 71 |
$roleBasedRule->setPercentageTieredPricingRules( $percentagePricingRules ); |
| 72 |
$needUpdate = true; |
| 73 |
} |
| 74 |
|
| 75 |
if ( isset( $data[ $WPRole . '_tiered_price_type' ] ) ) { |
| 76 |
|
| 77 |
$tieredPricingType = $data[ $WPRole . '_tiered_price_type' ]; |
| 78 |
|
| 79 |
if ( in_array( $tieredPricingType, array( 'fixed', 'percentage' ) ) ) { |
| 80 |
$roleBasedRule->setTieredPricingType( $tieredPricingType ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if ( isset( $data[ $WPRole . '_tiered_price_minimum' ] ) ) { |
| 85 |
|
| 86 |
$minimum = (int) $data[ $WPRole . '_tiered_price_minimum' ]; |
| 87 |
|
| 88 |
$roleBasedRule->setMinimumOrderQuantity( $minimum ); |
| 89 |
$needUpdate = true; |
| 90 |
} |
| 91 |
|
| 92 |
if ( $needUpdate ) { |
| 93 |
try { |
| 94 |
$roleBasedRule->save(); |
| 95 |
} catch ( \Exception $e ) { |
| 96 |
wc_get_logger()->error( 'Error saving role-based pricing rule for product ID ' . $product->get_id() . ' and role ' . $WPRole . ': ' . $e->getMessage(), |
| 97 |
array( 'source' => 'tiered-pricing-table' ) ); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return $product; |
| 103 |
} |
| 104 |
|
| 105 |
public function addColumnsToImporter( $columns ): array { |
| 106 |
|
| 107 |
global $wp_roles; |
| 108 |
$disabled_roles = apply_filters( 'tiered_pricing_table/role_based_rules/import_export_disabled_roles', array( 'editor', 'author', 'contributor', 'shop_manager' ) ); |
| 109 |
|
| 110 |
foreach ( wp_roles()->roles as $WPRole => $role_data ) { |
| 111 |
if ( in_array( $WPRole, $disabled_roles, true ) ) { |
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
$roleName = isset( $wp_roles->role_names[ $WPRole ] ) ? translate_user_role( $wp_roles->role_names[ $WPRole ] ) : $WPRole; |
| 116 |
|
| 117 |
$columns[ 'tpt_' . $WPRole ] = array( |
| 118 |
'name' => __( 'Tiered Pricing', 'tier-pricing-table' ) . ': ' . $roleName, |
| 119 |
'options' => array( |
| 120 |
$WPRole . '_tiered_price_pricing_type' => $roleName . ': ' . __( 'Regular pricing type', |
| 121 |
'tier-pricing-table' ), |
| 122 |
$WPRole . '_tiered_price_regular_price' => $roleName . ': ' . __( 'Regular price', |
| 123 |
'tier-pricing-table' ), |
| 124 |
$WPRole . '_tiered_price_sale_price' => $roleName . ': ' . __( 'Sale price', |
| 125 |
'tier-pricing-table' ), |
| 126 |
$WPRole . '_tiered_price_discount' => $roleName . ': ' . __( 'Percentage discount', |
| 127 |
'tier-pricing-table' ), |
| 128 |
$WPRole . '_tiered_price_type' => $roleName . ': ' . __( 'Tiered pricing type', |
| 129 |
'tier-pricing-table' ), |
| 130 |
$WPRole . '_tiered_price_minimum' => $roleName . ': ' . __( 'Minimum order quantity', |
| 131 |
'tier-pricing-table' ), |
| 132 |
$WPRole . '_tiered_price_fixed' => $roleName . ': ' . __( 'Fixed pricing rules', |
| 133 |
'tier-pricing-table' ), |
| 134 |
$WPRole . '_tiered_price_percentage' => $roleName . ': ' . __( 'Percentage pricing rules', |
| 135 |
'tier-pricing-table' ), |
| 136 |
), |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
return $columns; |
| 141 |
} |
| 142 |
|
| 143 |
public function addColumnsToMapper( array $columns ): array { |
| 144 |
|
| 145 |
global $wp_roles; |
| 146 |
$disabled_roles = apply_filters( 'tiered_pricing_table/role_based_rules/import_export_disabled_roles', array( 'editor', 'author', 'contributor', 'shop_manager' ) ); |
| 147 |
|
| 148 |
foreach ( wp_roles()->roles as $WPRole => $role_data ) { |
| 149 |
if ( in_array( $WPRole, $disabled_roles, true ) ) { |
| 150 |
continue; |
| 151 |
} |
| 152 |
|
| 153 |
$roleName = isset( $wp_roles->role_names[ $WPRole ] ) ? translate_user_role( $wp_roles->role_names[ $WPRole ] ) : $WPRole; |
| 154 |
|
| 155 |
$columns[ 'Tiered Pricing — [' . $roleName . '] Regular pricing type' ] = $WPRole . '_tiered_price_pricing_type'; |
| 156 |
|
| 157 |
$columns[ 'Tiered Pricing — [' . $roleName . '] Regular price' ] = $WPRole . '_tiered_price_regular_price'; |
| 158 |
|
| 159 |
$columns[ 'Tiered Pricing — [' . $roleName . '] Sale price' ] = $WPRole . '_tiered_price_sale_price'; |
| 160 |
|
| 161 |
$columns[ 'Tiered Pricing — [' . $roleName . '] Percentage discount' ] = $WPRole . '_tiered_price_discount'; |
| 162 |
|
| 163 |
$columns[ 'Tiered Pricing — [' . $roleName . '] Fixed pricing rules' ] = $WPRole . '_tiered_price_fixed'; |
| 164 |
|
| 165 |
$columns[ 'Tiered Pricing — [' . $roleName . '] Percentage pricing rules' ] = $WPRole . '_tiered_price_percentage'; |
| 166 |
|
| 167 |
$columns[ 'Tiered Pricing — [' . $roleName . '] Tiered pricing type' ] = $WPRole . '_tiered_price_type'; |
| 168 |
|
| 169 |
$columns[ 'Tiered Pricing — [' . $roleName . '] Minimum order quantity' ] = $WPRole . '_tiered_price_minimum'; |
| 170 |
} |
| 171 |
|
| 172 |
return $columns; |
| 173 |
} |
| 174 |
} |
| 175 |
|