2016-12-04 4 views
3

WooCommerceでは、の値をhref="%s"に組み合わせることをお勧めします。私はの後に$product->add_to_cart_url()を置いて編集しましたが、うまくいきません。PHP sprintf:%sの2つの変数値(文字列)を組み合わせる

global $product; 
$gclid=$_GET['gclid']; //Read gclid and store it in $gclid 

echo apply_filters(
    'woocommerce_loop_add_to_cart_link', 
    sprintf(
     '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>', 
     esc_url($product->add_to_cart_url().$gclid), 
     esc_attr(isset($quantity) ? $quantity : 1), 
     esc_attr($product->id), 
     esc_attr($product->get_sku()), 
     esc_attr(isset($class) ? $class : 'button'), 
     esc_html($product->add_to_cart_text()) 
    ), 
    $product 
); 

私は間違っていますか?

ありがとうございました。

+0

すべてのエラー:あなたは'gclid'値を取得していることを確認したら

は、あなたはそれを少し、この方法を変更する必要がありますか? 'apply_filters(X、sprintf(X、esc_url(X)')は、閉じかっこで囲まれていますか? –

+0

'code'global $ product; $ gclid = $ _GET ['gclid'] ; //読み取りGCLIDと$ GCLID はapply_filters( 'woocommerce_loop_add_to_cart_link'、 \tのsprintf( '%s'、 \t \t esc_url($製品 - > add_to_cart_url()の$ GCLID)、 \t \t esc_attr(ISSETをエコーそれを格納します。 ($数量)?$数量:1)、 \t \t esc_attr($製品 - > ID)、 \t \t esc_attr($製品 - > get_sku())、 \t \t esc_attr(ISSET($クラス)$クラス:? 'ボタン') 、 \t \t esc_html($ product-> add_to_cart_text()) \t)、 $ product); –

+0

エラーはありませんが、動作しません。私はちょうどリンクを作成しようとしていますhref = "%s" include $ gclid。そのリンクと私はURLからgclidを引っ張り、リンクの最後に配置しています。これはコード全体ですが、短縮するためにコード全体を渡すことはありませんでした。ありがとう。 –

答えて

2

ご質問は
少し不明確であるあなたがお店やアーカイブWooCommerceページにアドオン・ツー・カートボタンを表示loop/add-to-cart.php WooCommerceテンプレートを編集することを最初に指定する必要があります。それはのようなhref<a>タグでGET URLを設定したよう

例えば:

http://www.myshop.com/shop/?add-to-cart=208&gclid=601 

この:

http://www.myshop.com/shop/?add-to-cart=208 

が、それはあなたのような何かを追加する必要があります動作させるために追加を行います& + gclid= +(ここでは例601用)gclidの値...

ので次のコードは、完全にthis test server上のアクションでそれを参照してください)動作します:

<?php 
/** 
* Loop Add to Cart 
* 
* This template can be overridden by copying it to yourtheme/woocommerce/loop/add-to-cart.php. 
* 
* HOWEVER, on occasion WooCommerce will need to update template files and you 
* (the theme developer) will need to copy the new files to your theme to 
* maintain compatibility. We try to do this as little as possible, but it does 
* happen. When this occurs the version of the template file will be bumped and 
* the readme will list any important changes. 
* 
* @see   https://docs.woocommerce.com/document/template-structure/ 
* @author  WooThemes 
* @package  WooCommerce/Templates 
* @version  2.5.0 
*/ 

if (! defined('ABSPATH')) { 
    exit; 
} 

global $product; 

// Just for testing (replace after with $gclid=$_GET['gclid'];) 
$gclid = '&gclid=120'; 
// $gclid=$_GET['gclid']; 

echo apply_filters('woocommerce_loop_add_to_cart_link', 
    sprintf('<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>', 
     esc_url($product->add_to_cart_url().$gclid), 
     esc_attr(isset($quantity) ? $quantity : 1), 
     esc_attr($product->id), 
     esc_attr($product->get_sku()), 
     esc_attr(isset($class) ? $class : 'button'), 
     esc_html($product->add_to_cart_text()) 
    ), 
$product); 

// Checking that you get 'gclid' value (just for test) 
// (will display normally the 'gclid' value after the button add-to-cart) 
echo '<p>gclid value: '. $_GET['gclid'] .</p>; 

問題は確かにあなたの$gclid=$_GET['gclid'];から来ています。
'gclid'の値を取得していることを確認する必要があります。

$gclid= '&gclid=' . $_GET['gclid']; 

これは動作するはずです...

+0

非常に奇妙な状況。リンク上にマウスを置くと、gclidが表示されず、クリックするとgclidがリンクに表示されなくなります。ただし、CTRL + Uを押してページコードを表示すると、リンクには実際にgclidが表示されます。どのようにページコードが正しい情報を持つことができるか、何が表示され、動作しないのか、ありがとう。 –

+0

はい私はそれを見ました。私は同じコードを私のものに追加しました。それは私のものでは機能しません。多分それはテンプレートですか?プラグイン?何を指示してるんですか? –

関連する問題