woocommerce产品中的自定义字段复选框价格,然后+购物车价格更新或附加

时间:2019-09-24 07:43:07

标签: php wordpress woocommerce hook-woocommerce

到目前为止,一切都很好。这里一切都很好→ 1.自定义字段在单个产品编辑/管理页面中正确显示,并且已正确保存。 2.他们保存正确。他们正在正确地检索。

add_action('woocommerce_product_options_advanced','add_custom_field_product_dashboard');
function add_custom_field_product_dashboard(){
    global $post;
echo '<div class="product_custom_field">';

     // Checkbox Field
     woocommerce_wp_checkbox( array(
             'id'          => 'custom_services_fields',
             'description' =>  __('Select if you want add on services', 'xxx'),
             'label'       => __('Display custom add on services', 'xxx'),
             'desc_tip'    => 'true',
     ) );


     // Addon Service 1
     woocommerce_wp_text_input( array(
             'id'        => 'addon_service_1',
             'label'     => __('Service 1', 'woocommerce'),
             'description' =>  __('set custom minimum Lettering text field', 'woocommerce'),
             'desc_tip'  => 'true',
     ) );

    // Number Field
    woocommerce_wp_text_input(
        array(
            'id'                => '_number_field_1',
            'label'             => __( 'Service amount 1', 'woocommerce' ),
            'placeholder'       => '',
            'desc_tip'            => false,
            'description'       => __( "Please enter the service amount", 'woocommerce' ),
            'type'              => 'number',
            'desc_tip'  => 'true',
            'custom_attributes' => array(
                    'step'  => 'any',
                    'min'   => '0'
                )
        )
    );

         echo '</div>';
}


add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
 function woocommerce_product_custom_fields_save($post_id){
    // Checkbox Field
    $checkbox = isset( $_POST['xxx_custom_services_fields'] ) ? 'yes' : 'no';
    update_post_meta( $post_id, 'xxx_custom_services_fields', $checkbox );

    // Save Minimum Letters
    if ( isset( $_POST['addon_service_1'] ) )
        update_post_meta($post_id, 'addon_service_1', sanitize_text_field( $_POST['addon_service_1'] ) );

    // Save the services amount
    if ( isset( $_POST['_number_field_1'] ) )
        update_post_meta($post_id, '_number_field_1', sanitize_text_field( $_POST['_number_field_1'] ) );

    if ( isset( $_POST['addon_service_2'] ) )
        update_post_meta($post_id, 'addon_service_2', sanitize_text_field( $_POST['addon_service_2'] ) );

    // Save the services amount
    if ( isset( $_POST['_number_field_2'] ) )
        update_post_meta($post_id, '_number_field_2', sanitize_text_field( $_POST['_number_field_2'] ) );
}


add_action( 'woocommerce_before_add_to_cart_button', 'output_custom_text_field', 0 );
function output_custom_text_field() {
    global $post;

    // Get the checkbox value
    $custom_option = get_post_meta( $post->ID, 'custom_services_fields', true );

    // If is single product page and have the "custom text option" enabled we display the field
    if ( is_product() && ! empty($custom_option) ) {
      ?>
        <div class="serviceclass">
                        <input type="checkbox" id="option1"/>
                        <label for="option1"><?php global $post; echo get_post_meta($post->ID,'addon_service_1',true);?>
                    </label><span class="js_price bold">€<?php global $post; echo get_post_meta($post->ID,'_number_field_1',true);?></span>
                </div>


        <div class="serviceclass">
                        <input type="checkbox" id="option2"/>
                        <label for="option1"><?php global $post; echo get_post_meta($post->ID,'addon_service_1',true);?>
                    </label><span class="js_price bold">€<?php global $post; echo get_post_meta($post->ID,'_number_field_2',true);?></span>
                </div>


        <?php
    }
}

我被困住了。选中复选框时。我希望复选框商品的价格应附加在最终购物车页面中。

像→

hhtps://www.sampledomain.com/cart/ 要使用此功能,但是问题→

woocommerce_before_calculate_totals

0 个答案:

没有答案