【问题标题】:Customize a checkout field if there are applied coupons in WooCommerce如果在 WooCommerce 中有应用的优惠券,则自定义结帐字段
【发布时间】:2021-06-16 18:33:24
【问题描述】:

如果在 Woocommerce 中应用优惠券,需要控制订单备注结帐字段:我已经尝试过,但不会成功。

add_action('woocommerce_applied_coupon', 'apply_product_on_coupon');
function apply_product_on_coupon( ) {
    if (WC()->cart->has_discount('test')) {
        $fields['billing']['billing_customer_note']['placeholder'] = 'You can have up to three initials';
        $fields['billing']['billing_customer_note']['label'] = 'Personalise your Tote bag';
        $fields['billing']['billing_customer_note']['required'] = true;
        $fields['billing']['billing_customer_note']['input_class'] = array('tote-bag');
    }
    return $fields;
}

我知道 $fields 位有效,因为我在不同的场景中使用过它。如果我们假设优惠券代码是“测试”,我需要订单备注是强制性的,标签和占位符已更改并应用了一些额外的 CSS 类。

有什么建议吗?

【问题讨论】:

  • Team dolphin 的代码没有使用正确的方式,因为每次客户在结帐时添加或删除优惠券时,都会重新加载页面(并且客户将丢失所有输入数据的字段)。此外,如果客户在使用优惠券时尝试下订单而不填写订单备注,则该字段也不会被验证为必填项。

标签: php jquery wordpress woocommerce checkout


【解决方案1】:

请尝试使用此代码。将此代码添加到您的活动主题 functions.php 文件中。

summer 是优惠券代码名称。替换为您的优惠券代码名称

add_filter('woocommerce_checkout_fields', 'xa_remove_billing_checkout_field');
function xa_remove_billing_checkout_field($fields) {
    global $woocommerce;
    if (!empty(WC()->cart->applied_coupons)){
        if (in_array("summer", WC()->cart->applied_coupons)) {
            $fields['order']['order_comments']['required'] = true;
            $fields['order']['order_comments']['placeholder'] ='custom placeholder';
            $fields['order']['order_comments']['label'] = 'custom label';   
            $fields['order']['order_comments']['input_class'] = array('tote-bag');
        }
    }
    return $fields;
}

希望对你有用。谢谢

更新

如果您从结帐中删除优惠券,请刷新页面。

function action_woocommerce_removed_coupon( $coupon_code ) { 
    if ($coupon_code == "summer") { ?>
        <script>location.reload();</script>
    <?php }
};
add_action( 'woocommerce_removed_coupon', 'action_woocommerce_removed_coupon', 10, 1 );

【讨论】:

  • 差不多。我必须使用我的代码来显示订单注释,但条件可以按我的需要工作。也就是说,如果我删除优惠券,则不会删除更改。有什么想法吗?
  • 如果您从购物车中删除优惠券删除它的工作
【解决方案2】:

由于客户可以在结帐页面中添加或删除优惠券,因此需要 jQuery 和 Php。

仅当优惠券代码应用于订单时,以下代码才会要求结帐订单备注字段,更改其标签和占位符,并将 tote-bag 作为选择器类添加到 textarea 输入字段。它会在需要该字段时处理验证。

请改用以下代码:

add_filter('woocommerce_before_checkout_form', 'custom_order_notes_checkout_fields_js');
function custom_order_notes_checkout_fields_js($fields) {
    // Here below set your custom order notes attributes (when a coupon is applied)
    $field_label = __('custom label');
    $placeholder = __('custom placeholder');
    $label       = 'tote-bag';

    $required    = '<abbr class="required" title="required">*</abbr>';

    wc_enqueue_js( "jQuery(function($){
        var required     = '".$required."',
            label        = '".$field_label."',
            placeholder  = '".$placeholder."',
            input_class  = '".$input_class."',
            class_requ   = 'validate-required',
            class_valid  = 'woocommerce-validated',
            class_unval  = 'woocommerce-invalid woocommerce-invalid-required-field',
            notesPara    = '#order_comments_field',
            notesLabel   = notesPara+' label',
            notesText    = notesPara+' textarea',
            defaultLabel = $(notesText).html(),
            defaultPHold = $(notesText).attr('placeholder'),
            newLabel     = label+'&nbsp'+required;

            console.log(defaultPHold);

        if( $('tr.cart-discount').length > 0 ) {
            $(notesPara).addClass(class_requ).addClass(class_valid);
            $(notesLabel).html(newLabel);
            $(notesText).attr('placeholder', placeholder);
        }

        // On order notes change
        $(document.body).on('change input', notesText, function(){
            if( $('tr.cart-discount').length > 0 ) {
                if( $(this).val() != '' ) {
                    $(notesPara).removeClass(class_unval);
                    if ( ! $(notesPara).hasClass(class_valid) ) {
                        $(notesPara).addClass(class_valid);
                    }
                } else {
                    $(notesPara).removeClass(class_valid);
                    if ( ! $(notesPara).hasClass(class_unval) ) {
                        $(notesPara).addClass(class_unval);
                    }
                }
            }
        });

        // On coupon change
        $(document.body).on('updated_checkout', function(){
            if( $('tr.cart-discount').length > 0 ) {
                if( ! $(notesPara).hasClass(class_requ) ) {
                    $(notesPara).addClass(class_requ).addClass(class_valid);
                    $(notesLabel).html(newLabel);
                    $(notesText).addClass(input_class);
                    $(notesText).attr('placeholder', placeholder);
                }
            } else {
                if( $(notesPara).hasClass(class_requ) ) {
                    $(notesPara).removeClass(class_requ).removeClass(class_valid).removeClass(class_unval);
                    $(notesLabel).html(defaultLabel);
                    $(notesText).addClass(input_class);
                    $(notesText).attr('placeholder', defaultPHold);
                }
            }
        });
    });");
}

// Enable "Order notes" field validation for applied coupons
add_filter('woocommerce_checkout_process', 'validation_checkout_required_order_comments');
function validation_checkout_required_order_comments() {
    $applied_coupons = WC()->cart->get_applied_coupons();
    if ( ! empty($applied_coupons) && isset($_POST['order_comments']) && strlen($_POST['order_comments']) < 3 ) {
        wc_add_notice( __("Order comments is a required custom field whan a coupon is applied", "woocommerce"), 'error' );
    }
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

【讨论】:

  • 谢谢@LoicTheAztec - 您的代码在哪里检查优惠券名称?
猜你喜欢
  • 2017-07-13
  • 1970-01-01
  • 1970-01-01
  • 2016-02-25
  • 2014-09-08
  • 1970-01-01
  • 2020-03-19
  • 2021-02-17
  • 2021-03-18
相关资源
最近更新 更多