按属性值减少 WooCommerce 项目库存 [英] Reduce WooCommerce Item Inventory By Attribute Value

查看:56
本文介绍了按属性值减少 WooCommerce 项目库存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了 Woocommerce可变产品",其中唯一的变化是尺寸"属性:15 克、100 克、250 克.我想要做的是使用该变化量传递给 Woo wc-stock-functions,这样当购买产品变化15 克"时,整体库存减少 15,而不是 1.

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.

在 Woo 里面,有文件 wc-stock-functions (http://hookr.io/plugins/woocommerce/3.0.6/files/includes-wc-stock-functions/) - 这甚至提供了一个过滤器,woocommerce_order_item_quantity.我想用它来将库存数量乘以克数,并以此方式以克为单位减少库存.

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.

我正在尝试:

// 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.

推荐答案

第一个错误在 $item->get_attribute('pa_size'); as $itemWC_Order_Item_Product 对象的实例,WC_Order_Item_Product 类不存在 get_attribute() 方法.

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.

相反,您需要使用 WC_Order_Item_Product 类中的 get_product() 方法获取 WC_Product 对象的实例...

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

所以你的代码应该是:

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;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

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

注意:这个挂钩函数将根据新返回的增加数量值减少库存数量(在这种情况下,实际数量乘以 15)>

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)

这篇关于按属性值减少 WooCommerce 项目库存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆