Woocommerce - 如何以编程方式将设置字段添加到现有或新创建的送货方式

时间:2018-02-14 13:26:14

标签: wordpress woocommerce

希望有人可以帮助我解决这个问题。我需要为在WooCommerce中创建的所有送货方式添加设置字段。

默认情况下,选项为:

  • 方法标题
  • 纳税身份
  • 费用

如何在其中添加其他字段,以便选项为:

  • 方法标题
  • 纳税身份
  • 费用

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

使用这些代码片段,您可以设置新的自定义送货方式,并附加设置字段" Code"

.entry-header{ 
    overflow: hidden;
}
.entry-title{
    position: relative; /*I had to make it relative to wrap it inside the div*/
    white-space: nowrap;
    animation: floatText 5s infinite alternate ease-in-out;
}

@-webkit-keyframes floatText{
  from {
    right: 00%;
  }

  to {
    /* right: auto; */
    right: 100%;
  }
}

然后,您可以在woocommerce设置上找到新的自定义方法和其他设置代码>运送标签

this

之后你可以使用这些代码片段在cart-shipping.php模板上获得这些额外的文件

function imp_shipping_method_init() {
    class Imp_WC_Pickup_Shipping_Method extends WC_Shipping_Method {
        /**
         * Constructor for your shipping class
         *
         * @access public
         * @return void
         */
        public function __construct() {
            $this->id                 = 'imp_pickup_shipping_method'; // Id for your shipping method. Should be uunique.
            $this->method_title       = "Custom shipping method";  // Title shown in admin

            $this->enabled            = "yes"; // This can be added as an setting but for this example its forced enabled
            $this->title              = "Custom shipping method"; // This can be added as an setting but for this example its forced.

            $this->enabled = $this->get_option( 'enabled' );
            $this->title = $this->get_option( 'title' );

            add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );

            $this->init();
        }

        /**
         * Init your settings
         *
         * @access public
         * @return void
         */
        function init() {
            // Load the settings API
            $this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings
            $this->init_settings(); // This is part of the settings API. Loads settings you previously init.

            // Save settings in admin if you have any defined
            add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
        }

        public function init_form_fields() {
            $this->form_fields = array(
                'enabled' => array(
                    'title'     => __( 'Enable/Disable', 'woocommerce' ),
                    'type'       => 'checkbox',
                    'default'     => 'yes'
                ),

                'title' => array(
                    'title'         => __( 'Method Title', 'woocommerce' ),
                    'type'          => 'text',
                    'description'   => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
                    'default'       => __( 'Custom Shipping Method', 'woocommerce' ),
                    'desc_tip'      => true,
                ),

                'code' => array(
                    'title'         => __( 'Code', 'woocommerce' ),
                    'type'          => 'text',
                    'description'   => __( 'Code', 'woocommerce' ),
                    'default'       => __( 'Code', 'woocommerce' ),
                    'desc_tip'      => true,
                ), 
            );
        }

        /**
         * calculate_shipping function.
         *
         * @access public
         * @param mixed $package
         * @return void
         */
        public function calculate_shipping( $package=array() ) {
            $rate = array(
                'id' => $this->id,
                'label' => $this->title,
                // 'cost' => '100',
                // 'calc_tax' => 'per_item'
            );

            // Register the rate
            $this->add_rate( $rate );
        }
    }
}
add_action( 'woocommerce_shipping_init', 'imp_shipping_method_init' );

function add_your_shipping_method( $methods ) {
    $methods[] = 'Imp_WC_Pickup_Shipping_Method';
    return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_your_shipping_method' );

同样,您可以尝试取消注册默认运输woocomerce方法并注册具有相同功能和新增设置的新海关" Code"

希望它会对你有所帮助。