我不确定这是否可行,但感觉应该是这样,所以我很感激任何帮助。
基本上,我有一个产品列表,我希望通过两个不同的档案(由另一个插件设置)显示这些产品,我可以在其中控制通过使用短代码列出类别来显示的产品。不同的档案需要为相同的产品显示不同的价格,有时不列出某个类别中的特定产品。
所以我所做的是设置类别(可在前端过滤并因此显示它们的名称),为每个存档页面设置相同名称但不同的 slug,然后将每个产品添加到具有相同名称的两个类别中所以它们会出现在每个档案中。
问题是我需要将一个存档中几乎所有产品的基本价格提高 0.50,但我想不出一种方法。我可以像这样在视觉上获得正确的结果:
function bbloomer_alter_price_display( $price_html, $product ) {
global $wp_query;
$slug = basename(get_permalink($wp_query->post->ID));
// Only if not null
if ( '' === $product->get_price() ) return $price_html;
// If on specifc archive page
if ( $slug == "eathere" ) {
$orig_price = wc_get_price_to_display( $product );
$price_html = wc_price( $orig_price + 0.50 );
}
return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'bbloomer_alter_price_display', 9999, 2 );
但很明显,这只会更新视觉价格,一旦将其添加到购物车并结帐,它只会显示基本价格,未更改。
我知道您应该使用 woocommerce_before_calculate_totals 之类的钩子单独更新购物车,但我不知道如何应用它必须来自此存档才能拥有的逻辑修改后的价格。
此代码适用于他们所在的类别:
add_filter( 'woocommerce_product_get_price', 'custom_sale_price_for_category', 10, 2 );
function custom_sale_price_for_category( $price, $product ) {
//Get all product categories for the current product
$terms = wp_get_post_terms( $product->get_id(), 'product_cat' );
foreach ( $terms as $term ) {
$categories[] = $term->slug;
}
if ( ! empty( $categories ) && in_array( 'eathere-burgers', $categories, true ) ) {
$price *= ( 1 + 0.50 );
}
return $price;
}
但这会影响整个网站的价格,无论它们位于哪个存档页面。如果我补充说它应该只通过第一个示例中的 slug 影响存档页面,那么它们会正确出现在存档中,但同样不会在结帐购物车中更改。
我真的很想知道如何在不复制整个产品列表和调整价格的情况下使其正常工作。感觉这应该可以通过一些简单的代码实现,但是 Woocommerce 的挂钩和过滤器的功能非常密集,我发现很难弄清楚。
有人可以帮忙吗?
答案 0 :(得分:0)
我不知道逻辑是否正确,但您可以尝试在“woocommerce_product_get_price”过滤器中使用会话变量来存储存档值
session_start();
$_SESSION["archive"] = "eathere";
现在在购物车/结帐挂钩 'woocommerce_before_calculate_totals' 上获得相同的会话变量值,不要忘记
session_start();
并在此钩子中获取 $_SESSION["archive"]
的值以检查存档 slug 并根据此更新价格。
答案 1 :(得分:0)
在您的代码中,根据您所在的存档页面,将 slug 值分配给 if & elseif 语句中的会话变量
function bbloomer_alter_price_display( $price_html, $product ) {
global $wp_query;
$slug = basename(get_permalink($wp_query->post->ID));
// Only if not null
if ( '' === $product->get_price() ) return $price_html;
// If on specifc archive page
if ( $slug == "eathere" ) {
$_SESSION["archive"] = "eathere";
$orig_price = wc_get_price_to_display( $product );
$price_html = wc_price( $orig_price + 0.50 );
}
elseif ( $slug == some_other_archive_slug ) {
$_SESSION["archive"] = some_other_archive_slug;
$orig_price = wc_get_price_to_display( $product );
$price_html = wc_price( $orig_price + 2 );
}
return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'bbloomer_alter_price_display', 9999, 2 );
在购物车/结帐页面上,将存档页面 slug 的值与 Session 变量 slug 值进行比较,并在此基础上更新产品的价格
add_action( 'woocommerce_before_calculate_totals', 'update_price_cart', 9999 );
function update_price_cart( $cart ) {
// LOOP THROUGH CART ITEMS & ADD Price
if ($_SESSION["archive"] == "eathere"){
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$price = $product->get_price();
$cart_item['data']->set_price( $price + 0.50 );
}
}
elseif ($_SESSION["archive"] == some_other_archive_slug) {
$product = $cart_item['data'];
$price = $product->get_price();
$cart_item['data']->set_price( $price + 2 );
}
}
有时它可能会出错,在这种情况下你需要 start_session();在为会话赋值之前