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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊