2017-09-15 86 views
0

我試圖獲得add_custom_price函數中的$pi1$pi2變量值,但似乎沒有任何工作。我如何獲得另一個變量的值

我已經看過設置變量可以從函數類訪問,但我不知道如何正確訪問它們。

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)); 
+0

我看到你沒有在你的add_custom_price()函數中返回任何東西。 –

回答

0

當你設置你需要引用它們在你訪問它們以同樣的方式類的屬性(PI1 & PI2)。防爆。

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

將$ pid1的所有引用(除了聲明它們的地方)更改爲$ this-> pid1。爲$ pid2做這個。

看看這個視頻 - https://www.youtube.com/watch?v=4c4nP7GLL1c

0

您需要使用$this獲取/設置在類中的任何屬性。

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

//print/get property value 
echo $this->pi1; 
相關問題