在 Woocommerce 中访问时自动将最新发布的产品添加到购物车
Automatically add to cart latest published product on visit in Woocommerce
在 WooCommerce 中,我目前使用的功能是在页面访问时自动将特定产品(此处产品 ID 87)添加到购物车,使用 this code snippet:
// add item to cart on visit
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 87;
$found = false;
//check if product already in cart
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 );
}
}
}
如何在此函数中从 woocommerce 动态获取最新的产品 ID,而不是特定的产品 ID?
如有任何帮助或建议,我们将不胜感激。
这可以很容易地在您的代码片段中包含一个非常简单的 SQL 查询来获取最后发布的产品 ID。
WC 官方代码片段已过时,因为 $product->id
已被 $product->get_id()
取代。
完成重访代码:
add_action( 'template_redirect', 'auto_add_to_cart_last_product' );
function auto_add_to_cart_last_product() {
if ( is_admin() ) return; // Not for admin area
$cart = WC()->cart;
global $wpdb;
$found = false;
// SQL Query: Get last published product ID
$result = $wpdb->get_col(" SELECT ID FROM {$wpdb->prefix}posts
WHERE post_type LIKE 'product' AND post_status LIKE 'publish' ORDER BY ID DESC LIMIT 1");
$newest_product_id = reset($result);
// Check if product already in cart
if ( ! $cart->is_empty() )
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Added Woocommerce 3+ compatibility (as $product->id si outdated)
$product_id = method_exists( $cart_item['data'], 'get_id' ) ? $cart_item['data']->get_id() : $cart_item['data']->id;
if ( $product_id == $newest_product_id ){
$found = true; // Found!
break; // We exit from the loop
}
}
// If product not found or if there is no product in cart, we add it.
if ( ! $found || $cart->is_empty() )
$cart->add_to_cart( $product_id );
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。
从 woocommerce 版本 2.4 到最新版本都经过测试和工作。
在 WooCommerce 中,我目前使用的功能是在页面访问时自动将特定产品(此处产品 ID 87)添加到购物车,使用 this code snippet:
// add item to cart on visit
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 87;
$found = false;
//check if product already in cart
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 );
}
}
}
如何在此函数中从 woocommerce 动态获取最新的产品 ID,而不是特定的产品 ID?
如有任何帮助或建议,我们将不胜感激。
这可以很容易地在您的代码片段中包含一个非常简单的 SQL 查询来获取最后发布的产品 ID。
WC 官方代码片段已过时,因为 $product->id
已被 $product->get_id()
取代。
完成重访代码:
add_action( 'template_redirect', 'auto_add_to_cart_last_product' );
function auto_add_to_cart_last_product() {
if ( is_admin() ) return; // Not for admin area
$cart = WC()->cart;
global $wpdb;
$found = false;
// SQL Query: Get last published product ID
$result = $wpdb->get_col(" SELECT ID FROM {$wpdb->prefix}posts
WHERE post_type LIKE 'product' AND post_status LIKE 'publish' ORDER BY ID DESC LIMIT 1");
$newest_product_id = reset($result);
// Check if product already in cart
if ( ! $cart->is_empty() )
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Added Woocommerce 3+ compatibility (as $product->id si outdated)
$product_id = method_exists( $cart_item['data'], 'get_id' ) ? $cart_item['data']->get_id() : $cart_item['data']->id;
if ( $product_id == $newest_product_id ){
$found = true; // Found!
break; // We exit from the loop
}
}
// If product not found or if there is no product in cart, we add it.
if ( ! $found || $cart->is_empty() )
$cart->add_to_cart( $product_id );
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。
从 woocommerce 版本 2.4 到最新版本都经过测试和工作。