根据自定义"woocommerce_variation_option"将复选框添加到WooCommerce结帐页面 [英] Add checkbox to WooCommerce checkout page based on custom "woocommerce_variation_option"

查看:113
本文介绍了根据自定义"woocommerce_variation_option"将复选框添加到WooCommerce结帐页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码在产品中添加了其他变体选项:

I've added an additional variation option to my products with the following code:

add_action('woocommerce_variation_options', 'he_add_to_variation_option', 10, 3);
function he_add_to_variation_option( $loop, $variation_data, $variation){
    $is_trial = (get_post_meta($variation->ID, '_trialversion', true)) ? ' checked' : '';
    ?>
    <label class="tips" data-tip="<?php esc_attr_e( 'Enable this option to make as a trial version', 'woocommerce' ); ?>">
        <?php esc_html_e( 'Trial Version?', 'woocommerce' ); ?>
        <input type="checkbox" class="checkbox variable_is_trial_version" name="_trialversion[<?php echo esc_attr( $variation->ID ); ?>]"<?php echo $is_trial;?>/>
    </label>
<?php
}

add_action( 'woocommerce_save_product_variation', 'save_trialversion_option_fields'  );
function save_trialversion_option_fields( $post_id ) {
    if ( isset( $_POST['_trialversion'] ) ){
        foreach ( $_POST['_trialversion'] as $productid=>$checked ){
            update_post_meta( $productid, '_trialversion', 'yes' );
        }
    }    
}

这很好用,显示在变体中,并正确保存在数据库中.

This works great, it's displayed within the variants and it's saved correctly in the database.

到目前为止,很好.

现在,如果产品被标记为试用版",我想在结帐时添加一个额外的复选框.我正在使用德语"插件也是如此,它具有用于自定义复选框的选项,但我无法识别上述代码对我所做的更改.

Now, I would like to add an additional checkbox at checkout, if a product is flagged as "trial version". I'm using the "Germanized" plugin as well, which has options for custom checkboxes, but I can't get it to recognize the changes I've made with the above code.

如何为我的试用版变体完成自定义复选框?不管有没有德国化,在这一点上我只想让它工作.也许有一个免费的插件,但是如果我可以通过添加一些代码来做到这一点,那可能会更容易.

How would I accomplish the custom checkbox for my trial version variants? With or without Germanized, at this point I just want to get it to work. Maybe there's a free plugin, but if I can just do it by adding some code, that would probably be easier.

该复选框必须是完成试用版购买的必填项.

The checkbox would have to be a required one to complete the purchase of the trial version.

希望有人对如何做到这一点有所了解.期待您的回复!

Hopefully someone has an idea on how to do this. Looking forward to your replies!

推荐答案

当产品变体启用试用版"时,将在结帐页面上添加一个新复选框

When a product variant has the "trialversion" enabled, a new checkbox will be added at the checkout page

woocommerce_save_product_variation不应包含foreach循环,该函数的第二个参数已包含一个计数器$i

woocommerce_save_product_variation should not contain a foreach loop, the 2nd parameter of the function already contains a counter $i

通常也应该解决复选框的问题

Normally the problem with the checkboxes should also be solved

function add_to_variation_option( $loop, $variation_data, $variation){
    $is_trial = get_post_meta( $variation->ID, '_trialversion', true);

    if ( $is_trial == 'yes' ) {
        $is_trial = 'checked';
    } else {
        $is_trial = '';     
    }

    ?>
    <label class="tips" data-tip="<?php esc_attr_e( 'Enable this option to make as a trial version', 'woocommerce' ); ?>">
        <?php esc_html_e( 'Trial Version?', 'woocommerce' ); ?>
        <input type="checkbox" class="checkbox variable_is_trial_version" name="_trialversion[<?php echo esc_attr( $loop ); ?>]"<?php echo $is_trial;?>/>
    </label>
    <?php
}
add_action('woocommerce_variation_options', 'add_to_variation_option', 10, 3);

function save_trialversion_option_fields( $variation_id, $i ) {
    if ( !empty($_POST['_trialversion']) && !empty( $_POST['_trialversion'][$i] ) ) {
        update_post_meta( $variation_id, '_trialversion', 'yes' );
    } else {
        update_post_meta( $variation_id, '_trialversion', 'no' ); 
    }       
}
add_action( 'woocommerce_save_product_variation', 'save_trialversion_option_fields', 10, 2 );

/**
 * Add checkbox field to the checkout
 **/ 
function my_custom_checkout_field( $checkout ) {
    // Get $product object from Cart object
    $cart = WC()->cart->get_cart();

    foreach( $cart as $cart_item ) {                
        // The WC_Product object
        $product = wc_get_product( $cart_item['product_id'] );

        // Checks the product type, 'variable', returns boolean
        if ( $product->is_type( 'variable' ) ) {
            // Get variation id
            $variation_id = $cart_item['data']->get_id();

            // Get post meta
            $trialversion = get_post_meta( $variation_id, '_trialversion', true);

            // Found
            if ( $trialversion == 'yes' ) {
                $trialversion = 'found';

                // Break loop
                break;
            }
        }       
    }

    // Found
    if ( isset($trialversion) && $trialversion == 'found' ) {
        echo '<div id="my-new-field">';

        woocommerce_form_field( 'my_checkbox', array(
            'type'          => 'checkbox',
            'class'         => array('input-checkbox'),
            'label'         => __('I agree'),
            'required'  => true,
        ), $checkout->get_value( 'my_checkbox' ));

        echo '</div>';
    }
}
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field', 10, 1 );

这篇关于根据自定义"woocommerce_variation_option"将复选框添加到WooCommerce结帐页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆