| 15 | | add_action('woocommerce_before_add_to_cart_quantity', [__CLASS__, 'bpwp_single__bonusplus_price']); |
| 16 | | add_action('woocommerce_before_cart_totals', [__CLASS__, 'bpwp_single__bonusplus_price']); |
| 17 | | add_action('woocommerce_checkout_before_order_review', [__CLASS__, 'bpwp_single__bonusplus_price']); |
| 18 | | add_action('woocommerce_after_shop_loop_item', [__CLASS__, 'bpwp_single__bonusplus_price']); |
| | 16 | self::$maxDebitBonuses = 0; |
| | 17 | |
| | 18 | add_action('woocommerce_before_cart_totals', [__CLASS__, 'bpwp_cart_checkout_bonusplus_price']); |
| | 19 | add_action('woocommerce_checkout_before_order_review', [__CLASS__, 'bpwp_cart_checkout_bonusplus_price']); |
| | 20 | add_action('woocommerce_short_description', [__CLASS__, 'bpwp_single_product_bonusplus_price'], 10, 1); |
| | 21 | add_action('woocommerce_cart_calculate_fees', [__CLASS__, 'add_custom_fee_on_checkout']); |
| | 22 | |
| | 23 | } |
| | 24 | |
| | 25 | public static function add_custom_fee_on_checkout() |
| | 26 | { |
| | 27 | // TODO: Если юзер выбрал Да( списать), то уменьшаем сумму заказа |
| | 28 | |
| | 29 | // Получить maxDebitBonus |
| | 30 | $data = self::bpwp_get_calc_bonusplus_price(); |
| | 31 | |
| | 32 | if (is_array($data) && isset($data['request'])) { |
| | 33 | $fee_amount = -(int)$data['request']['maxDebitBonuses']; |
| | 34 | $fee_name = 'Списание бонусов'; |
| | 35 | $taxable = true; |
| | 36 | $tax_class = 'bpwp-bonuses-reserved'; |
| | 37 | |
| | 38 | WC()->cart->add_fee($fee_name, $fee_amount, $taxable, $tax_class); |
| | 39 | |
| | 40 | } |
| 142 | | $output = '<ul>'; |
| 143 | | |
| | 171 | |
| | 172 | $output = ''; |
| | 173 | |
| | 174 | if (isset($_GET['testrequest'])) { |
| | 175 | |
| | 176 | $output .= '<ul>'; |
| | 177 | |
| | 178 | if (is_array($data) && isset($data['request']) && isset($data['request']['discount'])) { |
| | 179 | |
| | 180 | foreach ($data['request']['discount'] as $discount) { |
| | 181 | $output .= '<li>'; |
| | 182 | $output .= '<strong>ext:</strong> ' . $discount['ext'] . '<br>'; |
| | 183 | |
| | 184 | if (is_array($discount['messages']) && !empty($discount['messages'])) { |
| | 185 | $output .= '<strong>messages:</strong> <ul>'; |
| | 186 | foreach ($discount['messages'] as $message) { |
| | 187 | $output .= '<li>' . $message . '</li>'; |
| | 188 | } |
| | 189 | $output .= '</ul>'; |
| | 190 | } |
| | 191 | |
| | 192 | $output .= '<strong>cb:</strong> ' . $discount['cb'] . ' - Сумма бонусов, которые будут начислены на данную позицию<br>'; |
| | 193 | $output .= '<strong>db:</strong> ' . $discount['db'] . ' - Сумма бонусов, которые будут списаны для данной позиции<br>'; |
| | 194 | $output .= '<strong>ds:</strong> ' . $discount['ds'] . ' - Общая сумма скидки для позиции (без учета бонусов)<br>'; |
| | 195 | $output .= '<strong>dp:</strong> ' . $discount['dp'] . ' - Процент скидки для позиции (без учета бонусов)<br>'; |
| | 196 | $output .= '<strong>ids:</strong> ' . $discount['ids'] . ' - Сумма скидки, примененная на стороне БонусПлюс для данной позиции (внутренняя скидка)<br>'; |
| | 197 | $output .= '<strong>idp:</strong> ' . $discount['idp'] . ' - Процент скидки, примененный на стороне БонусПлюс для данной позиции (внутренняя скидка)<br>'; |
| | 198 | $output .= '<strong>dbp:</strong> ' . $discount['dbp'] . ' - Процент списания бонусов<br>'; |
| | 199 | $output .= '<strong>cbp:</strong> ' . $discount['cbp'] . ' - Процент начисления бонусов<br>'; |
| | 200 | $output .= '</li>'; |
| | 201 | } |
| | 202 | } else { |
| | 203 | $output .= '<li>Invalid data format</li>'; |
| | 204 | } |
| | 205 | |
| | 206 | $output .= '</ul>'; |
| | 207 | $maxDebitBonuses = $data['request']['maxDebitBonuses']; |
| | 208 | $multiplicityDebitBonus = $data['request']['multiplicityDebitBonus']; |
| | 209 | $output .= '<strong>maxDebitBonuses:</strong> '; |
| | 210 | $output .= $maxDebitBonuses . '<br>'; |
| | 211 | $output .= '<strong>multiplicityDebitBonus:</strong> '; |
| | 212 | $output .= $multiplicityDebitBonus . '<br>'; |
| | 213 | |
| | 214 | } |
| | 215 | |
| | 216 | |
| | 217 | $info = bpwp_api_get_customer_data(); |
| | 218 | if ($info && is_array($info)) { |
| | 219 | |
| | 220 | $available_bonuses = $info['availableBonuses']; |
| | 221 | |
| | 222 | if (is_array($data) && isset($data['request']) && isset($data['request']['discount'])) { |
| | 223 | |
| | 224 | $bonuses = 0; |
| | 225 | |
| | 226 | foreach ($data['request']['discount'] as $discount) { |
| | 227 | if (isset($discount['cb']) && !empty($discount['cb'])){ |
| | 228 | $bonuses = $bonuses + $discount['cb']; |
| | 229 | } |
| | 230 | } |
| | 231 | |
| | 232 | $maxDebitBonuses = $data['request']['maxDebitBonuses']; |
| | 233 | |
| | 234 | } |
| | 235 | |
| | 236 | $output .= '<div class="bonus-plus-price">'; |
| | 237 | $output .= '<p>Ваш бонусный баланс '. $available_bonuses .'.</p><p>Сумма бонусов, которые будут начислены '. $bonuses .'.</p><p> На эту покупку будет списано '. $maxDebitBonuses .' бон.</p>'; |
| | 238 | $output .= '</div>'; |
| | 239 | |
| | 240 | } |
| | 241 | |
| | 242 | echo $output; |
| | 243 | } |
| | 244 | |
| | 245 | public static function bpwp_render_retailitems_calc($data) |
| | 246 | { |
| 146 | | $output .= '<li>'; |
| 147 | | $output .= '<strong>ext:</strong> ' . $discount['ext'] . '<br>'; |
| 148 | | |
| 149 | | if (is_array($discount['messages']) && !empty($discount['messages'])) { |
| 150 | | $output .= '<strong>messages:</strong> <ul>'; |
| 151 | | foreach ($discount['messages'] as $message) { |
| 152 | | $output .= '<li>' . $message . '</li>'; |
| 153 | | } |
| 154 | | $output .= '</ul>'; |
| 155 | | } |
| 156 | | |
| 157 | | $output .= '<strong>cb:</strong> ' . $discount['cb'] . ' - Сумма бонусов, которые будут начислены на данную позицию<br>'; |
| 158 | | $output .= '<strong>db:</strong> ' . $discount['db'] . ' - Сумма бонусов, которые будут списаны для данной позиции<br>'; |
| 159 | | $output .= '<strong>ds:</strong> ' . $discount['ds'] . ' - Общая сумма скидки для позиции (без учета бонусов)<br>'; |
| 160 | | $output .= '<strong>dp:</strong> ' . $discount['dp'] . ' - Процент скидки для позиции (без учета бонусов)<br>'; |
| 161 | | $output .= '<strong>ids:</strong> ' . $discount['ids'] . ' - Сумма скидки, примененная на стороне БонусПлюс для данной позиции (внутренняя скидка)<br>'; |
| 162 | | $output .= '<strong>idp:</strong> ' . $discount['idp'] . ' - Процент скидки, примененный на стороне БонусПлюс для данной позиции (внутренняя скидка)<br>'; |
| 163 | | $output .= '<strong>dbp:</strong> ' . $discount['dbp'] . ' - Процент списания бонусов<br>'; |
| 164 | | $output .= '<strong>cbp:</strong> ' . $discount['cbp'] . ' - Процент начисления бонусов<br>'; |
| 165 | | $output .= '</li>'; |
| 166 | | } |
| 167 | | } else { |
| 168 | | $output .= '<li>Invalid data format</li>'; |
| 169 | | } |
| 170 | | |
| 171 | | $output .= '</ul>'; |
| 172 | | $output .= '<strong>maxDebitBonuses:</strong> ' . $data['request']['maxDebitBonuses'] . '<br>'; |
| 173 | | $output .= '<strong>multiplicityDebitBonus:</strong> ' . $data['request']['multiplicityDebitBonus'] . '<br>'; |
| 174 | | |
| 175 | | return $output; |
| 176 | | } |
| 177 | | |
| 178 | | public static function bpwp_render_retailitems_calc($data) |
| 179 | | { |
| 180 | | if (is_array($data) && isset($data['request']) && is_array($data['request']['discount'])) { |
| 181 | | foreach ($data['request']['discount'] as $discount) { |
| | 251 | $output .= '<ul>'; |
| | 274 | |
| | 275 | /** |
| | 276 | * |
| | 277 | */ |
| | 278 | public static function bpwp_single_product_bonusplus_price($post_excerpt){ |
| | 279 | $content = ''; |
| | 280 | |
| | 281 | if (is_product()) { |
| | 282 | $price_data = self::bpwp_get_calc_bonusplus_price(); |
| | 283 | $content = $post_excerpt . self::bpwp_render_retailitems_calc($price_data); |
| | 284 | } |
| | 285 | |
| | 286 | return $content; |
| | 287 | } |
| | 288 | |
| | 289 | /** |
| | 290 | * Выводим бонусы, доступные для начисления и списания |
| | 291 | */ |
| | 292 | public static function bpwp_cart_checkout_bonusplus_price(){ |
| | 293 | $content = ''; |
| | 294 | |
| | 295 | if (is_cart() || is_checkout() ) { |
| | 296 | $price_data = self::bpwp_get_calc_bonusplus_price(); |
| | 297 | //$content = self::bpwp_render_retailitems_calc($price_data); |
| | 298 | $content = self::bpwp_render_calc_bonusplus_price($price_data); |
| | 299 | } |
| | 300 | return $content; |
| | 301 | } |
| | 302 | |
| | 303 | public function bpwp_get_max_debit_bonuses($data) { |
| | 304 | if (is_array($data) && isset($data['request']) && isset($data['request']['discount'])) { |
| | 305 | $this->maxDebitBonuses = $data['request']['maxDebitBonuses']; |
| | 306 | } |
| | 307 | } |
| | 308 | |