我想访问页面上当前显示的产品的ID。我正试图在一个单独的自定义插件中实现这一点。我试过了global $post
和global $product
和get_the_ID()
方法,但不起作用。
<?php
/*
Plugin Name: CleverTap
Plugin URI: https://clevertap.com
Description: CleverTap plugin
Author: CleverTap
Author URI: https://clevertap.com
Version: 1.0.0
Copyright: © 2015 WizRocket (email : [email protected])
License: GNU General Public License v3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
/**
* Check if WooCommerce is active
*/
$somevar = 0;
if ( in_array( \'woocommerce/woocommerce.php\', apply_filters( \'active_plugins\', get_option( \'active_plugins\' ) ) ) ) {
if ( ! class_exists( \'CleverTap\' ) ) {
/**
* Localisation
**/
load_plugin_textdomain( \'clevertap\', false, dirname( plugin_basename( __FILE__ ) ) . \'/\' );
class CleverTap {
public function __construct() {
// called only after woocommerce has finished loading
add_action( \'woocommerce_init\', array( &$this, \'woocommerce_loaded\' ) );
// called after all plugins have loaded
add_action( \'plugins_loaded\', array( &$this, \'plugins_loaded\' ) );
// called just before the woocommerce template functions are included
add_action( \'init\', array( &$this, \'include_template_functions\' ), 20 );
// indicates we are running the admin
if ( is_admin() ) {
// ...
}
// indicates we are being served over ssl
if ( is_ssl() ) {
// ...
}
// take care of anything else that needs to be done immediately upon plugin instantiation, here in the constructor
add_action( \'admin_init\', array( &$this, \'clevertap_admin_init\'));
add_action( \'admin_menu\', array( &$this, \'clevertap_admin_menu\') );
add_action(\'wp_head\', array( &$this, \'embedajs\'));
add_action(\'woocommerce_add_to_cart\', array( &$this, \'add\'));
}
public function add(){
GLOBAL $somevar;
$somevar++;
//get the current product id...in $id
$id=;
$p=WC()->product_factory->get_product($id)->get_title();
echo "<script>console.log( \'Debug Objects: " . $p . "\' );</script>";
}
/**
* Take care of anything that needs woocommerce to be loaded.
* For instance, if you need access to the $woocommerce global
*/
public function woocommerce_loaded() {
//
}
/**
* Take care of anything that needs all plugins to be loaded
*/
public function plugins_loaded() {
// ...
}
/**
* Override any of the template functions from woocommerce/woocommerce-template.php
* with our own template functions file
*/
public function include_template_functions() {
include( \'clevertap-template.php\' );
}
// finally instantiate our plugin class and add it to the set of globals
$GLOBALS[\'clevertap\'] = new CleverTap();
}
}
我是否遗漏了什么或者该使用哪个动作钩?
如果有人能帮忙,请帮忙!
谢谢
最合适的回答,由SO网友:Scriptonomy 整理而成
指定要传递给挂钩函数的参数数。。。
add_action(\'woocommerce_add_to_cart\', array( &$this, \'add\'), 10, 2);
在这种情况下,我通过了2。。。第二个参数是product\\u id。。。
现在像这样在这里提取product\\u id。。。
public function add($instance, $product_id){
GLOBAL $somevar;