从Woocommerce中的Toolset CRED表单数据覆盖购物车商品标题和价格

时间:2018-09-24 19:58:31

标签: php wordpress woocommerce cart price

我正在尝试覆盖WooCommerce购物车中某产品的价格。 问题是我正在使用Divi,其中包括ajax购物车更新。因此,当我重新加载结帐页面时,我可以看到1-2秒(在ajax加载期间)被覆盖的更改,此后,购物车详细信息再次被默认值覆盖。我尝试了一切,尝试了不同的异常处理,延迟,优先级更改,但没有任何效果。

这是我的初始代码:

add_filter( 'woocommerce_calculate_totals', 'custom_cart_items_prices', 1000, 1 )
function custom_cart_items_prices( $cart_object ) {

$title = $_GET['form-title'];
$money = $_GET['money'];

if ($title && $money) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

        // Iterating through cart items
        foreach ( $cart_object->get_cart() as $cart_item ) {

        // Get an instance of the WC_Product object
        $wc_product = $cart_item['data'];

        // Get the product name (WooCommerce versions 2.5.x to 3+)
        $original_name = method_exists( $wc_product, 'get_name' ) ? $wc_product->get_name() : $wc_product->post->post_title;

        // SET THE NEW NAME
        $new_name = $title;

        // Set the new name (WooCommerce versions 2.5.x to 3+)
        if( method_exists( $wc_product, 'set_name' ) )
            $wc_product->set_name( $new_name );
        else
            $wc_product->post->post_title = $new_name;
        }

    // Updated cart item price
    $cart_item['data']->set_price( $money ); 

}}

加载过程中的图片(一切看起来都很不错):

enter image description here

加载完成后:

enter image description here


更新-我还尝试了另一种方法,因为我使用的是由CRED Toolset和Toolset Woocommerce插件制作的自定义表单,客户可在其中创建具有自定义标题和价格的产品,并将其添加到购物车中默认的现有产品(并重定向到结帐页面)。我正在尝试用提交的表单数据替换购物车商品的标题和价格。

这是我的代码:

// Change the product ID
add_filter('cred_commerce_add_product_to_cart', function( $product_id, $form_id, $post_id ) {
    error_log($form_id);
    if( $form_id == 55 ) {
        if (!session_id()) {
            session_start();
        }
        $_SESSION['target_post_id'] = $post_id;
        if(isset($_POST['product-id'])){
            $product_id = $_POST['product-id'];
            $_SESSION['target_post_id'] = $post_id;
        }
    }
    return $product_id;
}, 20, 3);

// change the price in cart
add_action('woocommerce_before_calculate_totals', function(){
    session_start();
    if($form_id != 55 && !isset($_SESSION['target_post_id']))return;
    global $woocommerce;

    $post_id = $_SESSION['target_post_id']; 
    $price = 0;
    foreach ( $woocommerce->cart->get_cart() as $key => $cart_item ) {

        $product_id = $cart_item['data']->get_id();
        $adult_price = get_post_meta($product_id, 'wpcf-prezzo-1', true);
        $adult_num = get_post_meta($post_id, 'wpcf-adult-number', true);
        $child_price = get_post_meta($product_id, 'wpcf-prezzo-2', true);
        $child_num = get_post_meta($post_id, 'wpcf-children-number', true);

        $price = $adult_price * $adult_num + $child_price * $child_num; 
        $cart_item['data']->set_price($price); 
    }

}, 999);

但是它也不起作用。

如何获取提交的表单数据(自定义标题和价格)并覆盖Woocommerce中的购物车商品标题和价格?

3 个答案:

答案 0 :(得分:1)

1 woocommerce_add_cart_item_data

add_filter( 'woocommerce_add_cart_item_data', function($cart_item_data, $product_id, $variation_id ){
    if ( empty( $_GET['form-title'] ) ||  empty( $_GET['money'] ) ) {
        return;
    }
    $title = $_GET['form-title'];
    $money = $_GET['money'];
    $cart_item_data['form-title']   = $title  ;
    $cart_item_data['money']        = $money  ;
    return $cart_item_data;
} ,100,4);

2 woocommerce_before_calculate_totals

add_action( 'woocommerce_before_calculate_totals', 'update_custom_price', 10, 1 );
function update_custom_price( $cart_object ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }
    foreach ( $cart_object->cart_contents as $cart_item_key => $cart_item ) {
        if(isset($cart_item["money"]) ){
            $money = $cart_item["money"] ;
            $cart_item['data']->set_price( $money);
        }
        if(isset($cart_item["form-title"]) ){
            $title = $cart_item["form-title"] ;
        }
        $wc_product = $cart_item['data'];

        // Get the product name (WooCommerce versions 2.5.x to 3+)
        $original_name = method_exists( $wc_product, 'get_name' ) ? $wc_product->get_name() : $wc_product->post->post_title;

        // SET THE NEW NAME
        $new_name = $title;

        // Set the new name (WooCommerce versions 2.5.x to 3+)
        if( method_exists( $wc_product, 'set_name' ) )
            $wc_product->set_name( $new_name );
        else
            $wc_product->post->post_title = $new_name;
        }
    }
    return $cart_object ;
}

希望这会有所帮助。

答案 1 :(得分:1)

在使用带有自定义表单的工具集插件时,请尝试以下操作,您将在其中使用WC_Sessions存储产品ID并更改价格:

add_filter( 'cred_commerce_add_product_to_cart', 'cred_commerce_add_product_to_cart', 20, 3 );
function cred_commerce_add_product_to_cart( $product_id, $form_id, $post_id ) {
    if( $form_id == 55 && $post_id > 0 ) {
        WC()->session->set( 'cp_id', $post_id );
    }
    return $product_id;
}

add_filter( 'woocommerce_add_cart_item_data', 'filter_add_cart_item_data', 30, 4 );
function filter_add_cart_item_data( $cart_item_data, $product_id, $variation_id ){
    if ( WC()->session->get('cp_id') ) {
        $cp_id   = WC()->session->get('cp_id');
        $product = wc_get_product($cp_id);
        $cart_item_data['cp_price'] = $product->get_regular_price();
        $cart_item_data['cp_title'] = $product->get_title();
    }
    return $cart_item_data;
}

add_action( 'woocommerce_before_calculate_totals', 'custom_cart_items_price', 30, 1 );
function custom_cart_items_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Iterating through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        if( isset($cat_item['cp_price']) && isset($cat_item['cp_title']) ){
            $cart_item['data']->set_price( $cat_item['cp_price'] ); 
            $cart_item['data']->set_name( $cat_item['cp_title'] );
        }
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中。应该可以。


一些related answers

答案 2 :(得分:-1)

停用所有插件,瞧! 您会惊奇地了解,这是一个原因。

一个接一个地启用它们,会让您知道哪个冲突。 并且..请考虑使用WordPress堆栈交换解决WordPress特定的问题。