插件开发-添加代码到各个页面

添加全局的代码

在做seo或者其他的一些统计功能的时候,通常是需要添加全局的代码的;

通常是在主题文件的header.php的文件中直接添加,如下是在插件中实现这个功能:

这种一般直接是在wp_head中挂一个钩子就行:

 

function __construct(){
        add_action( 'wp_head', array($this, 'add_global_script'));
}

public function add_global_script(){
   echo  "<script>test='123test'</script>";
}

 

    • 产品页面数据获取
      if( is_product()){
                  $product = get_product(get_the_ID());
                  echo 'p_name:'.$product->get_name()."<br>";
                  echo 'p_title:'.$product->get_title()."<br>";
                  echo 'p_sku:'.$product->get_sku()."<br>";
                  echo 'p_price:'.$product->get_price()."<br>";
      
                  $variations =$product->get_available_variations(); 
                  foreach ($variations as $k => $v) {
                      echo 'p_v_id:'.$v['variation_id']."<br>";
                      echo 'p_v_name:'.$v['name']."<br>";
                      echo 'p_v_sku:'.$v['sku']."<br>";
                      echo 'p_v_price:'.$v['display_price']."<br>";
                  }
              }
      

       

    • 加车页面数据获取
      if( is_cart() ){
                  $items = WC()->cart->get_cart();
                  // var_dump($items);
                  foreach($items as $i=>$fetch){
                      $item = $fetch['data']->post;
                      $cart[]=array(
                          'id'        => $fetch['product_id'], 
                          'name'        => $item->post_title, 
                          'description' => $item->post_content, 
                          'quantity'    => $fetch['quantity'], 
                          'price'      => get_post_meta($fetch['product_id'], '_price', true)
                      );
                  }
              }
      

        


  • 结账页面数据获取
    if( is_cart() ){
                $items = WC()->cart->get_cart();
                // var_dump($items);
                foreach($items as $i=>$fetch){
                    $item = $fetch['data']->post;
                    $cart[]=array(
                        'id'        => $fetch['product_id'], 
                        'name'        => $item->post_title, 
                        'description' => $item->post_content, 
                        'quantity'    => $fetch['quantity'], 
                        'price'      => get_post_meta($fetch['product_id'], '_price', true)
                    );
                }
            }
    

      



  • 成功页面数据获取
    $order = wc_get_order( $order_id );
    if ($order) {
                $currency = $order->get_currency();
                $total  = $order->get_total();
                $quantity = $order->get_item_count();
               // echo $order->get_product();
            }
            $product = [];
            $jsonStr = '';
            foreach ($order->get_items() as $key => $item) {
                // var_dump($item);
                $product[] = $item['product_id'];
                $product_id = $item['product_id'];
                $product_instance = wc_get_product($product_id);
    
                $jsonStr .= 
                      '{content_id: "'.$item['product_id'].'",'.
                      'content_type: "product",'.
                      'content_name: "'.$product_instance->get_title().'",'.
                      'quantity: "'.$item['quantity'].'",'.
                      'price: "'.round( $item['subtotal'], 2 ).'"},';
            }
posted @ 2022-05-13 10:04  德玛东亚  阅读(46)  评论(0)    收藏  举报