使用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
);
我做错了什么?
感谢。
答案 0 :(得分:2)
你的问题有点不清楚
您应首先指定要修改 loop/add-to-cart.php
WooCommerce模板,该模板在商店和档案库WooCommerce页面上显示添加到购物车按钮。
因为它在 href
<a>
标记中设置了GET Url,例如:
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>;
问题肯定来自您的
$gclid=$_GET['gclid'];
。
您必须确保获得'gclid'
的价值。一旦您确定获得
'gclid'
值,就应该稍微更改一下,这样:$gclid= '&gclid=' . $_GET['gclid'];
这应该有用......