getContainer()->getFileManager()->locateAsset( 'admin/integrations/wpallimport-icon.png' ); } public function run() { $this->name = 'Tiered Pricing Table'; $this->slug = 'tier-pricing-table'; $this->add_option( 'update_tiered_pricing', '1' ); $this->process(); } public function addFields() { foreach ( $this->getFieldsToImport() as $slug => $field ) { if ( empty( $field ) ) { continue; } $this->add_field( $slug, $field['title'], $field['type'], null, $field['description'], true, '', isset( $field['role'] ) ? $field['role'] : '' ); } } public function processImport( $post_id, $data, $import_options, $article ) { $this->log( '------------- Tiered Pricing Table START -------------' ); foreach ( $this->getFieldsToImport() as $fieldSlug => $fieldData ) { if ( empty( $article['ID'] ) || $this->can_update_meta( $fieldSlug, $import_options ) ) { // System key if ( substr( $fieldSlug, 0, 2 ) === '__' ) { continue; } $roleKey = $this->getRoleKey( $fieldSlug ); // role is not enabled if ( $roleKey && empty( $data[ '__' . $roleKey . '_enabled' ] ) ) { continue; } $fieldValue = $fieldData['unSerialize']( $data[ $fieldSlug ] ); $this->log( sprintf( 'Field %s updated with value %s', $fieldSlug, json_encode( $fieldValue ) ) ); update_post_meta( $post_id, $fieldSlug, $fieldValue ); } } $this->log( '------------- Tiered Pricing Table END -------------' ); } public function getRoleKey( $key ) { foreach ( wp_roles()->roles as $role => $roleData ) { if ( strpos( $key, $role ) !== false ) { return $role; } } return ''; } public function getFieldsToImport() { $fields = array( '_fixed_price_rules' => array( 'title' => __( 'Fixed Tiered prices', 'tier-pricing-table' ), 'type' => 'text', 'unSerialize' => array( $this, 'unSerializeRules' ), 'description' => __( 'The format for the rules must be the following: quantity:price,quantity:price. For example: "10:5,20:15" where 10 and 20 are quantities and 5 and 15 are prices for those quantities.', 'tier-pricing-table' ), ), '_percentage_price_rules' => array( 'title' => __( 'Percentage Tiered prices', 'tier-pricing-table' ), 'type' => 'text', 'unSerialize' => array( $this, 'unSerializeRules' ), 'description' => __( 'Format for the rules must be the following: quantity:percent_discount,quantity:percent_discount. For example: "10:5,20:15" where 10 and 20 are quantities and 5 and 15 are amounts of discount.', 'tier-pricing-table' ), ), '_tiered_price_rules_type' => array( 'title' => __( 'Tiered pricing type', 'tier-pricing-table' ), 'type' => 'text', 'unSerialize' => function ( $data ) { if ( in_array( $data, array( 'fixed', 'percentage' ) ) ) { return $data; } return 'fixed'; }, 'description' => __( 'The field must be either "fixed" or "percentage".', 'tier-pricing-table' ), ), '_tiered_price_minimum_qty' => array( 'title' => __( 'Minimum product quantity', 'tier-pricing-table' ), 'type' => 'text', 'unSerialize' => function ( $min ) { return (int) $min; }, 'description' => __( 'The field must contain an integer value.', 'tier-pricing-table' ), ), ); foreach ( wp_roles()->roles as $role => $roleData ) { $fields[ '__' . $role . '_enabled' ] = array( 'title' => '', 'description' => '', 'type' => 'is_enabled_role_field', 'role' => $role, 'is_sub_field' => true, ); $fields[ '_' . $role . '_tiered_price_pricing_type' ] = array( 'title' => __( 'Pricing type', 'tier-pricing-table' ), 'type' => 'role_field', 'role' => $role, 'is_sub_field' => true, 'unSerialize' => function ( $type ) { return 'fixed' === $type ? 'flat' : 'percentage'; }, 'description' => __( 'The field must be either "fixed" or "percentage". This affect what fields will be used to grant a discount: sale and regular prices or percentage discount.', 'tier-pricing-table' ), ); $fields[ '_' . $role . '_tiered_price_regular_price' ] = array( 'title' => __( 'Regular price', 'tier-pricing-table' ), 'type' => 'role_field', 'role' => $role, 'is_sub_field' => true, 'unSerialize' => array( $this, 'unSerializePrice' ), 'description' => __( 'Regular price for the role', 'tier-pricing-table' ), ); $fields[ '_' . $role . '_tiered_price_sale_price' ] = array( 'title' => __( 'Sale price', 'tier-pricing-table' ), 'type' => 'role_field', 'role' => $role, 'is_sub_field' => true, 'unSerialize' => array( $this, 'unSerializePrice' ), 'description' => __( 'Sale price for the role', 'tier-pricing-table' ), ); $fields[ '_' . $role . '_tiered_price_discount' ] = array( 'title' => __( 'Discount (%)', 'tier-pricing-table' ), 'type' => 'role_field', 'role' => $role, 'is_sub_field' => true, 'unSerialize' => function ( $discount ) { if ( '' === $discount ) { return null; } $discount = $discount ? (float) $discount : null; if ( ! $discount ) { return null; } $discount = max( 0, $discount ); return min( 100, $discount ); }, 'description' => __( 'Percentage discount for the role.', 'tier-pricing-table' ), ); $fields[ '_' . $role . '_fixed_price_rules' ] = array( 'title' => __( 'Fixed Tiered prices', 'tier-pricing-table' ), 'type' => 'role_field', 'role' => $role, 'is_sub_field' => true, 'unSerialize' => array( $this, 'unSerializeRules' ), 'description' => __( 'The format for the rules must be the following: quantity:price,quantity:price. For example: "10:5,20:15" where 10 and 20 are quantities and 5 and 15 are prices for those quantities.', 'tier-pricing-table' ), ); $fields[ '_' . $role . '_percentage_price_rules' ] = array( 'title' => __( 'Percentage Tiered prices', 'tier-pricing-table' ), 'type' => 'role_field', 'is_sub_field' => true, 'role' => $role, 'unSerialize' => array( $this, 'unSerializeRules' ), 'description' => __( 'Format for the rules must be the following: quantity:percent_discount,quantity:percent_discount. For example: "10:5,20:15" where 10 and 20 are quantities and 5 and 15 are amounts of discount.', 'tier-pricing-table' ), ); $fields[ '_' . $role . '_tiered_price_rules_type' ] = array( 'title' => __( 'Tiered pricing type', 'tier-pricing-table' ), 'type' => 'role_field', 'is_sub_field' => true, 'role' => $role, 'unSerialize' => function ( $data ) { if ( in_array( $data, array( 'fixed', 'percentage' ) ) ) { return $data; } return 'fixed'; }, 'description' => __( 'The field must be either "fixed" or "percentage".', 'tier-pricing-table' ), ); $fields[ '_' . $role . '_tiered_price_minimum_qty' ] = array( 'title' => __( 'Minimum product quantity', 'tier-pricing-table' ), 'type' => 'role_field', 'is_sub_field' => true, 'role' => $role, 'unSerialize' => function ( $min ) { return (int) $min; }, 'description' => __( 'The field must contain an integer value.', 'tier-pricing-table' ), ); } return $fields; } public function unSerializePrice( $price ) { if ( '' === $price ) { return null; } return $price ? (float) $price : null; } /** * UnSerialize rules * * @param mixed $data * * @return mixed */ public function unSerializeRules( $data ) { $rules = explode( TierPricingTablePlugin::getRulesSeparator(), $data ); $data = array(); if ( $rules ) { foreach ( $rules as $rule ) { $rule = explode( ':', $rule ); if ( isset( $rule[0] ) && isset( $rule[1] ) ) { $data[ intval( $rule[0] ) ] = $rule[1]; } } } $data = array_filter( $data ); $data = array_filter( $data, function ( $itemKey ) { return is_numeric( $itemKey ) && $itemKey > 1; }, ARRAY_FILTER_USE_KEY ); return ! empty( $data ) ? $data : array(); } public function is_active_addon( $post_type = null ) { if ( ! is_plugin_active( 'wp-all-import-pro/wp-all-import-pro.php' ) && ! is_plugin_active( 'wp-all-import/plugin.php' ) ) { return false; } if ( null !== $post_type ) { if ( @in_array( $post_type, $this->active_post_types ) || empty( $this->active_post_types ) ) { return true; } return false; } return true; } public function process() { $this->active_post_types = array( 'product', 'variation' ); add_action( 'init', array( $this, 'addFields' ), 10 ); add_filter( 'pmxi_addons', array( $this, 'wpai_api_register' ) ); add_filter( 'wp_all_import_addon_parse', array( $this, 'wpai_api_parse' ) ); add_filter( 'wp_all_import_addon_import', array( $this, 'wpai_api_import' ) ); add_filter( 'pmxi_options_options', array( $this, 'wpai_api_options' ) ); add_action( 'pmxi_extend_options_featured', array( $this, 'wpai_api_metabox' ), 10, 2 ); add_action( 'admin_init', array( $this, 'admin_notice_ignore' ) ); add_action( 'pmxi_reimport', function ( $post_type, $post ) { if ( 'product' !== $post_type && empty( $post['is_override_post_type'] ) ) { return false; } ?>