对于 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
);
我做错了什么?
谢谢。
最佳答案
你的问题有点不清楚
您应该首先指定要编辑 loop/add-to-cart.php
WooCommerce 模板,在商店和存档 WooCommerce 页面上显示添加到购物车按钮。
因为它在 href
中设置了一个 GET Url <a>
标签,例如:
http://www.myshop.com/shop/?add-to-cart=208
要使其正常工作,您需要添加如下内容:
http://www.myshop.com/shop/?add-to-cart=208&gclid=601
这将工作添加 &
+ gclid=
+ 的值gclid
(这里例如 601
)...
因此下面的代码将完美地工作(在 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>;
The problem is certainly coming from your
$gclid=$_GET['gclid'];
.
You have to be sure that you are getting the value of'gclid'
.Once you are sure you are getting
'gclid'
value, you should change it a little bit, this way:$gclid= '&gclid=' . $_GET['gclid'];
这应该可以……
关于PHP sprintf : combine 2 variables values (string) in %s,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40957938/