WooCommerce可变产品的自定义库存数量减少

问题描述 投票:-1回答:1

就像跟随此tutorial的许多其他用户一样,我遇到了以下问题

如果我以后更改订单的状态以完成,就会发生这种情况,则将自定义库存加上调整后的数量与原始数量之间的差额重新添加。

我该如何预防?

ps.s。几个月前,我几次与教程作者联系,很遗憾,没有任何回复

本教程中的代码

/**
 * 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 );

/**
 * 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 );

不幸的是,我第一次尝试,没有理想的结果

/**
 * Reduce with custom stock quantity based on the settings.
 *
 * @param $quantity
 * @param $order
 * @param $item
 * @return $quantity
 */
function dvpi_custom_stock_reduction( $quantity, $order, $item ) {
    /** @var WC_Order_Item_Product $product */

    $multiplier = $item->get_product()->get_meta( '_stock_multiplier' );

    if ( ! empty( $multiplier ) ) {
        // Get product (parent) id
        $product_id = $item->get_product_id();

        // Get product object
        $product = wc_get_product( $product_id );

        // Get product stock
        $product_stock = $product->get_stock_quantity();

        // Calculate the reduce
        $reduce = $multiplier * $quantity;

        // Update stock
        wc_update_product_stock( $product, $product_stock - $reduce );

        // 0 because we do not use the standard behavior
        $quantity = 0;
    }

    return $quantity;
}
add_filter( 'woocommerce_order_item_quantity', 'dvpi_custom_stock_reduction', 10, 3 );
php wordpress woocommerce hook-woocommerce orders
1个回答
0
投票

解决方案是添加一个额外的钩子,即woocommerce_prevent_adjust_line_item_product_stock

以这种方式,我们检查woocommerce_prevent_adjust_line_item_product_stock的条件是否得到满足,并防止对其进行调整。

$multiplier
© www.soinside.com 2019 - 2024. All rights reserved.