最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Reduce Stock Based on Custom Variation Field Rather than Attribute (Working Script)

programmeradmin3浏览0评论

I currently have a script that works perfectly. The issue is, I want to use the value in a custom field I have created for my variations rather than an attribute. See code:

// reduce stock based on 'pa_weight' attribute
add_filter( 'woocommerce_order_item_quantity', 'filter_order_item_quantity', 10, 3 ); 
function filter_order_item_quantity( $quantity, $order, $item )  
{
    $product   = $item->get_product();
    $term_name = $product->get_attribute('pa_weight');

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

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

    return $quantity;
}

I have tried replacing

$term_name = $product->get_attribute('pa_weight');

With $term_name = $product->get_post_meta('custom_field', true);

And similar things, but keep getting undefined method errors and the like. It is as though the value isn't being properly called.

Any idea? TIA.

发布评论

评论列表(0)

  1. 暂无评论