好的,我已经对昨天发布的问题进行了更多研究,因此,这是我最初发布的文章的重要更新:
在WooCommerce
中,我希望每页总共显示12个项目。如果项目多于12个,则应显示分页。显示的所有物品都必须有库存。
由于每个项目都是唯一的,因此禁用了库存管理。
首先,我想为每个售罄的商品添加一个类别'art-gallery'
。
然后我要隐藏所有商店页面中售罄的商品,但类别页面'art-gallery'
除外。
我的目标是将所有出售的物品自动移动到单独页面上的美术馆,该页面仅显示出售的艺术品/物品。由于每件艺术品都是独一无二的,因此,除了在WooCommerce
存档页面所说的美术馆外,它不再应该在商店中展示。 (相关类:archive
tax-product_cat
term-art-gallery
term-30
)
我的尝试/方法:
如果我在信息中心中选中“隐藏目录中的缺货商品”,则售罄的商品不再显示在商店中。但是它们也不会显示在类别页面'art-gallery'
上。
我试图用WP_Query
解决这个问题。到目前为止,它可以获取12个项目,没有一个售罄。但是分页和类别不再起作用。无论我单击什么,始终显示相同的12个(库存)商品:
if ( woocommerce_product_loop() ) {
$params = array(
'posts_per_page' => 12,
'post_type' => array('product', 'product_variation'),
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock'
)
)
);
do_action( 'woocommerce_before_shop_loop' );
$getItems = new WP_Query($args);
woocommerce_product_loop_start();
if ( wc_get_loop_prop( 'total' ) ) {
while ( $getItems->have_posts() ) {
$getItems->the_post();
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
wp_reset_query();
}
woocommerce_product_loop_end();
do_action( 'woocommerce_after_shop_loop' );
}
以下代码在禁用库存管理的情况下不起作用:
class studiowp_product_category_on_stock_change {
protected $category_id;
protected $stock_level;
protected $variable_product_id = 0;
protected $variable_product_add_category = false;
public function __construct( $category_id, $stock_level ) {
$this->category_id = $category_id;
$this->stock_level = $stock_level;
add_action( 'woocommerce_product_set_stock', array( $this, 'woocommerce_product_set_stock' ) );
add_action( 'woocommerce_variation_set_stock', array( $this, 'woocommerce_variation_set_stock' ) );
add_action( 'woocommerce_process_product_meta_variable', array( $this, 'woocommerce_process_product_meta_variable' ) );
}
public function woocommerce_process_product_meta_variable( $product_id ) {
/** @var WC_Product_Variable $product */
$product = wc_get_product( $product_id );
if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) {
$managing_stock = true;
$stock_quantity = 0;
if ( !$product->managing_stock() ) {
$available_variations = $product->get_available_variations();
foreach ( $available_variations as $available_variation ) {
$variation = wc_get_product( $available_variation['variation_id'] );
if ( $variation->managing_stock() ) {
$stock_quantity += $variation->get_stock_quantity();
}
else {
$managing_stock = false;
}
}
}
if ( $managing_stock ) {
$category_ids = $product->get_category_ids();
if ( $stock_quantity <= $this->stock_level ) {
if ( !in_array( $this->category_id, $category_ids ) ) {
$category_ids[] = $this->category_id;
$product->set_category_ids( $category_ids );
$product->save();
}
}
else {
if ( in_array( $this->category_id, $category_ids ) ) {
unset( $category_ids[array_search($this->category_id, $category_ids)] );
$product->set_category_ids( $category_ids );
$product->save();
}
}
}
}
}
/**
* @param WC_Product_Variation $variation
*/
public function woocommerce_variation_set_stock( $variation ) {
/** @var WC_Product_Variable $product */
$product = wc_get_product( $variation->get_parent_id() );
$stock_quantity = 0;
$available_variations = $product->get_available_variations();
foreach ( $available_variations as $available_variation ) {
$variation = wc_get_product( $available_variation['variation_id'] );
if ( $variation->managing_stock() ) {
$stock_quantity += $variation->get_stock_quantity();
}
}
$category_ids = $product->get_category_ids();
if ( $stock_quantity <= $this->stock_level ) {
if ( !in_array( $this->category_id, $category_ids ) ) {
$category_ids[] = $this->category_id;
$product->set_category_ids( $category_ids );
$product->save();
}
}
else {
if ( in_array( $this->category_id, $category_ids ) ) {
unset( $category_ids[array_search($this->category_id, $category_ids)] );
$product->set_category_ids( $category_ids );
$product->save();
}
}
}
/**
* @param WC_Product $product
*/
public function woocommerce_product_set_stock( $product ) {
$stock_quantity = $product->get_stock_quantity();
$category_ids = $product->get_category_ids();
if ( $stock_quantity <= $this->stock_level ) {
if ( !in_array( $this->category_id, $category_ids ) ) {
$category_ids[] = $this->category_id;
$product->set_category_ids( $category_ids );
$product->save();
}
}
else {
if ( in_array( $this->category_id, $category_ids ) ) {
unset( $category_ids[array_search($this->category_id, $category_ids)] );
$product->set_category_ids( $category_ids );
$product->save();
}
}
}
}
/**
* Example
* when stock quantity equals or is less than 0, add product to category with id 30
* when stock is greater than 0, remove product from category with id 30
*/
new studiowp_product_category_on_stock_change( 30, 0 );
理想情况下,我会使用functions.php
1)将类别'art-gallery'
应用于每个售罄的商品,并且
2)隐藏商店中所有售罄或属于'art-gallery'
类别的商品;任何一个都可以。
但是在禁用库存管理时,我还没有找到解决售罄商品的方法。不过,如果需要,我将启用它。非常感谢您的帮助。