亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過減少 WooCommerce 中的自定義庫存數量,使“缺貨”產品變體變灰

通過減少 WooCommerce 中的自定義庫存數量,使“缺貨”產品變體變灰

PHP
慕后森 2023-07-21 16:10:40
我現在面臨的問題是,它不會檢查這些變體是否缺貨(例如主庫存為 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',? ? ? ? ),? ? ) );}
查看完整描述

2 回答

?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

將總庫存數量與新添加的設置進行比較$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 );



查看完整回答
反對 回復 2023-07-21
?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

它不起作用,因為:

  • $item您的代碼中未定義變量。

  • 您的自定義字段在父變量產品中定義。

所以你需要更換:

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

通過以下方式(從父變量產品獲取數據)

$multiplier = get_post_meta( $variation->get_parent_id(), '_stock_multiplier', true );

所以在你的代碼中:

add_filter( 'woocommerce_variation_is_active', 'my_jazzy_function', 10, 2 ); 

function my_jazzy_function( $active, $variation ) {    

    // Get multiplier

    if( $multiplier = get_post_meta( $variation->get_parent_id(), '_stock_multiplier', true ) {

        // Get stock quantity

        $var_stock_count = (int) $variation->get_stock_quantity();

    

        // if there are 5 or less, disable the variant, could always just set to 0

        return $var_stock_count <= $multiplier ? false : $active;

    }

    return  $active;

}

現在應該可以了。


查看完整回答
反對 回復 2023-07-21
  • 2 回答
  • 0 關注
  • 157 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號