这是我在stackoverflow上的第一个问题,所以请原谅我,以防我破坏任何社区约定等。
我的问题在于Woocommerce,特别是我想添加处理费,但作为订单项,因为PayPal Advance通过'$ woocommerce-> cart-> add_fee'命令正确处理额外费用。
我在这里发现了很多线索和其他有类似问题的人,但这些解决方案都没有帮助我,因为它们似乎已经过时,或者我可能会误解。
这是我目前正在组装代码的其中一个主题:
WooCommerce: Add product to cart with price override?
我们的代码的目的是:
以下是我解决此问题的最新尝试:
// Add cart functionality to add additional fee to reach minimum order
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$taxes = array_sum($woocommerce->cart->taxes);
$toasty = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total + $taxes );
$surcharge = 34.95 - ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total + $taxes );
if ($surcharge > 0 ) {
$custom_price = $surcharge; // This will be your custom price
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $value) {
if ( $value['product_id'] !== 36324 && $first) {
$woocommerce->cart->add_to_cart(36324);
$first = false;
}
if ( $value['product_id'] == 36324 && $second) {
$woocommerce->cart->remove_cart_item($item);
$value['data']->price = $custom_price;
$second = false;
}
if ( $value['product_id'] !== 36324 && $third) {
$woocommerce->cart->add_to_cart(36324);
$third = false;
}
}
}
}
目前它出现了这个wordpress错误: 致命错误:第1680行/wp-includes/query.php中允许的内存大小为268435456字节(试图分配130968字节)
我认为应该是多个'if'子句,而不是调用不同的函数。
我们想要删除然后重新添加它的原因是因为它改变了价格,但它并未在购物车总数中确认。因此,例如处理费可能会在购物车中说“$ 21.00”,但实际上它仍然只收取1.00美元。
非常感谢您提前,如果您需要更多信息来帮助我 - 我很乐意提供:)
答案 0 :(得分:0)
最后自己组装了代码,这里适合所有可能需要它的人:
/*David is adding the code so that a minimum order of 25 dollars is in the cart. || All your processing fee are belong to us now*/
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
add_action( 'woocommerce_checkout_before_order_review' , 'wc_minimum_order_amount');
function wc_minimum_order_amount() {
global $woocommerce;
$prod_to_check = 36324; //Processing Fee line item ID
$found = false; // Default value
/* Check for the Processing Fee Line Item in Woocommerce Cart */
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
if ($cart_item['product_id'] == $prod_to_check || $cart_item['variation_id'] == $prod_to_check) {
$found = true;
}
}
/* If the Processing Fee Line Item is in the cart, remove it. This is required to update the cost of the line item. Or keep it off, in case the minimum order amount has been reached.*/
if( $found == true) {
$prod_to_remove = 36324; // Set the product ID to remove
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
if ($cart_item['product_id'] == $prod_to_remove || $cart_item['variation_id'] == $prod_to_remove) { //remove single product
$woocommerce->cart->remove_cart_item($cart_item_key);
}
}
}
/* Calculate the difference up to the minimum order amount required, then change Processing Fee line item price accordingly. */
if ( WC()->cart->total < 34.95) {
$minimum = 34.95; // Set this variable to specify a minimum order value
$surcharge_product = ($minimum - WC()->cart->total);
update_post_meta( '36324', '_regular_price', str_replace("$","", $surcharge_product) );
update_post_meta( '36324', '_price', str_replace("$","", $surcharge_product) );
}
/* If the cart total is below minimum order amount, add the Processing Fee Line item with the new price. */
if ( WC()->cart->total < 34.95) {
if( is_cart() || is_checkout() ) {
$woocommerce->cart->add_to_cart(36324);
wc_add_notice(
sprintf( 'A Processing Fee has been added to make your order total match our minimum order of 34.95.'
), 'error'
);
}
}
}