我通常使用javascript,从不使用php。
我的任务是将此脚本/php代码添加到woocommerce。
<?php
$ImageUrl = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), \'single-post-thumbnail\' )[0];
$ItemId = $product->id;
$Title = $product-> get_title();
$ProductUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$CurrencySymbol = get_woocommerce_currency_symbol();
$Currency = get_woocommerce_currency();
$Price = $product->get_price();
$RegularPrice = $product->get_regular_price();
$DiscountAmount = $RegularPrice - $Price;
$terms = get_terms( \'product_tag\' );
?>
<script>
var Title = "<?php echo $Title; ?>";
var ItemId = "<?php echo $ItemId; ?>";
var ImageUrl = "<?php echo $ImageUrl; ?>";
var ProductUrl = "<?php echo $ProductUrl; ?>";
var CurrencySymbol = "<?php echo $CurrencySymbol; ?>";
var Currency = "<?php echo $Currency; ?>";
var Price = "<?php echo $Price; ?>";
var DiscountAmount = "<?php echo $DiscountAmount; ?>";
var RegularPrice = "<?php echo $RegularPrice; ?>";
var _learnq = _learnq || [];
_learnq.push([\'track\', \'Viewed Product\', {
Title: Title,
ItemId: ItemId,
ImageUrl: ImageUrl,
Url: ProductUrl,
Metadata: {
Currency: Currency,
CurrencySymbol: CurrencySymbol,
Price: Price,
DiscountAmount: DiscountAmount,
RegularPrice: RegularPrice
}
}]);
</script>
只能将其添加到产品页面。
以下是我正在尝试的解决方案:
/*
Add klaviyo script if the page is a product page
*/
if ( is_product() ) {
function add_klaviyo_script() {
echo \'<?php
$ImageUrl = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), \\\'single-post-thumbnail\\\' )[0];
$ItemId = $product->id;
$Title = $product-> get_title();
$ProductUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$CurrencySymbol = get_woocommerce_currency_symbol();
$Currency = get_woocommerce_currency();
$Price = $product->get_price();
$RegularPrice = $product->get_regular_price();
$DiscountAmount = $RegularPrice - $Price;
$terms = get_terms( \\\'product_tag\\\' );
?>
<script>
var Title = "<?php echo $Title; ?>";
var ItemId = "<?php echo $ItemId; ?>";
var ImageUrl = "<?php echo $ImageUrl; ?>";
var ProductUrl = "<?php echo $ProductUrl; ?>";
var CurrencySymbol = "<?php echo $CurrencySymbol; ?>";
var Currency = "<?php echo $Currency; ?>";
var Price = "<?php echo $Price; ?>";
var DiscountAmount = "<?php echo $DiscountAmount; ?>";
var RegularPrice = "<?php echo $RegularPrice; ?>";
var _learnq = _learnq || [];
_learnq.push([\\\'track\\\', \\\'Viewed Product\\\', {
Title: Title,
ItemId: ItemId,
ImageUrl: ImageUrl,
Url: ProductUrl,
Metadata: {
Currency: Currency,
CurrencySymbol: CurrencySymbol,
Price: Price,
DiscountAmount: DiscountAmount,
RegularPrice: RegularPrice
}
}]);
</script>\';
}
/*
Add klaviyo script to footer
*/
add_action( \'wp_footer\', \'add_klaviyo_script\' );
}
我将把它添加到函数中。php文件。
我的问题是,这是将php/script添加到头部的最佳方法吗?我以前没有使用过php或wordpress,所以这对我来说很新。我曾考虑过使用wp\\u enque,但由于我有一些php变量,我不确定它是否会起作用。
SO网友:D. Dan
首先,您应该在页脚中包含此代码。php或类似产品。第二个:
if ( is_product() ) { //you are already in php
function add_klaviyo_script() {
echo \'<?php // dont need to echo php
$ItemId = $product->id; // and variables
解决方案(footer.php):
<footer>
...things
</footer>
<?php
if ( is_product() ) {
$ImageUrl = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), \\\'single-post-thumbnail\\\' )[0];
$ItemId = $product->id;
$Title = $product-> get_title();
$ProductUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$CurrencySymbol = get_woocommerce_currency_symbol();
$Currency = get_woocommerce_currency();
$Price = $product->get_price();
$RegularPrice = $product->get_regular_price();
$DiscountAmount = $RegularPrice - $Price;
$terms = get_terms( \\\'product_tag\\\' );
?>
<script>
var Title = "<?php echo $Title; ?>";
var ItemId = "<?php echo $ItemId; ?>";
var ImageUrl = "<?php echo $ImageUrl; ?>";
var ProductUrl = "<?php echo $ProductUrl; ?>";
var CurrencySymbol = "<?php echo $CurrencySymbol; ?>";
var Currency = "<?php echo $Currency; ?>";
var Price = "<?php echo $Price; ?>";
var DiscountAmount = "<?php echo $DiscountAmount; ?>";
var RegularPrice = "<?php echo $RegularPrice; ?>";
var _learnq = _learnq || [];
_learnq.push([\\\'track\\\', \\\'Viewed Product\\\', {
Title: Title,
ItemId: ItemId,
ImageUrl: ImageUrl,
Url: ProductUrl,
Metadata: {
Currency: Currency,
CurrencySymbol: CurrencySymbol,
Price: Price,
DiscountAmount: DiscountAmount,
RegularPrice: RegularPrice
}
}]);
</script><?php
}
}