使用短代码在任何地方显示 WooCommerce 优惠券输入字段 [英] Display a WooCommerce coupon input field anywhere with a shortcode

查看:24
本文介绍了使用短代码在任何地方显示 WooCommerce 优惠券输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于将 Woocommerce 优惠券字段插入任何页面所需的小项目,但这对我来说似乎很复杂.我在谷歌上搜索过这个,但没有任何关于这个的资源.找到这个关于插入字段的代码.

将此代码插入文本块:

<form id="ajax-coupon-redeem"><p><input type="text" name="coupon" id="coupon"/><input type="submit" name="redeem-coupon" value="Redeem Offer"/></p><p class="结果"></p></form><!-- #ajax-coupon-redeem -->

这会生成表单,但没有其他代码可以将其处理到页面中?

是否可以通过短代码或其他方式生成?

解决方案

以下自定义 Shortcode,将显示一个文本输入字段(带有提交按钮),用户可以在其中输入要应用的优惠券代码.

用法:[coupon_field] 或在 PHP 代码中 echo do_shortcode("[coupon_field]");

代码:

add_shortcode( 'coupon_field', 'display_coupon_field' );函数 display_coupon_field() {if(isset($_GET['coupon']) &&isset($_GET['redeem-coupon'])){if( $coupon = esc_attr($_GET['coupon']) ) {$applied = WC()->cart->apply_coupon($coupon);} 别的 {$优惠券=假;}$success = sprintf( __('Coupon "%s" 申请成功.'), $coupon );$error = __("此优惠券无法使用");$message = isset($applied) &&$应用?$成功:$错误;}$output = '<div class="redeem-coupon"><form id="coupon-redeem"><p><input type="text" name="coupon" id="coupon"/><input type="submit" name="redeem-coupon" value="'.__('Redeem Offer').'"/></p>';$output .= isset($coupon) ?'<p class="result">'.$message.'</p>':'';返回 $output .'</form></div>';}

代码位于活动子主题(或主题)的 function.php 文件中.经测试有效.

申请优惠券后显示成功或错误信息.

I have a small project on what I need to insert Woocommerce coupon field into any page, but this seems complicated for me. I searched on Google about this, but don't have any resources about this. Found this codes about inserting field.

Inserted into a text block this code:

<div class="redeem-coupon">
<form id="ajax-coupon-redeem">
    <p>
        <input type="text" name="coupon" id="coupon"/>
        <input type="submit" name="redeem-coupon" value="Redeem Offer" />
    </p>
    <p class="result"></p>
</form><!-- #ajax-coupon-redeem -->

And this generate the form, but don't have other code that will handle this into page?

Is possible to generate via shortcode or something?

解决方案

The following custom Shortcode, will display a text imput field (with a submit button) where user can enter a coupon code to be applied.

Usage: [coupon_field] or in Php code echo do_shortcode("[coupon_field]");

The code:

add_shortcode( 'coupon_field', 'display_coupon_field' );
function display_coupon_field() {
    if( isset($_GET['coupon']) && isset($_GET['redeem-coupon']) ){
        if( $coupon = esc_attr($_GET['coupon']) ) {
            $applied = WC()->cart->apply_coupon($coupon);
        } else {
            $coupon = false;
        }

        $success = sprintf( __('Coupon "%s" Applied successfully.'), $coupon );
        $error   = __("This Coupon can't be applied");

        $message = isset($applied) && $applied ? $success : $error;
    }

    $output  = '<div class="redeem-coupon"><form id="coupon-redeem">
    <p><input type="text" name="coupon" id="coupon"/>
    <input type="submit" name="redeem-coupon" value="'.__('Redeem Offer').'" /></p>';

    $output .= isset($coupon) ? '<p class="result">'.$message.'</p>' : '';

    return $output . '</form></div>';
}

Code goes on function.php file of your active child theme (or theme). Tested and works.

It displays a success or an error message, once applying a coupon.

这篇关于使用短代码在任何地方显示 WooCommerce 优惠券输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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