gpt4 book ai didi

php - 在 WooCommerce 中,随着自定义库存数量的减少,将 "out-of-stock"产品变体变灰

转载 作者:行者123 更新时间:2023-12-03 21:12:32 28 4
gpt4 key购买 nike

我使用了很棒的片段 https://jeroensormani.com/custom-stock-quantity-reduction/向变体添加附加设置,以将主要库存库存减少变体中的设定数量。
我现在面临的问题是它不检查这些变体是否缺货(例如,主要库存为 10,捆绑设置设置为 12 瓶)。
我用来添加总库存减少乘数的代码是:

// For implementation instructions see: https://aceplugins.com/how-to-add-a-code-snippet/

/**
* Simple product setting.
*/
function ace_add_stock_inventory_multiplier_setting() {

?><div class='options_group'><?php

woocommerce_wp_text_input( array(
'id' => '_stock_multiplier',
'label' => __( 'Inventory reduction per quantity sold', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Enter the quantity multiplier used for reducing stock levels when purchased.', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'min' => '1',
'step' => '1',
),
) );

?></div><?php

}
add_action( 'woocommerce_product_options_inventory_product_data', 'ace_add_stock_inventory_multiplier_setting' );

/**
* Add variable setting.
*
* @param $loop
* @param $variation_data
* @param $variation
*/
function ace_add_variation_stock_inventory_multiplier_setting( $loop, $variation_data, $variation ) {

$variation = wc_get_product( $variation );
woocommerce_wp_text_input( array(
'id' => "stock_multiplier{$loop}",
'name' => "stock_multiplier[{$loop}]",
'value' => $variation->get_meta( '_stock_multiplier' ),
'label' => __( 'Inventory reduction per quantity sold', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Enter the quantity multiplier used for reducing stock levels when purchased.', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'min' => '1',
'step' => '1',
),
) );

}
add_action( 'woocommerce_variation_options_pricing', 'ace_add_variation_stock_inventory_multiplier_setting', 50, 3 );

/**
* Save the custom fields.
*
* @param WC_Product $product
*/
function ace_save_custom_stock_reduction_setting( $product ) {

if ( ! empty( $_POST['_stock_multiplier'] ) ) {
$product->update_meta_data( '_stock_multiplier', absint( $_POST['_stock_multiplier'] ) );
}
}
add_action( 'woocommerce_admin_process_product_object', 'ace_save_custom_stock_reduction_setting' );

/**
* Save custom variable fields.
*
* @param int $variation_id
* @param $i
*/
function ace_save_variable_custom_stock_reduction_setting( $variation_id, $i ) {
$variation = wc_get_product( $variation_id );
if ( ! empty( $_POST['stock_multiplier'] ) && ! empty( $_POST['stock_multiplier'][ $i ] ) ) {
$variation->update_meta_data( '_stock_multiplier', absint( $_POST['stock_multiplier'][ $i ] ) );
$variation->save();
}
}
add_action( 'woocommerce_save_product_variation', 'ace_save_variable_custom_stock_reduction_setting', 10, 2 );

减少数量的代码如下:
// For implementation instructions see: https://aceplugins.com/how-to-add-a-code-snippet/

/**
* Reduce with custom stock quantity based on the settings.
*
* @param $quantity
* @param $order
* @param $item
* @return mixed
*/
function ace_custom_stock_reduction( $quantity, $order, $item ) {

/** @var WC_Order_Item_Product $product */
$multiplier = $item->get_product()->get_meta( '_stock_multiplier' );

if ( empty( $multiplier ) && $item->get_product()->is_type( 'variation' ) ) {
$product = wc_get_product( $item->get_product()->get_parent_id() );
$multiplier = $product->get_meta( '_stock_multiplier' );
}

if ( ! empty( $multiplier ) ) {
$quantity = $multiplier * $quantity;
}

return $quantity;
}
add_filter( 'woocommerce_order_item_quantity', 'ace_custom_stock_reduction', 10, 3 );

我试图做的是添加一个“如果”片段来检查数量
add_filter( ‘woocommerce_variation_is_active’, ‘my_jazzy_function’, 10, 2 );

function my_jazzy_function( $active, $variation ) {
// Get Multiplier
$multiplier = $item->get_product()->get_meta( '_stock_multiplier' );

$var_stock_count = $variation->get_stock_quantity();

// if there are 5 or less, disable the variant, could always just set to 0.
if( $var_stock_count <= $multiplier ) {
return false;
}
else {
return true;
}
}
但这不起作用,我认为它只检查变化数量(如果您将变化设置为自己的数量而不是全局数量)。
如何将总库存数量与新添加的设置进行比较 $multiplier ?
任何帮助都会很棒。

最佳答案

  • 将总库存数量与新添加的设置进行比较 $multiplier
  • 注释和解释添加到代码

  • function filter_woocommerce_variation_is_active( $active, $variation ) {    
    // Get multiplier
    $multiplier = get_post_meta( $variation->get_variation_id(), '_stock_multiplier', true );

    // NOT empty
    if ( ! empty( $multiplier ) ) {
    // Get stock quantity
    $var_stock_count = $variation->get_stock_quantity();

    // Stock quantity < multiplier
    if( $var_stock_count < $multiplier ) {
    $active = false;
    }
    }

    return $active;
    }
    add_filter( 'woocommerce_variation_is_active', 'filter_woocommerce_variation_is_active', 10, 2 );

    关于php - 在 WooCommerce 中,随着自定义库存数量的减少,将 "out-of-stock"产品变体变灰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62817436/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com