抱歉,伙计们,不得不再次发布此消息,因为网站不允许我回复评论(新帐户)。
我一直在编写一些代码,以根据自定义字段的数值减少可变产品的数量。
这很好,但是我已经实现了一个复选框,基本上可以打开/关闭每个产品上的功能。此自定义复选框也可以正常工作,出现在“Product General Settings”(产品常规设置)中,并存储数据良好-我已通过产品页面上的VAR\\u DUMP()确认了这一点,它准确地转储了yes-(长度=3)。
虽然数据正在存储,但我似乎无法将其用作条件,如果(\'是\'==$stock\\u weight\\u复选框)数量的正确减少不起作用,则该条件何时到位。
我似乎无法在此过程中使用VAR\\u DUMP(),因为它发生在签出确认过程中,所以我看不到此时是否显示了任何值。
我的假设如下:我不正确地调用此区域中的post\\u meta(checkout)。也许这个电话在产品页面上有效,但我遗漏了一些东西,或者我处理电话的方式不正确。
这是一个可变产品,在签出期间正在常规设置区域中查找自定义字段,此时该字段不可用?(猜测)
代码如下:
// reduce stock based on \'custom_field\'
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_meta( \'custom_field\', true );
$stock_weight_checkbox = $product->get_meta( \'_stock_weight_checkbox\', true );
// \'pa_weight\' attribute value is "15 grams" - keep only the numbers
$quantity_grams = preg_replace(\'/[^0-9.]+/\', \'\', $term_name);
// new quantity
if( \'yes\' == $stock_weight_checkbox && is_numeric ( $quantity_grams ) && $quantity_grams != 0 )
$quantity *= $quantity_grams;
return $quantity;
}
function validate_attribute_weight( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
// Get custom field
$weight = get_post_meta( $variation_id, \'custom_field\', true );
$stock_weight_checkbox = get_post_meta( get_the_id(),\'_stock_weight_checkbox\', true );
if ( \'yes\' == $stock_weight_checkbox && ! empty( $weight ) ) {
// Get product object
$product = wc_get_product( $product_id );
// Get current product stock
$product_stock = $product->get_stock_quantity();
// ( Weight * quantity ) > product stock
if( ( ( $weight * $quantity ) > $product_stock ) ) {
wc_add_notice( sprintf( \'Sorry, you cannot add <strong>\' . $weight .\'</strong> of <strong>%1$s</strong> to the cart because there are only <strong>%2$sg</strong> left in our inventory. Please choose a lesser amount. We hope to have more in stock shortly.\', $product->get_name(), $product_stock ), \'error\' );
$passed = false;
}
}
return $passed;
}
add_filter( \'woocommerce_add_to_cart_validation\', \'validate_attribute_weight\', 10, 5 );
有人注意到什么不对吗?或者至少让我知道如何在签出过程中查看VAR\\u DUMP()?(我习惯于在页面的某个部分(即:产品页面)上吐痰)
SO网友:simongcc
您的代码中有一些要点可能需要实际检查。
下面是您可能有兴趣更新的内容,以及用于var\\u转储进程的方法。
function validate_attribute_weight( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
// Get custom field
// 1. I am not sure if you directly get the post meta using WP builtin return expected result or not since I can see you use $product->get_meta() before
// $weight = get_post_meta( $variation_id, \'custom_field\', true );
// $stock_weight_checkbox = get_post_meta( get_the_id(),\'_stock_weight_checkbox\', true );
// you may try to change to
// current product from the filter, it is better than get_the_id() because if filter provides to you, more directly and bullet-proof
$product = wc_get_product( $product_id );
// with $product fetched, you may use the same code previously which ensure consistency
$stock_weight_checkbox = $product->get_meta( \'_stock_weight_checkbox\', true );
if ( \'yes\' == $stock_weight_checkbox && ! empty( $weight ) ) {
// Get product object
// $product = wc_get_product( $product_id ); // since already get, no need to do so
// Get current product stock
$product_stock = $product->get_stock_quantity();
// ( Weight * quantity ) > product stock
if( ( ( $weight * $quantity ) > $product_stock ) ) {
wc_add_notice( sprintf( \'Sorry, you cannot add <strong>\' . $weight .\'</strong> of <strong>%1$s</strong> to the cart because there are only <strong>%2$sg</strong> left in our inventory. Please choose a lesser amount. We hope to have more in stock shortly.\', $product->get_name(), $product_stock ), \'error\' );
$passed = false;
}
}
// what you concern is the testing logic, if you want to intercept, you may add exit()
// var_dump() here
exit();
return $passed;
}
add_filter( \'woocommerce_add_to_cart_validation\', \'validate_attribute_weight\', 10, 5 );
因为WordPress ajax过程类似于:
单击add to cart(添加到购物车)它通过JS ajax通过URL传递数据,然后返回一些内容,它将向php启动操作以进行处理<;==这是您可以截获的点,当php退出此处时,过程中的任何内容都在中间,因此您可以看到var\\u dump()
进程完成后,php返回结果(false/true)并将一些内容返回到页面关于拦截进程的其他注意事项:当您使用var\\u dump进行跟踪时,最好也使用var\\u dump($\\u请求)。如果您有选项,那么需要确认它是从表单传递到php的。这有助于跟踪和确认。如果$\\u请求中不存在该参数,则很可能该参数不在提交的表单中。