douye2488 2018-04-05 19:47
浏览 120
已采纳

按属性值减少WooCommerce项目库存

I have a setup with Woocommerce "Variable Products" where the only variation is 'size' attribute: 15 grams, 100 grams, 250 grams. What I want to do is use that variation amount to pass to the Woo wc-stock-functions, so that when a product variation '15 grams' is purchased, the overall stock goes down by 15, not 1.

Inside Woo, there is file wc-stock-functions (http://hookr.io/plugins/woocommerce/3.0.6/files/includes-wc-stock-functions/) - and this even gives a filter, woocommerce_order_item_quantity. I want to use this to multiply the inventory number by the # of grams, and to reduce inventory this way by grams.

I'm trying this:

// define the woocommerce_order_item_quantity callback 
function filter_woocommerce_order_item_quantity( $item_get_quantity, $order, 
$item ) { 
$original_quantity = $item_get_quantity; 
$item_quantity_grams = $item->get_attribute('pa_size');
// attribute value is "15 grams" - so remove all but the numerals
$item_quantity_grams = preg_replace('/[^0-9.]+/', '', $item_quantity_grams);
// multiply for new quantity
$item_get_quantity = ($item_quantity_grams * $original_quantity);

return $item_get_quantity; 
}; 

// add the filter 
add_filter( 'woocommerce_order_item_quantity', 
'filter_woocommerce_order_item_quantity', 10, 3 ); 

but am getting internal server error as a response right now.

Does anyone have an idea of what I'm doing wrong with the above code? Thanks for any help.

  • 写回答

1条回答 默认 最新

  • dongqindu8110 2018-04-05 20:49
    关注

    First error is in $item->get_attribute('pa_size'); as $item is an instance of WC_Order_Item_Product object and get_attribute() method doesn't exist for WC_Order_Item_Product Class.

    Instead you need to get the an instance of the WC_Product object using get_product() method from WC_Order_Item_Product Class…

    So your code should be:

    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_size');
    
        // The 'pa_size' attribute value is "15 grams" And we keep only the numbers
        $quantity_grams = preg_replace('/[^0-9.]+/', '', $term_name);
    
        // Calculated new quantity
        if( is_numeric ( $quantity_grams ) && $quantity_grams != 0 )
            $quantity *= $quantity_grams;
    
        return $quantity;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    Note: This hooked function is going to reduce the stock quantity based on that new returned increased quantity value (in this case the real quantity multiplied by 15)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应