Добавление цены доставки
-
Доброго дня!
добавляю на сайт виджет почты россии.
Сам виджет добавил, в нем выбирается точка доставки.
Не могу добавить цену и адрес доставки к заказу.Вот код добавления метода доставки
add_action( 'woocommerce_shipping_init', 'add_pochta_shipping_method' ); function add_pochta_shipping_method() { if ( ! class_exists( 'Pochta_Shipping_Method' ) ) { class Pochta_Shipping_Method extends WC_Shipping_Method { /** * Конструктор класса */ public function __construct() { $this->id = 'pochta_shipping_method'; $this->title = 'Почта России'; $this->method_description = 'Доставка почтой России'; $this->enabled = 'yes'; $this->init(); } /** * Инициализируем настройки */ function init() { // API настроек // $this->init_form_fields(); // Мы можем перезаписать этот метод, чтобы добавить свои настройки // $this->init_settings(); // This is part of the settings API. Loads settings you previously init. // Сохраняем настройки add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); } /** * Расчёт стоимости доставки */ public function calculate_shipping( $package ) { $rate = array( 'id' => $this->id, 'label' => $this->title, 'cost' => '0' ); $this->add_rate( $rate ); } } } add_filter( 'woocommerce_shipping_methods', 'add_pochta_shipping_class' ); function add_pochta_shipping_class( $methods ) { $methods[ 'pochta_shipping_method' ] = 'Pochta_Shipping_Method'; return $methods; } }
Это кусок кода со страницы checkout, здесь добавляю сам виджет на страницу
<? $method = WC()->session->get('chosen_shipping_methods'); if ($method[0] === "pochta_shipping_method") { $customer = WC()->session->get('customer'); $cart = WC()->session->get('cart_totals'); ?> <tr class="pochta-map"> <td colspan="2"> <div id="ecom-widget-tapmoda" style="height: 500px"> <script> ecomStartWidget({ id: 207, callbackFunction: pochtaDeliveryData, containerId: 'ecom-widget-tapmoda', sumoc: <?= $cart['cart_contents_total']; ?>, start_location: '<?= $customer['shipping_state']; ?>, <?= $customer['city']; ?>' }); </script> </div> </td> </tr> <? }
Тут передаю по ajax данные от виджета
function pochtaDeliveryData(data) { let dataDelivery = new FormData(); dataDelivery.append('action', 'pochta_detail'); dataDelivery.append('addressTo', data['addressTo']); dataDelivery.append('deliveryTime', data['deliveryDescription']['description']); dataDelivery.append('deliveryCash', data['cashOfDelivery']); fetch('/wp-admin/admin-ajax.php', { method: 'POST', body: dataDelivery }).then(response => { if (response.status !== 200) { return Promise.reject(); } return response.text(); }).then(answer => { console.log(answer); }).catch(() => console.log('error')); }
В этой функции нужно добавить цену и адрес, но как это сделать, разобраться не могу.
add_action('wp_ajax_pochta_detail', 'pochta_detail'); add_action('wp_ajax_nopriv_pochta_detail', 'pochta_detail'); function pochta_detail() { WC()->session->set( 'shipping_calculated_cost', $_POST['deliveryCash'] ); wp_die(); }
Буду признателен за любые подсказки, спасибо!
Просмотр 4 ответов — с 1 по 4 (всего 4)
Просмотр 4 ответов — с 1 по 4 (всего 4)
- Тема «Добавление цены доставки» закрыта для новых ответов.