更改Woocommerce购物车项目价格的产品自定义复选框选项

时间:2019-10-09 21:41:04

标签: php wordpress woocommerce product hook-woocommerce

以下代码在单个产品页面上的“添加到购物车”按钮之前显示一个自定义复选框:

add_action( 'woocommerce_before_add_to_cart_button', 'output_custom_text_field', 0 );
function output_custom_text_field() {

//Lots of code then:

<input type="checkbox" id="option1" name="option1">

}

现在,我想在Woocommerce会话中捕获/捕获此复选框选项,然后在以下代码中进行自定义价格计算:

function final_cart_update( $cart_object ) {
    foreach ( $cart_object->get_cart() as $cart_item ) {

            // get the custom pricing for this product
            if (isset( $_POST['option1'])) {
            $pricing_custom = get_post_meta( $cart_item['product_id'], '_number_field_1', true );
            }


            // get product price
            $price = floatval( $cart_item['data']->get_price() );

            // set new price
            $cart_item['data']->set_price( $price + $pricing_custom );
    }
}
add_action( 'woocommerce_before_calculate_totals', 'final_cart_update', 99, 1 );

缺少的部分将捕获复选框选项以在会话中进行设置,例如:

if (isset( $_POST['option1'])) {
    // Set it in session
}

感谢您的帮助。


管理员代码的一部分→

/*-------------------------------------------*/
/* 5. Adding Custom Field */
/*-------------------------------------------*/

// Add custom fields in "product data" settings metabox ("Advanced" tab)
add_action('woocommerce_product_options_advanced','woocious_add_custom_field_product_dashboard');
function woocious_add_custom_field_product_dashboard(){
    global $post;

     echo '<div class="product_custom_field">';

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


     // Minimum Letter Text Box
     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'
                )
        )
    );

     // Maximum Letter Text Box
     woocommerce_wp_text_input( array(
             'id'        => 'addon_service_2',
             'label'     => __('Service 2', 'woocommerce'),
             'description' => __('set custom maximum Lettering text field', 'woocommerce'),
             'desc_tip'  => 'true'
     ) );

     // Number Field
    woocommerce_wp_text_input(
        array(
            'id'                => '_number_field_2',
            'label'             => __( 'Service amount 2', '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>';
}

// Save Inputted Entries, in the Product Dashboard Text Fields.
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
 function woocommerce_product_custom_fields_save($post_id){
    // Checkbox Field
    $checkbox = isset( $_POST['woocious_custom_services_fields'] ) ? 'yes' : 'no';
    update_post_meta( $post_id, 'woocious_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 Maximum Letters
    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_1'] ) )
        update_post_meta($post_id, '_number_field_1', sanitize_text_field( $_POST['_number_field_1'] ) );


}

在产品页面上输出HTML→

// Output Custom Text Field to Product Page
add_action( 'woocommerce_before_add_to_cart_button', 'output_custom_text_field', 0 );
function output_custom_text_field() {

    // Get the checkbox value
    $custom_option = get_post_meta( $post->ID, 'woocious_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="woociousbuy_inner">
                        <div class="woociousbuy_two">
                            <h3>Add on Services</h3>
                            <input type="checkbox" id="option1" name="option1">
                            <label for="option1"><?php global $post; echo get_post_meta($post->ID,'addon_service_1',true);?>
                            <svg class="svgcheckbox" width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"><path d="M13 50.986L37.334 75 88 25" stroke-width="15" stroke="#66bb6a" fill="none" fill-rule="evenodd" stroke-dasharray="150" stroke-dashoffset="150"/></svg>
                        </label><span class="woocense_price bold">€<?php global $post; echo get_post_meta($post->ID,'_number_field_1',true);?></span>
                    </div>

                    <div class="woociousbuy_inner">
                            <input type="checkbox" id="option2" name="option2"/>
                            <label for="option2"><?php global $post; echo get_post_meta($post->ID,'addon_service_2',true);?>
                            <svg class="svgcheckbox" width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"><path d="M13 50.986L37.334 75 88 25" stroke-width="15" stroke="#66bb6a" fill="none" fill-rule="evenodd" stroke-dasharray="150" stroke-dashoffset="150"/></svg>
                        </label><span class="woocense_price bold">€<?php global $post; echo get_post_meta($post->ID,'_number_field_2',true);?></span>
                        </div>
                </div>
        <?php
    }
}

1 个答案:

答案 0 :(得分:1)

您不需要使用任何会话。使用woocommerce_add_cart_item_data过滤器挂钩,例如:

add_action( 'woocommerce_before_add_to_cart_button', 'custom_product_option_checkbox_field' );
function custom_product_option_checkbox_field() {
    echo '<p><label><input type="checkbox" id="option1" name="option1"> '.__("Option 1").'</label></p>';

}

// Add selected add-on option as custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'filter_add_cart_item_data_callback', 10, 3 );
function filter_add_cart_item_data_callback( $cart_item_data, $product_id, $variation_id ) {
    if ( isset( $_POST['option1'] ) && $pricing_custom = get_post_meta( $product_id, '_number_field_1', true ) ) {
        $cart_item_data['pricing_custom'] = $pricing_custom;
        $cart_item_data['unique_key']     = md5( microtime().rand() ); // Make each item unique
    }
    return $cart_item_data;
}

// Change the product price
add_action( 'woocommerce_before_calculate_totals', 'action_before_calculate_totals_callback', 10, 1 );
function action_before_calculate_totals_callback( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Avoiding hook repetition and price calculation problems
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        if ( isset( $cart_item['pricing_custom'] ) ) {
            // Set the calculated price
            $cart_item['data']->set_price( $cart_item['data']->get_price() + $cart_item['pricing_custom'] );
        }
    }
}

代码进入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。