0

我试图在函数内部获取$pi1$pi2变量值 add_custom_price,但似乎没有任何效果。

我已经研究过设置变量可以从函数类访问,但我不确定我是否理解如何正确访问它们。

add_filter( 'gform_confirmation', array(gravity_pi,custom_confirmation), 10, 4 );
    class gravity_pi {
      public $pi1;
      public $pi2;


      public function custom_confirmation( $confirmation, $form, $entry, $ajax, $product_id, $pi1, $pi2 ) { 
          if( $form['id'] == '2' ) {
            $post = get_post( $entry['post_id'] );
            $this->pi1 = rgar( $entry, '20' );
            $this->pi2 = rgar( $entry, '21' );
            $exclude_list = array("pi24","pi64","pi65","pi66","pi67","pi68","pi69","pi70","pi71","pi72","pi73","pi74","pi75","pi76","pi77","pi78","pi79","pi80","pi81","pi82");
            if(!in_array($this->pi1, $exclude_list) && !empty($this->pi1)){
              $target_product_id = '86';
              $pid1 = '86';
            }else{
              $pid1 = preg_replace("/[^0-9,.]/", "", $this->pi1 );
            }
            if(!in_array($pi2, $exclude_list) && !empty($this->pi2)){
              $target_product_id = '87';
              $pid2 = '87';
            }else{
              $pid2 = preg_replace("/[^0-9,.]/", "", $this->pi2);
            }
            $product_ids = ''.$pid1.','.$pid2.'';
            $url = 'https://*****.com/cart/?add-to-cart='.$product_ids.'';
            $confirmation = array( 'redirect' => $url );
          }
          return $confirmation;
      }

      public function add_custom_price( $cart_object, $entry,$form, $field, $input_id ) {

          foreach ( $cart_object->cart_contents as $key => $value ) {

              if( 86 == $value['data']->id ) {

                  $value['data']->set_price(  $this->pi1 );

              }
              if( 87 == $value['data']->id ) {

                  $value['data']->set_price( $this->pi2 );

              }

         }
      }
    }
    add_action( 'woocommerce_before_calculate_totals', array(gravity_pi,add_custom_price));
4

2 回答 2

0

当您设置类属性(pi1 和 pi2)时,您需要以与访问它们相同的方式引用它们。前任。

<?php
...
$this->pi1 = rgar($entry, '20');

将$pid1 的所有引用(除了你声明它们的地方)更改为$this->pid1。也为 $pid2 执行此操作。

观看此视频 - https://www.youtube.com/watch?v=4c4nP7GLL1c

于 2017-09-15T18:08:29.430 回答
0

您需要使用$this来获取/设置类中的任何属性。

// set pi1 property value
$this->pi1 = 10;

//print/get property value
echo $this->pi1;
于 2017-09-15T18:11:18.093 回答