【问题标题】:Php sprintf: combine 2 variables values (string) in %sPhp sprintf:在 %s 中组合 2 个变量值(字符串)
【发布时间】:2016-12-04 11:06:18
【问题描述】:

使用 WooCommerce,我希望将 $gclid 变量的值组合到下面的 href="%s" 中。我通过将$gclid 放在$product->add_to_cart_url() 之后对其进行了编辑,但这不起作用。

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

我做错了什么?

谢谢。

【问题讨论】:

  • 有什么错误吗?我看到的是您的语法错误:apply_filters(X, sprintf(X, esc_url(X)。两个右括号在哪里?
  • codeglobal $product; $gclid=$_GET['gclid']; //读取gclid并将其存储在 $gclid echo apply_filters( 'woocommerce_loop_add_to_cart_link', sprintf( '%s', 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 );
  • 没有错误,但无法正常工作。我只是想让链接 href="%s" 包含 $gclid。它是一个链接,我从 url 中提取 gclid 并将其放在链接的末尾。这是完整的代码,我之前没有给出完整的代码来尝试缩短它。谢谢。

标签: php wordpress templates get woocommerce


【解决方案1】:

你的问题有点不清楚
您应该首先指定您要编辑 loop/add-to-cart.php WooCommerce 模板,该模板在商店和存档 WooCommerce 页面上显示 添加到购物车按钮

因为它在 href &lt;a&gt; 标签中设置了一个 GET Url,例如:

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

要使其正常工作,您需要添加以下内容:

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

这将添加 &amp; + gclid= + gclid 的值(例如 @ 987654330@)…

所以下面的代码将完美运行(在 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= '&gclid=' . $_GET['gclid'];

这应该可行……

【讨论】:

  • 非常奇怪的情况。当您将鼠标悬停在链接上时,它不会显示 gclid,当您单击它时,它不会将 gclid 放在链接中。但是,如果你 CTRL+U 查看页面代码,链接实际上有 gclid。知道页面代码如何获得正确的信息,但是显示的内容和工作方式没有吗?谢谢。
  • 是的,我看到了。我向我的添加了相同的代码,但它对我的不起作用。也许是模板?插件?你有什么建议?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多