在显示产品完整列表的WooCommerce商店页面上,我想要获取每个产品的ID,以便能够添加表单按钮并直接捕获每个产品的ID。 我试图插入functions.php:
global $product;
$id = $product->get_id();
和
$product = wc_get_product();
$id = $product->get_id();
但是,要么它不加载页面,要么返回商店的页面ID,而不是我点击的产品。
我将表单放在一个弹出窗口中,在每个产品中,我都有一个自定义字段,在其中放置了一个按钮
我在我的functions.php上创建了一个弹出窗口,在其中插入cf7短代码,并使用动态自定义字段将ID传递给它:
add_shortcode( 'product_id', 'get_product_id' );
function get_product_id() {
//$product_id = sanitize_text_field( $_POST[ 'product_id' ] );
$id = get_the_ID($product_id);
return $id;
}
弹出按钮我将其插入到content-product.php的循环中
echo the_field('compartir');
有了这个,我在每个产品中都有一个按钮,单击时会打开一个带有cf7表单的弹出窗口。但是我得到的ID是页面的ID,而不是每种产品的ID。
我只是使用了这段代码,它返回了相同的页面ID,而不是产品
add_action( 'wpcf7_init', 'mp_cf7_custom_shortcode' );
function mp_cf7_custom_shortcode() {
wpcf7_add_form_tag('id_producto','get_id_producto');
}
function get_id_producto(){
$id = get_the_ID($product_id);
return '<input type="text" name="prod-id" value="'. $id .'" />';
}
有人可以帮我吗?