Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php sprintf: combine 2 variables values (string) in %s

With WooCommerce, I would like the the value of $gclid variable to be combined in the href="%s" below. I edited it by placing the $gclid after the $product->add_to_cart_url(), but that doesn't work.

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

What I am doing wrong?

Thanks.

like image 692
Nomadic Pimp Avatar asked Mar 20 '26 12:03

Nomadic Pimp


1 Answers

Your question is a little bit unclear
You should specify first that you want to edit the loop/add-to-cart.php WooCommerce template that displays add-to-cart button on shop and archives WooCommerce pages.

As it set a GET Url in the href <a> tag like for example:

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

To make it work you will need to add something like:

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

This will work adding & + gclid= + the value of gclid (here for example 601)…

So the code below will work perfectly (see it in action on 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'];

This should work…

like image 79
LoicTheAztec Avatar answered Mar 23 '26 02:03

LoicTheAztec