简体   繁体   English

用于Woocommerce变量产品的混合php和jQuery代码

[英]Mixed php and jQuery code for Woocommerce variable products

I am having a problem in adding the product as a variable with variations. 我在将产品添加为具有变体的变量时遇到问题。 I am trying to add accessories on every product page but its working for simple product and is creating the problem in a variable or with variations 我正在尝试在每个产品页面上添加附件,但它适用于简单的产品,并且在变量或变量中创建问题

i tried to solve this 我试图解决这个问题

//this variable code is not working//
 <?php if($currentProduct->get_type() == "variable"){ ?>
    var varRules = $(".variations_form").data('product_variations');
    <?php foreach($currentProduct->get_attributes() as $k=>$v){ ?>
    var attribute_<?= getVar($k) ?> = $("select[name=attribute_<?= $k ?>]").val();   
    <?php } ?> 
    $.each(varRules,function(){
      <?php 
      $cond = [];
      foreach($currentProduct->get_attributes() as $k=>$v){ 
      $cond[] = '(this.attributes["attribute_'.$k. '"]=='. 'attribute_'.getVar($k).' || this.attributes["attribute_'.$k. '"]=="")';
      } ?>                    
      if(<?=implode(" && ", $cond)?>){
        total = this.display_price; 
      }   
    });
    //not even this grouped one//  
    <?php } else if($currentProduct->get_type() == "grouped"){ ?>
     var total = 0;
     <?php foreach($currentProduct->get_children() as $k=>$id){ ?>                    
     total += $("[name='quantity[<?= $id ?>]']").val() * <?= getPrice(wc_get_product($id)); ?>;
     <?php } ?>
     <?php } else { ?>
     total = parseFloat(<?= getPrice($currentProduct) ?>);
 <?php } ?>

i want it to work for variable products 我希望它适用于可变产品

As it seems that you are using php inside some jQuery code, there are some errors like: 因为你似乎在一些jQuery代码中使用php,所以有一些错误,如:

  • Your php opening tags should all be always like <?php but not like <?= 你的php开放标签应该总是像<?php但不喜欢<?=
  • When you want to display something like a variable from php in jQuery, dont forget echo 当你想在jQuery中显示类似php的变量时, 不要忘记echo
  • Dont forget missing ; 不要忘记失踪; before your closing php tags. 在你关闭php标签之前。

I have revisited your code, but nobody can test it as it's an extract with missing things and the context is also missing from your question. 我已经重新访问了你的代码,但是没有人可以测试它,因为它是一个缺少的东西,你的问题中也缺少上下文。

<?php if($currentProduct->get_type() == "variable"){ ?>
    var varRules = $(".variations_form").data('product_variations');
    <?php foreach($currentProduct->get_attributes() as $k => $v){ ?>
    var attribute_<?php echo getVar($k); ?> = $("select[name=attribute_<?php echo $k; ?>]").val();   
    <?php } ?> 
    $.each(varRules,function(){
        <?php 
        $cond = [];
        foreach($currentProduct->get_attributes() as $k=>$v){ 
            $cond[] = '(this.attributes["attribute_'.$k.'"]=='. 'attribute_'.getVar($k).' || this.attributes["attribute_'.$k.'"]=="")';
        } ?>                    
        if(<?php echo implode(" && ", $cond) ?>){
            total = this.display_price; 
        }   
    });
    //not even this grouped one//  
    <?php } else if($currentProduct->get_type() == "grouped"){ ?>
     var total = 0;
     <?php foreach($currentProduct->get_children() as $k=>$id){ ?>                    
     total += $("[name='quantity[<?php echo $id; ?>]']").val() * <?php echo getPrice(wc_get_product($id)); ?>;
     <?php } ?>
     <?php } else { ?>
     total = parseFloat(<?php echo getPrice($currentProduct); ?>);
 <?php } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM