使用this code通过属性值减少库存中的库存。它可以减少库存,但是当您有100克的库存,而客户购买了120克时。它不会显示缺货。
因此创建了以下代码:
<?php
add_filter( 'woocommerce_order_item_quantity', 'filter_order_item_quantity', 10, 3 );
function filter_order_item_quantity( $quantity, $order, $item )
{
$product = $item->get_product();
$stock_quantity = $product->get_stock_quantity();
$term_name = $product->get_attribute('pa_weight');
// 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;
if($quantity > $stock_quantity) {
return $quantity;
} else {
echo '<p>Out of Stock</p>';
}
}
但是,它不起作用...
是否可以通过$数量检查库存?
if($quantity > $stock_quantity) {
return $quantity;
} else {
echo '<p>Out of Stock</p>';
}
编辑:
试图将库存减少到wc_reduce_stock_levels
,但没有成功
add_filter( 'wc_reduce_stock_levels', 'filter_reduce_stock_levels', 5, 1);
function filter_reduce_stock_levels( $order_id ) {
if ( is_a( $order_id, 'WC_Order' ) ) {
$order = $order_id;
$order_id = $order->get_id();
} else {
$order = wc_get_order( $order_id );
}
if ( 'yes' === get_option( 'woocommerce_manage_stock' ) && $order && apply_filters( 'woocommerce_can_reduce_order_stock', true, $order ) && sizeof( $order->get_items() ) > 0 ) {
foreach ( $order->get_items() as $item ) {
if ( $item->is_type( 'line_item' ) && ( $product = $item->get_product() ) && $product->managing_stock() ) {
$term_name = $product->get_attribute('pa_weight');
$qty = apply_filters( 'woocommerce_order_item_quantity', $item->get_quantity(), $order, $item );
$item_name = $product->get_formatted_name();
$new_stock = wc_update_product_stock( $product, $qty, 'decrease' );
$quantity_grams = preg_replace('/[^0-9.]+/', '', $term_name);
$qty *= $quantity_grams;
if ( ! is_wp_error( $new_stock ) ) {
/** translators: 1: item name 2: old stock quantity 3: new stock quantity */
$order->add_order_note( sprintf( __( '%1$s stock reduced from %2$s to %3$s.', 'woocommerce' ), $item_name, $new_stock + $qty, $new_stock ) );
// Get the latest product data.
$product = wc_get_product( $product->get_id() );
if ( '' !== get_option( 'woocommerce_notify_no_stock_amount' ) && $new_stock <= get_option( 'woocommerce_notify_no_stock_amount' ) ) {
do_action( 'woocommerce_no_stock', $product );
} elseif ( '' !== get_option( 'woocommerce_notify_low_stock_amount' ) && $new_stock <= get_option( 'woocommerce_notify_low_stock_amount' ) ) {
do_action( 'woocommerce_low_stock', $product );
}
if ( $new_stock < 0 ) {
do_action( 'woocommerce_product_on_backorder', array( 'product' => $product, 'order_id' => $order_id, 'quantity' => $qty ) );
}
}
}
}
// ensure stock is marked as "reduced" in case payment complete or other stock actions are called
$order->get_data_store()->set_stock_reduced( $order_id, true );
do_action( 'woocommerce_reduce_order_stock', $order );
}
}
答案 0 :(得分:1)
删除所有旧代码,并将其添加到主题的“ functions.php”中。
// 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;
}
// check out of stock using 'pa_weight' attribute
add_filter( 'woocommerce_add_to_cart_validation', 'woocommerce_validate_attribute_weight' );
function woocommerce_validate_attribute_weight()
{
// get product id
if (isset($_REQUEST["add-to-cart"])) {
$productid = (int)$_REQUEST["add-to-cart"];
} else {
$productid = null;
}
// get quantity
if (isset($_REQUEST["quantity"])) {
$quantity = (int)$_REQUEST["quantity"];
} else {
$quantity = 1;
}
// get weight of selected attribute
if (isset($_REQUEST["attribute_pa_weight"])) {
$weight = preg_replace('/[^0-9.]+/', '', $_REQUEST["attribute_pa_weight"]);
} else {
$weight = null;
}
// comparing stock
if($productid && $weight)
{
$product = wc_get_product($productid);
$productstock = (int)$product->get_stock_quantity();
if(($weight * $quantity) > $productstock)
{
wc_add_notice( sprintf( 'You cannot add that amount of "%1$s" to the cart because there is not enough stock (%2$s remaining).', $product->get_title(), $productstock ), 'error' );
return;
}
}
return true;
}
第一个功能几乎与您的功能相同,并且您无法使用woocommerce_order_item_quantity
显示缺货消息,因为它只有在成功确认订单后才会执行。
您需要在将产品添加到购物车时验证新库存,这可以通过使用第二个功能(使用woocommerce_add_to_cart_validation
)来实现。
答案 1 :(得分:0)
我从未使用过woo commerce,但我发现您正在尝试从例如15 grams
。我不确定,但很可能您也应该为$stock_quantity
做同样的事情。最终代码应该是这样的。而且我还注意到您正在检查,如果要求的数量比库存数量要好,则将其退回是不合逻辑的。应该是恶毒对。我的意思是要求的数量应小于或等于库存数量。
<?php
add_filter( 'woocommerce_order_item_quantity', 'filter_order_item_quantity', 10, 3 );
function filter_order_item_quantity( $quantity, $order, $item )
{
$product = $item->get_product();
$stock_quantity = $product->get_stock_quantity();
$term_name = $product->get_attribute('pa_weight');
// The 'pa_size' attribute value is "15 grams" And we keep only the numbers
$quantity_grams = preg_replace('/[^0-9.]+/', '', $term_name);
$stock_quantity = (int)preg_replace('/[^0-9.]+/', '', $stock_quantity);
// Calculated new quantity
if( is_numeric ( $quantity_grams ) && $quantity_grams != 0 )
$quantity *= $quantity_grams;
if($quantity <= $stock_quantity) {
return $quantity;
} else {
return '<p>Out of Stock</p>';
}
}