使用 Woocommerce 中的商店下拉菜单自定义取货运输方式

Custom Pickup shipping method with a dropdown of stores in Woocommerce

我定制了 Woocommerce 送货方式(店内提货),通过结帐部分的 select 字段,客户有机会在不同的商店地址之间进行选择:

我希望在付款之前,自定义送货方式的标题是“店内提货”+ selected 商店地址。因此,客户和商店都可以看到 selected 的地址。 这是完整的代码。如您所见,我将 selected 地址放在 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);
?>

当店内取货是选择的送货方式时,以下代码将:

  • 将所选商店添加到显示的送货方式标签。
  • 如果没有选择商店,验证显示错误通知的字段,避免结账。
  • 将所选商店保存为订单元数据和订单“发货项目元数据”。
  • 在管理员订单“发货”项目中显示所选商店。
  • 在客户订单和电子邮件通知中显示所选商店。

该代码替换了您的最后一个函数。

    // Output dropdown Store list
    add_action('woocommerce_after_shipping_rate', 'output_dropdown_stores_list', 10, 2);
    function output_dropdown_stores_list( $shipping_rate, $index )  {
        $chosen_shipping_rate_id = WC()->session->get('chosen_shipping_methods')[0];

        if ( $shipping_rate->id === $chosen_shipping_rate_id ) {
            $option_key = 'woocommerce_' . $shipping_rate->method_id . '_' . $shipping_rate->instance_id . '_settings';
            $settings   = get_option($option_key);

            if ( isset($settings['stores']) ) :
                $stores_list = explode("\n", str_replace("\r", "", $settings['stores']) );
            ?>
            <select id="storelist" name="storelist">
                <option value=""><?php _e("Choose a store location", "pickupinstore"); ?></option>
            <?php foreach( $stores_list as $key => $store ) {
                echo '<option value="'.$store.'">'.$store.'</option>';
            } ?>
            </select>
            <script>
            jQuery(function($){
                var label = '<?php echo $shipping_rate->label; ?>';
                $(document.body).on('change', 'select#storelist', function(){
                    $(this).parent().find('label').text(label+': '+$(this).val());
                });
            });
            </script>
            <?php
            endif;
        }
    }


    // Pickup store Validation
    add_action( 'woocommerce_checkout_process', 'validate_pickup_store' );
    function validate_pickup_store() {
        $chosen_shipping_rate_id = WC()->session->get('chosen_shipping_methods')[0];

        if ( false !== strpos( $chosen_shipping_rate_id, 'pickupinstore' )
        && isset($_POST['storelist']) && empty($_POST['storelist']) ) {
           wc_add_notice( __( 'Please chose your Pickup store.', 'pickupinstore' ), 'error' );
        }
    }


    // Save chosen pickup store as order meta
    add_action( 'woocommerce_checkout_create_order', 'save_pickup_stores_to_order', 10, 2 );
    function save_pickup_stores_to_order( $order, $data ) {
        if ( isset($_POST['storelist']) && ! empty($_POST['storelist']) ) {
            $order->update_meta_data('pickup_store', esc_attr($_POST['storelist']) );
        }
    }


    // Save chosen pickup store as order shipping item meta
    add_action( 'woocommerce_checkout_create_order_shipping_item', 'save_pickup_stores_to_order_item_shipping', 10, 4 );
    function save_pickup_stores_to_order_item_shipping( $item, $package_key, $package, $order ) {
        if ( isset($_POST['storelist']) && ! empty($_POST['storelist']) ) {
            $item->update_meta_data('_pickup_store', esc_attr($_POST['storelist']) );
        }
    }

    // Admin: Change store order shipping item displayed meta key label to something readable
    add_filter('woocommerce_order_item_display_meta_key', 'filter_order_item_displayed_meta_key', 20, 3 );
    function filter_order_item_displayed_meta_key( $displayed_key, $meta, $item ) {
        // Change displayed meta key label for specific order item meta key
        if( $item->get_type() === 'shipping' && $meta->key === '_pickup_store' ) {
            $displayed_key = __("Store", "pickupinstore");
        }
        return $displayed_key;
    }

    // Customer: Display Store below shipping method on orders and email notifications
    add_filter( 'woocommerce_get_order_item_totals', 'display_pickup_store_on_order_item_totals', 10, 3 );
    function display_pickup_store_on_order_item_totals( $total_rows, $order, $tax_display ){
        $chosen_store   = $order->get_meta('pickup_store'); // Get pickup store
        $new_total_rows = array(); // Initializing

        if( empty($chosen_store) )
            return $total_rows; // Exit

        // Loop through total rows
        foreach( $total_rows as $key => $value ){
            if( 'shipping' == $key ) {
                $new_total_rows['pickup_store'] = array(
                    'label' => __("Pickup in store", "pickupinstore") . ':',
                    'value' => $chosen_store,
                );
            } else {
                $new_total_rows[$key] = $value;
            }
        }
        return $new_total_rows;
    }
}