通过减少 WooCommerce 中的自定义库存数量,使“缺货”产品变体变灰

我现在面临的问题是,它不会检查这些变体是否缺货(例如主库存为 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',

        ),

    ) );


}


慕后森
浏览 130回答 2
2回答

肥皂起泡泡

将总库存数量与新添加的设置进行比较$multiplier注释并添加到代码中的解释function filter_woocommerce_variation_is_active( $active, $variation ) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // Get multiplier&nbsp; &nbsp; $multiplier = get_post_meta( $variation->get_variation_id(), '_stock_multiplier', true );&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // NOT empty&nbsp; &nbsp; if ( ! empty( $multiplier ) ) {&nbsp; &nbsp; &nbsp; &nbsp; // Get stock quantity&nbsp; &nbsp; &nbsp; &nbsp; $var_stock_count = $variation->get_stock_quantity();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Stock quantity < multiplier&nbsp; &nbsp; &nbsp; &nbsp; if( $var_stock_count < $multiplier ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $active = false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return $active;}add_filter( 'woocommerce_variation_is_active', 'filter_woocommerce_variation_is_active', 10, 2 );

慕虎7371278

它不起作用,因为:$item您的代码中未定义变量。您的自定义字段在父变量产品中定义。所以你需要更换:$multiplier&nbsp;=&nbsp;$item->get_product()->get_meta(&nbsp;'_stock_multiplier'&nbsp;);通过以下方式(从父变量产品获取数据):$multiplier&nbsp;=&nbsp;get_post_meta(&nbsp;$variation->get_parent_id(),&nbsp;'_stock_multiplier',&nbsp;true&nbsp;);所以在你的代码中:add_filter( 'woocommerce_variation_is_active', 'my_jazzy_function', 10, 2 );&nbsp;function my_jazzy_function( $active, $variation ) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // Get multiplier&nbsp; &nbsp; if( $multiplier = get_post_meta( $variation->get_parent_id(), '_stock_multiplier', true ) {&nbsp; &nbsp; &nbsp; &nbsp; // Get stock quantity&nbsp; &nbsp; &nbsp; &nbsp; $var_stock_count = (int) $variation->get_stock_quantity();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // if there are 5 or less, disable the variant, could always just set to 0&nbsp; &nbsp; &nbsp; &nbsp; return $var_stock_count <= $multiplier ? false : $active;&nbsp; &nbsp; }&nbsp; &nbsp; return&nbsp; $active;}现在应该可以了。
打开App,查看更多内容
随时随地看视频慕课网APP