2016-11-11 12 views
0

親愛なるStackOverflowの友人、 私はWordpressに "サービス機能"を挿入する必要があります - Woocommerceは、サイトから別のものに情報を呼び出します。woocommerceにサービス機能を挿入する

彼らは私に古いウェブマスターのメモを残して、困惑を解決するために助けが必要です。関数をタグに挿入しようとしましたが、PHPファイルに正しく組み込む方法がわかりません。 私はこの機能を公式の既存のwoocommerceページであるtabs.phpに入れたいと思っています(私はテンプレート内のフォルダで上書きしますが、これは問題ありません)。

これは私が受信したノート、スクリプトです:

$(document).ready(function() { 
var nf = $('.content_nutritional_facts'); 
if (nf.length) { 
    var id = nf.attr('id'); 
    var url ="http://www.ourwebsite.it/__SCRIPT__NutritionalFactsService.php?   
sku="+id; 

    $.ajax({ 
     type: "GET", 
     dataType: "jsonp", 
     url: url, 
     success: function (data) { 
      nf.html(data); 
     } 
    }); 
    } 
}); 

私はにコードを配置したいファイルがtabs.php(Woocommerce)であり、その内容は

<?php 
if (! defined('ABSPATH')) { 
exit; 
} 

$tabs = apply_filters('woocommerce_product_tabs', array()); 

if (! empty($tabs)) : ?> 

<div class="woocommerce-tabs wc-tabs-wrapper"> 

<!--start TRIED TO PUT HERE THE SCRIPT--> 
<script> 
jQuery(document).ready(function() { 
var nf = jQuery('.content_nutritional_facts'); 
if (nf.length) { 
    var id = nf.attr('id'); 
    var url = "http://www.mywebsite.it/dropshipping/__SCRIPT__NutritionalFactsService.php?sku="+id; 

    jQuery.ajax({ 
     type: "GET", 
     dataType: "jsonp", 
     url: url, 
     success: function (data) { 
      nf.html(data); 
     } 
    }); 
    } 
});</script> 
<!--end TRIED TO PUT HERE THE SCRIPT--> 

<!--start TRIED TO PUT HERE THE html div--> 
<div class="content_nutritional_facts" id="<?php echo ($sku = $product- >get_sku()) ? $sku : __('N/A', 'woocommerce'); ?>"> something should appear  </div> 
<!--end TRIED TO PUT HERE THE html div--> 

    <ul class="tabs wc-tabs"> 
     <?php foreach ($tabs as $key => $tab) : ?> 
      <li class="<?php echo esc_attr($key); ?>_tab"> 
       <a href="#tab-<?php echo esc_attr($key); ?>"><?php echo  apply_filters('woocommerce_product_' . $key . '_tab_title', esc_html( $tab['title']), $key); ?></a> 
      </li> 
     <?php endforeach; ?> 
    </ul> 
    <?php foreach ($tabs as $key => $tab) : ?> 
     <div class="woocommerce-Tabs-panel woocommerce-Tabs-panel--<?php echo esc_attr($key); ?> panel entry-content wc-tab" id="tab-<?php echo esc_attr($key); ?>"> 
      <?php call_user_func($tab['callback'], $key, $tab); ?> 
     </div> 
    <?php endforeach; ?> 
    </div> 

<?php endif; ?> 

これはありますoriginal tabs.php

お寄せいただきありがとうございます!ここで

+0

私はあなたが達成しようとしているかについては明らかではありませんよ。新しいタブを作成する必要があり、そのタブは別のサイトからデータを動的に取得する必要がありますか?そして、クロスドメインURLでajaxを使用することが可能かどうかはわからないので、おそらく['wp_remote_get()'](https://codex.wordpress.org/Function_API/wp_remote_get)を調べることをお勧めします。 – helgatheviking

+0

ありがとうあなたは@helgatheviking私はあなたのリンクを見てみましょう...はい私は新しいタブを作成したいと思いますが、私は達成しようとしていると私はできません..何かをその機能から表示することです今すぐappearencesを心配し、スクリプトが動作していることを確認し、最初のWebサイトから取得したテキストを表示するだけです)。本当にありがとう、私はあなたに知らせる! – Elena

答えて

0

は、新しいタブを作成し、一部のリモートコンテンツをつかむためにwp_remote_get()を使用する方法は次のとおりです。

// Adds the new tab 
add_filter('woocommerce_product_tabs', 'woo_nutritional_info_product_tab'); 
function woo_nutritional_info_product_tab($tabs) { 
    $tabs['desc_tab'] = array(
     'title'  => __('Nutritional Information', 'your-plugin'), 
     'priority' => 50, 
     'callback' => 'woo_nutritional_info_product_tab_content' 
    ); 
    return $tabs; 
} 

// The new tab content 
function woo_nutritional_info_product_tab_content() { 

    $product_id = get_the_ID(); 
    $url = "http://www.website.it/dropshipping/__SCRIPT__NutritionalFactsService.php?sku=" . $product_id; 

    // I'd suggest using the Transient API https://codex.wordpress.org/Transients_API to store the response 
    $response = wp_remote_get($url); 

    if (is_array($response) && ! is_wp_error($response)) { 
     $headers = $response['headers']; // array of http header lines 
     $body = $response['body']; // use the content 

     // do something with the response 
     var_dump($body); 
    } 
} 
関連する問題