【问题标题】:Run CSS in PHP function WooCommerce [duplicate]在 PHP 函数 WooCommerce 中运行 CSS [重复]
【发布时间】:2020-03-24 20:59:07
【问题描述】:

我写了这个函数:

add_action( 'woocommerce_before_calculate_totals', 'adding_custom_price', 10, 1);
function adding_custom_price( $cart ) {
global $woocommerce;

    foreach ( $cart->get_cart() as $cart_item ) {

        //If there is minimum 1 coupon active
        if (!empty(WC()->cart->applied_coupons)) {

        }
        //If there isn't any coupon in cart
        else {
          //Here I want to do some styling in CSS or run something in jQuery
        }
    }
}

我想在其中做一些 CSS 并在 else 中运行一些 jQuery,我该怎么做?

【问题讨论】:

  • 你不能在 PHP 代码中运行 CSS / JQuery。

标签: php jquery css wordpress woocommerce


【解决方案1】:

基于:Run a JavaScript function from a php if statement(我建议您先阅读已接受的答案和@treyBake 的评论)

add_action( 'woocommerce_before_calculate_totals', 'adding_custom_price', 10, 1);
function adding_custom_price( $cart ) {
    global $woocommerce;
    foreach ( $cart->get_cart() as $cart_item ) {

        //If there is minimum 1 coupon active
        if (!empty(WC()->cart->applied_coupons)) {

        }
        //If there isn't any coupon in cart
        else {
        //Here I want to do some styling in CSS or run something in jQuery
        echo "<script>function myFunction() {var x = document.getElementsByClassName('checkout-button'); x[0].innerHTML = 'Checkout with no Coupons';}</script>";
        }
    }
}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 2021-03-04
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 2013-10-21
    相关资源
    最近更新 更多