我尝试动态更改产品的价格。我已经安装了插件@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
........
...............
fSearch = new FragmentSearch();
froute = new FragmentRoute();
fHelp = new FragmentHelp();
fSettings = new FragmentSettings();
fSearchRoute = new FragmentSearchRoute();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
int selectedPosition = sharedPreferences.getInt("SELECTED_POSITION", 1);
switch (selectedPosition) {
case 1:
// FragmentSearch
getFragmentManager().beginTransaction().replace(R.id.container, fSearch).commit();
break;
case 2:
// FragmentRoute
getFragmentManager().beginTransaction().replace(R.id.container, froute).commit();
break;
case 3:
// FragmentHelp
getFragmentManager().beginTransaction().replace(R.id.container, fHelp).commit();
break;
default:
getFragmentManager().beginTransaction().replace(R.id.container, fSearch).commit();
break;
}
}
,我添加了一些字段来插入价格。
这是我改变价格的代码:
WC_fields_Factory
[...]
add_filter( 'woocommerce_get_cart_item_from_session', array( $this, 'get_cart_item_from_session' ), 10, 2 );
add_filter( 'woocommerce_add_cart_item', array( $this, 'simone_add_cart_item' ), 10, 1 );
add_action( 'woocommerce_before_calculate_totals', array( $this, 'simone_update_price'), 99 );
并且它不起作用...... 现在,当我尝试将成本增加到产品的正常价格时,就会发生这种情况:
private function _getPrice($product_id, $price) {
$all_fields = apply_filters( 'wcff/load/all_fields', $product_id, 'wccpf' );
$prezzoCalcolato = 0;
foreach ( $all_fields as $title => $fields )
{
foreach ( $fields as $field )
{
if( isset($field['field_valore']) && $field['field_grouprice'] == 'price-var' )
{
$quantita = isset($_REQUEST[ $field["name"] ]) ? intval($_REQUEST[ $field["name"] ]) : 0;
$prezzoCalcolato = intval($quantita) * intval($field['field_valore']);
} elseif( isset($field['field_valore']) && $field['field_grouprice'] == 'price-fix' ) {
$prezzoCalcolato = intval($field['field_valore']) ;
}
}
}
$prezzoCalcolato += $price;
return $prezzoCalcolato;
}
public function simone_add_cart_item( $cart_item, $cart_item_key ) {
$price = $this->_getPrice($cart_item['product_id'], $cart_item['data']->get_price());
$cart_item['data']->set_price($price);
return $cart_item;
}
public function get_cart_item_from_session( $cart_item, $values ) {
$price = $this->_getPrice($cart_item['product_id'], $cart_item['data']->get_price());
$cart_item['data']->set_price($price);
return $cart_item;
}
public function simone_update_price($cart) {
foreach($cart->get_cart() as $cart_key_item => $cart_item) {
$price = $this->_getPrice($cart_item['product_id'], $cart_item['data']->get_price());
$cart_item['data']->set_price($price);
}
}
工作正常
否则我设定价格如下:
$cart_item['data']->set_price(57);
它没有工作-.-我试图像这样设置floatval或intval:
$prezzo = 57;
$cart_item['data']->set_price($prezzo);
没有什么不行的:(
在线解决方案是:
$cart_item['data']->set_price(intval($prezzo));
或
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
$custom_price = 10; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
} }
他们没有工作!!!!