使用会话woocommerce传递产品的自定义属性

时间:2017-02-07 10:59:36

标签: php wordpress session woocommerce

我正在尝试使用会话将产品'advance-payment' and 'post_title'的自定义属性获取到另一个页面。 post_title已通过,但自定义属性advance-payment未通过。 advance-payment打印在simple.php上,但不打印在custom_form.php上。

simple.php

<?php
session_start();
$_SESSION['post_title_form'] = $product->post->post_title;
$ca_advance_value = $product->get_attribute('advance-payment');
$_SESSION['post_advance_form']=$ca_advance_value;
//attribute is echoed here
echo "Test " . $ca_advance_value;

custom_form.php

<?php
session_start();
/* Template Name: CustomFormPage */ 
//is displayed
echo "TEST 1: " .$_SESSION['post_title_form'];
//not displayed
echo "TEST 2: ".$_SESSION['post_advance_form'];

1 个答案:

答案 0 :(得分:0)

您可以通过以下挂钩添加自定义详细信息以获取商务会话:

add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_custom_data_vase', 10, 2 );
    function add_cart_item_custom_data_vase( $cart_item_meta, $product_id ) {
        global $woocommerce;            
        $cart_item_meta['post_title'] = 'post title';
        $cart_item_meta['post_advance_form'] = 'Advance Payment';
        return $cart_item_meta; 
    }

通过以下代码,您可以从custom_form.php上的购物车会话中获取购物车商品:

global $woocommerce;
    $items = $woocommerce->cart->get_cart();
    foreach($items as $item => $values) {           
        $post_title = $values['post_title'];
        $post_advance_form = $values['post_advance_form'];
    }