![](/img/trans.png)
[英]Greying out “out-of-stock” product variations with custom stock quantity reduction in WooCommerce
[英]Check custom product meta on Woocommerce Stock Quantity Reduction
所以我有一些功能隱藏在 function 中,所有 function 都完美。 一切正常。 我遇到的唯一阻止我完成此腳本的問題發生在結帳后庫存減少期間。
我有一個位於常規設置頁面(包括圖片)上的自定義復選框字段。 我只是想創造一種情況,即 function 不會針對未選中此復選框的產品觸發。
如果我刪除復選框要求,所有功能都完美......如果我保留它,唯一不觸發的 function 是根據這個 function 中的條件實際減少庫存數量的那個。
我已經嘗試了一切,但仍然無法正常工作。 所有幫助表示贊賞。 (PS。我已經排除了代碼的 rest,它可以很好地避免代碼過度飽和 - 如果它有幫助,我可以包括它。)
// ---------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------- //
// ---------------------------- 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;
}
我不確定這個答案,但不會add_filter( 'woocommerce_order_item_quantity', 'filter_order_item_quantity', 10, 4 );
無法調用filter_order_item_quantity
因為它是在 scope quantity_control
中定義的?
如果您在 function 內部定義 function,這可能意味着您不會在其他任何地方使用它,對吧?
如果是這樣,您可以只使用閉包:
...
if ( 'yes' == $stock_weight_checkbox ) {
add_filter( 'woocommerce_order_item_quantity', function ( $quantity, $order, $item )
{
...
return $quantity;
}, 10, 4 );
}
...
否則,在與quantity_control
相同的級別上定義 function 也應該有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.