简体   繁体   中英

Check custom product meta on Woocommerce Stock Quantity Reduction

So I have some functions tucked inside of a function that all function perfectly. Everything works as it should. The ONLY issue I am encountering that is stopping me from completing this script occurs during stock reduction after on checkout.

I have a custom checkbox field that is located on the General Settings page (picture included). I am just trying to create a situation where the function doesn't fire for the products that DO NOT have this checkbox checked.

If I remove the checkbox requirement, all functions perfectly... if I keep it, the only function that doesn't fire is the one that actually reduces the stock quantity based on the conditions in this function.

I have tried everything and still can't get this to work. All help appreciated. (PS. I have excluded the rest of the code which fires fine to avoid over-saturating the code - I can include this if it helps.)

常规设置复选框

// ---------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------- //
// ----------------------------  INVENTORY CONTROL BY WEIGHT  ----------------------------- //
// ---------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------- //


add_action('woocommerce_before_main_content', 'quantity_control', 10, 1);
    function quantity_control( $post_id ) {
        $stock_weight_checkbox = get_post_meta( get_the_id(),'_stock_weight_checkbox', true );

        var_dump($stock_weight_checkbox);

        if ( 'yes' == $stock_weight_checkbox ) {

                // ---------------------------------------------------------------------------------------- //
                // --------------------------  REDUCE WEIGHT BASED ON CUSTOM FIELD  ----------------------- //
                // ---------------------------------------------------------------------------------------- //

                add_filter( 'woocommerce_order_item_quantity', 'filter_order_item_quantity', 10, 4 ); 
                function filter_order_item_quantity( $quantity, $order, $item )  
                {
                    $product   = $item->get_product();
                    $term_name = $product->get_meta( 'custom_field', true );
                    $stock_weight_checkbox = $product->get_meta( '_stock_weight_checkbox', true );


                    // 'pa_weight' attribute value is "15 grams" - keep only the numbers
                    $quantity_grams = preg_replace('/[^0-9.]+/', '', $term_name);

                    // new quantity
                    if( 'yes' == $stock_weight_checkbox && is_numeric ( $quantity_grams ) && $quantity_grams != 0 )
                        $quantity *= $quantity_grams;

                    return $quantity;

                }

I am not sure of this answer, but wouldn't add_filter( 'woocommerce_order_item_quantity', 'filter_order_item_quantity', 10, 4 ); be unable to call filter_order_item_quantity because it is defined in the scope quantity_control ?

If you define the function inside of the function, it will probably mean you don't use it anywhere else, right?

If so, you can just use closures:

...
if ( 'yes' == $stock_weight_checkbox ) {

    add_filter( 'woocommerce_order_item_quantity', function ( $quantity, $order, $item )
    {
        ...
        return $quantity;

    }, 10, 4 );

}
...

Otherwise, defining the function on the same level as quantity_control should work as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM