首页 > 解决方案 > 在 PHP 函数 WooCommerce 中运行 CSS

问题描述

我写了这个函数:

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,我该怎么做?

标签: phpjquerycsswordpresswoocommerce

解决方案


基于:从 php if 语句运行 JavaScript 函数(我建议您先阅读已接受的答案和@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>";
        }
    }
}

希望这可以帮助。


推荐阅读