Я создал собственный метод доставки Woocommerce (самовывоз в магазине), и через поле выбора в разделе оформления заказа у покупателя есть возможность выбирать между разными адресами магазина:
Я хочу, чтобы прямо перед платежом название индивидуального способа доставки было Самовывозом в магазине + выбранный адрес магазина. Таким образом, и покупатель, и магазин могли видеть, какой адрес был выбран. Вот полный код. Как видите, я поместил выбранный адрес в переменную javascript, но мне нужно добавить содержимое этой переменной в заголовок. Любые идеи?
<?php
/**
* Plugin Name: Pickup in Store
* Plugin URI: www.woocommerce.com
* Description: Pickup in store - Custom Shipping Method
* Version: 1.0.0
* Author: Woocommerce
* Author URI: www.woocommerce.com
* License: GPL-3.0+
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
* Domain Path: /lang
* Text Domain: woocommerce
*/
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
function pickupinstore_shipping_method() {
if ( ! class_exists( 'PickupInStore_Shipping_Method' ) ) {
$titolo = '';
class PickupInStore_Shipping_Method extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct( $instance_id = 0 ) {
$this->id = 'pickupinstore';
$this->instance_id = absint( $instance_id );
$this->method_title = __( 'Pickup in Store', 'pickupinstore' );
$this->method_description = __( 'Custom Shipping Method - Pickup in Store', 'pickupinstore' );
$this->supports = array(
'shipping-zones',
'instance-settings',
'instance-settings-modal',
);
$this->init();
}
/**
* Init your settings
*
* @access public
* @return void
*/
public function init() {
// Load the settings API
$this->init_form_fields();
$this->init_settings();
$this->title = null != $this->get_option('title') ? $this->get_option('title') : __( 'Pickup in Store', 'pickupinstore' );
// Save settings in admin if you have any defined
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
/**
* Define settings field for this shipping
* @return void
*/
public function init_form_fields() {
$this->instance_form_fields = array(
'title' => array(
'title' => __('Titolo', 'tutsplus'),
'type' => 'text',
'description' => __('Titolo metodo di spedizione visualizzato a frontend', 'prsv_pickup'),
'default' => __('Pickup in store', 'prsv_pickup')
),
'stores' => array(
'title' => __('Elenco store', 'prsv_pickup'),
'type' => 'textarea',
'description' => __('Lista store disponibili', 'prsv_pickup')
),
);
}
/**
* This function is used to calculate the shipping cost. Within this function we can check for weights, dimensions and other parameters.
*
* @access public
* @param mixed $package
* @return void
*/
public function calculate_shipping( $package = array() ) {
$cost = 0;
$this->add_rate( array(
'id' => $this->id,
'label' => $this->title,
'cost' => $cost
) );
}
}
}
}
add_action( 'woocommerce_shipping_init', 'pickupinstore_shipping_method' );
function add_pickupinstore_shipping_method( $methods ) {
$methods['pickupinstore'] = 'PickupInStore_Shipping_Method';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_pickupinstore_shipping_method' );
}
function shipping_instance_custom_desc($shipping_rate, $index)
{
$current_instance_ids = WC()->session->get('chosen_shipping_methods');
$current_instance_id = $current_instance_ids[0];
if ($shipping_rate->id == $current_instance_id) {
$option_key = 'woocommerce_' . $shipping_rate->method_id . '_' . $shipping_rate->instance_id . '_settings';
$instance_settings = get_option($option_key);
if (isset($instance_settings['stores'])) {
$stores_array = explode("\n", str_replace("\r", "", $instance_settings['stores'] ));
?>
<div class="shipping-method-desc">
<?php echo'<script type="text/javascript">
function funzionelista(sel) {
selected_sede = sel.options[sel.selectedIndex].text;
}
</script>'; ?>
<select id="listasede" onChange="funzionelista(this);">
<option value="">Scegli la sede</option>
<?php
foreach($stores_array as $key => $value):
echo '<option value="'.$key.'">'.$value.'</option>';
endforeach;
?>
</select>
</div>
<?php
$GLOBALS['titolo'] = $instance_settings['title'].$_POST["listasede"];
}
}
}
add_action('woocommerce_after_shipping_rate', 'shipping_instance_custom_desc', 10, 2);
?>