dongzi1209 2015-12-11 20:16
浏览 52

如何更改woocommerce优惠券代码?

I want the coupon to appear on my cart total and on my coupon section. Right now coupon code appear on my cart total and on my cart coupon section. But I want to change it a little bit.

For default, it shows like "coupon: couponcode and then $10 with the remove link."

I want it to print the cart % discount "15% off " for cart total and my coupon section. For my cart total I don't want the "remove link" to appear. I attach an image, I want to look something like that.

For example, I want it look like this.

Cart total - 15% off and then $10.00. Cart coupon - couponcode 15% off and then "remove" link, the remove link is you can remove the couponcode.

demonstration

Wc-cart-functions.php.

I know this make the coupon work. How do I print the discount type. If I remove the "remove" link, it will remove for carts total and my coupon section. But I just want it to remove in my cart total and I want the "Remove" link appear on my coupon section. How do that?

/**
 * Get a coupon label
 *
 * @access public
 * @param string $coupon
 */
function wc_cart_totals_coupon_label( $coupon ) {
    if ( is_string( $coupon ) )
        $coupon = new WC_Coupon( $coupon );

    echo apply_filters( 'woocommerce_cart_totals_coupon_label', esc_html( __( 'Coupon:', 'woocommerce' ) . ' ' . $coupon->code ), $coupon );
}

/**
 * Get a coupon value
 *
 * @access public
 * @param string $coupon
 */
function wc_cart_totals_coupon_html( $coupon ) {
    if ( is_string( $coupon ) ) {
        $coupon = new WC_Coupon( $coupon );
    }

    $value  = array();

    if ( $amount = WC()->cart->get_coupon_discount_amount( $coupon->code, WC()->cart->display_cart_ex_tax ) ) {
        $discount_html = wc_price( $amount );
    } else {
        $discount_html = '';
    }

    $value[] = apply_filters( 'woocommerce_coupon_discount_amount_html', $discount_html, $coupon );

    if ( $coupon->enable_free_shipping() ) {
        $value[] = __( 'Free shipping coupon', 'woocommerce' );
    }

    // get rid of empty array elements
    $value = array_filter( $value );
    $value = implode( ', ', $value ) . ' <a href="' . esc_url( add_query_arg( 'remove_coupon', urlencode( $coupon->code ), defined( 'WOOCOMMERCE_CHECKOUT' ) ? WC()->cart->get_checkout_url() : WC()->cart->get_cart_url() ) ) . '" class="woocommerce-remove-coupon" data-coupon="' . esc_attr( $coupon->code ) . '">' . __( '[Remove]', 'woocommerce' ) . '</a>';

    echo apply_filters( 'woocommerce_cart_totals_coupon_html', $value, $coupon );
}

Cart Coupon (cart-totals.php)

<ul class="cart_totals <?php if ( WC()->customer->has_calculated_shipping() ) echo 'calculated_shipping'; ?>">

    <?php do_action( 'woocommerce_before_cart_totals' ); ?>

    <li>
        <span class="label"><?php _e( 'Subtotal', 'woocommerce' ); ?></span>
        <span class="value"><?php wc_cart_totals_subtotal_html(); ?></span>
    </li>

     <?php foreach ( WC()->cart->get_coupons() as $code => $coupon ) : ?>
        <li class="cart-discount coupon-<?php echo esc_attr( sanitize_title( $code ) ); ?>">
            <span class="label"><?php wc_cart_totals_coupon_label( $coupon ); ?></span>
            <span class="value"><?php wc_cart_totals_coupon_html( $coupon ); ?></span>
        </li>
    <?php endforeach; ?>

  <?php do_action( 'woocommerce_after_cart_totals' ); ?>

</ul>

Coupon section (cart.php)

<section class="row-wrap prod-wrap">

    <div class="row-inner">
    <form action="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" method="post">

    <?php do_action( 'woocommerce_before_cart_table' ); ?>

        <div id="coupon">
          <?php foreach ( WC()->cart->get_coupons() as $code => $coupon ) : ?>
           <li class="cart-discount coupon-<?php echo esc_attr( sanitize_title( $code ) ); ?>">
            <span class="label"><?php wc_cart_totals_coupon_label( $coupon ); ?></span>
            <span class="value"><?php wc_cart_totals_coupon_html( $coupon ); ?></span>
           </li>
        <?php endforeach; ?>
     </div>
  <?php do_action( 'woocommerce_after_cart_table' ); ?>

    </form>
</div>
</section>

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
    • ¥20 关于URL获取的参数,无法执行二选一查询
    • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
    • ¥15 marlin编译错误,如何解决?
    • ¥15 有偿四位数,节约算法和扫描算法
    • ¥15 VUE项目怎么运行,系统打不开
    • ¥50 pointpillars等目标检测算法怎么融合注意力机制
    • ¥20 Vs code Mac系统 PHP Debug调试环境配置
    • ¥60 大一项目课,微信小程序
    • ¥15 求视频摘要youtube和ovp数据集