wordpress Woocommerce,获取当前产品 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27385920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Woocommerce, get current product id
提问by shparkison
I'm currently working on a WooCommerce theme and attempting to add a sidebar to the product detail page.
我目前正在研究 WooCommerce 主题并尝试向产品详细信息页面添加侧边栏。
I've been able to get the sidebar added (specifically, this one: http://woocommerce.wp-a2z.org/oik_file/templatescontent-widget-product-php/)
我已经能够添加侧边栏(特别是这个:http: //woocommerce.wp-a2z.org/oik_file/templatescontent-widget-product-php/)
Now, I'm trying to figure out how to add a class of "active" to the currently selected product and can't seem to figure it out?
现在,我想弄清楚如何向当前选定的产品添加一类“活动”,但似乎无法弄清楚?
In other words, how do I do something along the lines of if the current product id is equal to the product id in the sidebar add class="active" to the li?
换句话说,如果当前产品 id 等于侧栏中的产品 id 添加 class="active" 到 li,我该如何做?
I've made numerous searches and haven't been able to come up with anything useful, so I'm turning here.
我已经进行了多次搜索,但没有找到任何有用的东西,所以我转向这里。
Any help greatly appreciated!!
非常感谢任何帮助!!
回答by Samyer
2017 Update:
2017年更新:
global $product;
$id = $product->get_id();
Woocommerce doesn't like you accessing those variables directly. This will get rid of any warnings from woocommerce if your wp_debug is true.
Woocommerce 不喜欢您直接访问这些变量。如果您的 wp_debug 为真,这将消除来自 woocommerce 的任何警告。
回答by helgatheviking
If the query hasn't been modified by a plugin for some reason, you should be able to get a single product page's "id" via
如果查询由于某种原因没有被插件修改,您应该能够通过以下方式获取单个产品页面的“id”
global $post;
$id = $post->ID
OR
或者
global $product;
$id = $product->id;
EDIT: As of WooCommerce 3.0 this needs to be
编辑:从 WooCommerce 3.0 开始,这需要是
global $product;
$id = $product->get_id();
回答by Orlandster
Since WooCommerce 2.2you are able to simply use the wc_get_productMethod. As an argument you can pass the IDor simply leave it empty if you're already in the loop.
由于 WooCommerce2.2您可以简单地使用该wc_get_product方法。作为参数ID,如果您已经在循环中,您可以传递或简单地将其留空。
wc_get_product()->get_id();
wc_get_product()->get_id();
OR with 2 lines
或 2 行
$product = wc_get_product();
$id = $product->get_id();
回答by R T
Retrieve the ID of the current item in the WordPress Loop.
检索 WordPress 循环中当前项目的 ID。
echo get_the_ID();
hence works for the product id too. #tested #woo-commerce
因此也适用于产品 ID。#tested #woo-commerce
回答by Etienne Dupuis
Save the current product id before entering your loop:
在进入循环之前保存当前产品 ID:
$current_product = $product->id;
Then in your loop for your sidebar, use $product->id again to compare:
然后在侧边栏的循环中,再次使用 $product->id 进行比较:
<li><a <? if ($product->id == $current_product) { echo "class='on'"; }?> href="<?=get_permalink();?>"><?=the_title();?></a></li>
回答by Hugo R
your can query woocommerce programatically you can even add a product to your shopping cart. I'm sure you can figure out how to interact with woocommerce cart once you read the code. how to interact with woocommerce cart programatically
您可以以编程方式查询 woocommerce 您甚至可以将产品添加到您的购物车。我敢肯定,一旦您阅读了代码,您就可以弄清楚如何与 woocommerce 购物车进行交互。 如何以编程方式与 woocommerce 购物车交互
====================================
====================================
<?php
add_action('wp_loaded', 'add_product_to_cart');
function add_product_to_cart()
{
global $wpdb;
if (!is_admin()) {
$product_id = wc_get_product_id_by_sku('L3-670115');
$found = false;
if (is_user_logged_in()) {
if (sizeof(WC()->cart->get_cart()) > 0) {
foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if ($_product->get_id() == $product_id)
WC()->cart->remove_cart_item($cart_item_key);
}
}
} else {
if (sizeof(WC()->cart->get_cart()) > 0) {
foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if ($_product->id == $product_id)
$found = true;
}
// if product not found, add it
if (!$found)
WC()->cart->add_to_cart($product_id);
} else {
// if no products in cart, add it
WC()->cart->add_to_cart($product_id);
}
}
}
}

