【问题标题】:Add custom fields to WooComerce product setting pages in the shipping tab and apply automatic calculations based on the values将自定义字段添加到运输选项卡中的 WooCommerce 产品设置页面,并根据值应用自动计算
【发布时间】:2021-10-19 15:34:43
【问题描述】:

基于对以下问题的回答:

我设法在 WooCommerce 运输选项中创建了两个额外的字段:

  • 添加装载米的字段
  • 添加运输重量的字段

这些字段将通过管理员用户编辑字段获得值,或者通过帮助我们用值预填充字段的计算来获得值。

下面的 sn-p 显示了通过将 sn-ps 添加到 functions.php 文件中来创建字段。

add_action('woocommerce_product_options_shipping', function() {
  
    woocommerce_wp_text_input( 
        array( 
            'id'            => '_loading_meters', 
            'label'         => __('Loading meters', 'woocommerce'), 
            'desc_tip'      => 'true',
            'description'   => __( 'Vul hier de laadmeters in', 'woocommerce' ),
            'type'          => 'number',
            'custom_attributes' => array(
                            'step'  => 'any',
                            'min'   => '0'
                        ) 
        )
    );      

    woocommerce_wp_text_input( 
        array( 
            'id'            => '_loading_weight', 
            'label'         => __('Loading weight', 'woocommerce'), 
            'desc_tip'      => 'true',
            'description'   => __( 'Vul hier het laadgewicht in', 'woocommerce' ),
            'type'          => 'number',
            'custom_attributes' => array(
                            'step'  => 'any',
                            'min'   => '0'
                        ) 
        )
    );
    
});

第二个 sn-p 保存管理员用户手动添加的值。这个 sn-p 也被添加到functions.php 文件中。

add_action('woocommerce_process_product_meta', function($post_id) {
    $product = wc_get_product($post_id);
    $num_loading_meters = isset($_POST['_loading_meters']) ? $_POST['_loading_meters'] : '';
    $num_loading_weight = isset($_POST['_loading_weight']) ? $_POST['_loading_weight'] : '';
    $product->update_meta_data('_loading_meters', sanitize_text_field($num_loading_meters));
    $product->update_meta_data('_loading_weight', sanitize_text_field($num_loading_weight)); $product->save();
});

作为最终结果,我希望实现使用计算自动填写这两个字段,但仍然可以由管理员用户编辑。

装载米的计算:

  • (长(米))x(高(米))/2.4

装载重量的计算:

  • 装载米* 1750

有没有人可以为我指明正确的方向?

【问题讨论】:

  • @7uc1f3r 感谢您的修订和有用的反馈。我今天将对此进行处理,并就您的提案提供一些额外的反馈。
  • @7uc1f3r 它按预期完成工作 - 感谢您分享您的经验。

标签: wordpress woocommerce backend product shipping


【解决方案1】:

对于计算,您可以使用$product->get_length()$product->get_height()

计算可以自动保存,但请注意,如果您要手动调整值,则必须在某处指出这一点。否则保存时自动计算会被手动输入的值覆盖,反之亦然。

为了防止这种情况,我添加了一个额外的字段,即复选框。勾选时会保存手动输入的值,不勾选则自动计算并保存这些值

所以你得到:

// Add custom fields to product shipping tab
function action_woocommerce_product_options_shipping() {
    // Checkbox
    woocommerce_wp_checkbox( 
        array( 
        'id'             => '_loading_checkbox',
        'label'          => __( 'My checkbox', 'woocommerce' ),
        'desc_tip'       => false,
        'description'    => __( 'If this is checked, the values ​​entered manually will be saved against the values ​​of the automatic calculation', 'woocommerce' )
        )
    );
    
    // Field loading meters
    woocommerce_wp_text_input(
        array( 
            'id'                => '_loading_meters', 
            'label'             => __( 'Loading meters', 'woocommerce' ), 
            'desc_tip'          => true,
            'description'       => __( 'Vul hier de laadmeters in', 'woocommerce' ),
            'type'              => 'number',
            'custom_attributes' => array(
                'step'  => 'any',
                'min'   => '0'
            )
        )
    );
    
    // Field loading weight
    woocommerce_wp_text_input( 
        array( 
            'id'                => '_loading_weight', 
            'label'             => __('Loading weight', 'woocommerce'), 
            'desc_tip'          => true,
            'description'       => __( 'Vul hier het laadgewicht in', 'woocommerce' ),
            'type'              => 'number',
            'custom_attributes' => array(
                'step'  => 'any',
                'min'   => '0'
            )
        )
    );
}
add_action( 'woocommerce_product_options_shipping', 'action_woocommerce_product_options_shipping', 10, 0 );

// Save
function action_woocommerce_admin_process_product_object( $product ) {
    // Checkbox has been checked
    if ( isset( $_POST['_loading_checkbox'] ) ) {
        // Isset
        if ( isset( $_POST['_loading_meters'] ) ) {
            $product->update_meta_data( '_loading_meters', sanitize_text_field( $_POST['_loading_meters'] ) );
        }
        
        // Isset
        if ( isset( $_POST['_loading_weight'] ) ) {
            $product->update_meta_data( '_loading_weight', sanitize_text_field( $_POST['_loading_weight'] ) );
        }
    } else {
        // Initialize
        $loading_meters = '';
        
        // Isset
        if ( isset( $_POST['_loading_meters'] ) ) {
            // Get values
            $height = $product->get_length();
            $length = $product->get_height();

            // NOT empty
            if ( ! empty( $height ) && ! empty( $length ) ) {
                // Calculation
                $loading_meters = ( $length * $height ) / 2.4;
                
                // Update meta
                $product->update_meta_data( '_loading_meters', $loading_meters );
            }
        }

        // Isset and NOT empty
        if ( isset( $_POST['_loading_weight'] ) && ! empty( $loading_meters ) ) {
            // Calculation
            $loading_weight = $loading_meters * 1750;
            
            // Update meta
            $product->update_meta_data( '_loading_weight', $loading_weight );
        }
    }
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );

结果:

【讨论】:

    猜你喜欢
    • 2017-12-26
    • 2021-02-03
    • 2020-12-21
    • 2019-01-30
    • 2014-07-12
    • 1970-01-01
    • 2021-05-02
    • 2018-09-22
    • 2016-06-01
    相关资源
    最近更新 更多