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

问题描述 投票:0回答:3

我正在尝试覆盖 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 工具集和工具集 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 中的购物车商品标题和价格?

php wordpress woocommerce cart product-price
3个回答
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 文件中。应该有效。


一些相关答案


0
投票

停用所有插件,瞧! 你会惊讶地发现这是一个原因。

将它们一一启用,会让您知道哪一个是冲突的。 并且..请考虑使用 WordPress Stack Exchange 来解决 WordPress 特定问题。


0
投票

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

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.