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

初学者115

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

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
    }
}
LoicTheAztec

您不需要为此使用任何会话。使用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文件中。经过测试和工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过WooCommerce产品设置中的自定义复选框禁用添加到购物车按钮

用产品自定义字段值替代woocommerce购物车项目价格

通过自定义字段在Woocommerce中更改购物车中的产品价格

自定义复选框,在WooCommerce购物车页面中添加费用

购物车页面Woocommerce中的自定义“条款和条件”接受复选框

根据自定义字段和数量阈值更改WooCommerce购物车项目价格

根据Woocommerce中的自定义购物车数据更改购物车价格

根据自定义管理复选框的条件更改商店档案上的“添加到购物车”按钮

WooCommerce-为购物车中的每个产品添加自定义价格

将自定义产品计算价格添加到Woocommerce购物车

将自定义产品价格乘以WooCommerce购物车中的数量

WooCommerce中的自定义条件购物车项目价格

在WooCommerce中显示自定义计算的购物车项目价格

在Woocommerce中将购物车项目的价格替换为自定义字段值

在Woocommerce中将具有自定义数据的产品添加为单独的购物车项目

如何将自定义选项(如自定义价格)传递给WooCommerce购物车

基于 Woocommerce 中的维度自定义字段的自定义购物车项目价格计算

从Woocommerce 3中的隐藏输入字段自定义价格中设置购物车项目价格

如何在使用自定义选项选择价格添加到magento的购物车中之前更改价格?

在Woocommerce结帐页面中添加到购物车中的产品的复选框字段

在将产品添加到购物车之前,需要Woocommerce复选框

Woocommerce结帐:将产品添加到购物车的复选框

附加的自定义按钮,可在添加到Wovcommerce中的购物车之前更改产品价格

WooCommerce购物车-动态价格变量传递到自定义价格挂钩

更改购物车中WooCommerce产品变化的原始价格

在Woocommerce微型购物车/购物车中设置自定义计算商品价格

在Woocommerce中为类别应用优惠券时,自定义购物车项目价格

根据Woocommerce中特定产品属性值更改购物车项目价格

更改 WooCommerce 订阅产品的注册费购物车项目价格