Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在我的functions.php中执行IF语句来确定我是否在singles产品页面上?(商业)_Javascript_Php_Wordpress_Woocommerce - Fatal编程技术网

Javascript 如何在我的functions.php中执行IF语句来确定我是否在singles产品页面上?(商业)

Javascript 如何在我的functions.php中执行IF语句来确定我是否在singles产品页面上?(商业),javascript,php,wordpress,woocommerce,Javascript,Php,Wordpress,Woocommerce,我制作了一个php函数,可以将脚本上传到带有“cart”is\u cart()。但是当我尝试使用is_product()时,它不会在单个产品页面上注册它。如何让我的php在单个产品页面上呈现脚本 我在我的functions.php中调用这个函数 function refresh_update_qty() { if (is_cart() || is_product()): ?> <script type="text/javascript

我制作了一个php函数,可以将脚本上传到带有“cart”
is\u cart()。但是当我尝试使用
is_product()
时,它不会在单个产品页面上注册它。如何让我的php在单个产品页面上呈现脚本

我在我的functions.php中调用这个函数

function refresh_update_qty() {
    if (is_cart() || is_product()):
        ?>
        <script type="text/javascript">

            jQuery('div.woocommerce').on('click', ' button.plus, button.minus', function(e){
                var math_method = '';
                if(jQuery(this).hasClass('plus')) {
                    math_method = "1";
                }else if(jQuery(this).hasClass('plus')) {
                    math_method = "-1";
                } else {
                //    Do nothing
                }
                var this_input = this.parentNode.querySelector('input[type=number]');
                var current_val = this_input.value;
                var new_val = parseInt(current_val) + parseInt(math_method);
                this_input.value = new_val;
                document.getElementById('update_cart_button').disabled = false;
                <?php
                if(is_cart()):
                ?>
                jQuery("[name='update_cart']").trigger("click");
                <?php
                endif;
                ?>
                e.preventDefault();
            });

        </script>
        <?php
    endif;
}
add_action( 'wp_footer', 'refresh_update_qty' );
函数刷新\更新\数量(){
如果(is_cart()| | is_product()):
?>
jQuery('div.woocommerce')。在('click','button.plus',button.减号',函数(e){
var math_方法=“”;
if(jQuery(this).hasClass('plus')){
数学方法=“1”;
}else if(jQuery(this).hasClass('plus')){
数学方法=“-1”;
}否则{
//无所事事
}
var this_input=this.parentNode.querySelector('input[type=number]”);
var current_val=该输入值;
var new\u val=parseInt(当前值)+parseInt(数学方法);
此输入值=新值;
document.getElementById('update\u cart\u button')。disabled=false;
jQuery(“[name='update_cart']”)。触发器(“单击”);
e、 预防默认值();
});

我解决了这个问题。似乎我的产品页面的主体中有woocommerce类,而我的购物车中有一个div。因此,一旦我将
jQuery('div.woocommerce')
更改为
jQuery('woocommerce')
,它就起作用了

最终代码如下

function refresh_update_qty() {
    if (is_cart() || is_product()):
        ?>
        <script type="text/javascript">
            (function($) {

            $('.woocommerce').on('click', ' button.plus, button.minus', function(e){
                var math_method = '';
                if($(this).hasClass('plus')) {
                    math_method = "1";
                }else if($(this).hasClass('minus')) {
                    math_method = "-1";
                } else {
                //    Do nothing
                }
                var this_input = this.parentNode.querySelector('input[type=number]');
                var current_val = this_input.value;
                var new_val = parseInt(current_val) + parseInt(math_method);
                this_input.value = new_val;
                <?php
                if(is_cart()):
                ?>
//                IF CART PAGE UPDATE CART
                document.getElementById('update_cart_button').disabled = false;
                $("[name='update_cart']").trigger("click");
                <?php
                endif;
                ?>
                e.preventDefault();
            });
            })( jQuery );

        </script>
        <?php
    endif;
}
add_action( 'wp_footer', 'refresh_update_qty' );
函数刷新\更新\数量(){
如果(is_cart()| | is_product()):
?>
(函数($){
$('.woocommerce')。在('click','button.plus',button.减号',函数(e){
var math_方法=“”;
if($(this).hasClass('plus')){
数学方法=“1”;
}else if($(this).hasClass('减号')){
数学方法=“-1”;
}否则{
//无所事事
}
var this_input=this.parentNode.querySelector('input[type=number]”);
var current_val=该输入值;
var new\u val=parseInt(当前值)+parseInt(数学方法);
此输入值=新值;
//如果购物车页面更新购物车
document.getElementById('update\u cart\u button')。disabled=false;
$(“[name='update_cart']”)。触发器(“单击”);
e、 预防默认值();
});
})(jQuery);

is_-product()
仍然有效,因此问题在于其他地方。也许告诉你的代码应该做什么或它的目的是有用的。顺便说一句,你可以通过调试:
如果(is_-product()){echo'it works';}其他,用一种简单的方法测试
是_-cart()
是_-product()
还是类似的东西有效{echo'NOT working';}
@7uc1f3r所以我测试了echo,它成功了,但是当我查找我添加的脚本时,它没有成功。但是我找到了一个解决方案,但是由于代码的冗余,它非常混乱。谢谢你的建议。