替换对象数组中的值

时间:2012-03-16 21:13:47

标签: php arrays object

我有以下数组:::

WC_Cart Object (
  [cart_contents] => Array (
    [d6f5bb0bbc] => Array (
      [product_id] => 14
      [variation_id] => 325
      [variation] => Array (
        [Your Build] => Windows
      )
      [quantity] => 1

我如何更换(更新)[quantity] => 1来说[quantity] => 3 :::

任何帮助都将受到赞赏:::

更新>>>更多信息。使事情更清楚

下面是我想要访问的钩子(woocommerce_calculate_totals)来改变上面打印的数组中来自print_r($this); :::

的值

我不确定如何使用add_action( 'woocommerce_calculate_totals', 'my_quantity' );

创建我需要更新值的函数

我使用过数组&以前挂钩,但这有点离我的联盟:::

//允许插件在计算最终总数之前挂钩并更改总计

do_action('woocommerce_calculate_totals', $this);                   

            /** 
             * Grand Total
             *
             * Based on discounted product prices, discounted tax, shipping cost + tax, and any discounts to be added after tax (e.g. store credit)
             */

$this->total = apply_filters('woocommerce_calculated_total', number_format( $this->cart_contents_total + $this->tax_total + $this->shipping_tax_total + $this->shipping_total - $this->discount_total, 2, '.', ''), $this);

3 个答案:

答案 0 :(得分:7)

这是你想要的吗?

$wc_cart->cart_contents['d6f5bb0bbc']['quantity'] = 3;

访问数组和对象的一些示例:

$object->some_property
$object->{'complex'.'property'.'name'.(2*3)}
$array[12]
$array['hello']
$array[ $array_index*10 ]

了解有关数组和对象的更多信息:

答案 1 :(得分:0)

首先从对象中获取cart_contents,然后逐步完成数组:

$wc_cart->cart_contents['d6f5bb0bbc']['quantity'] = 3;

答案 2 :(得分:0)

WordPress中有动作和过滤器,它们是不同的:

http://codex.wordpress.org/Function_Reference/add_action http://codex.wordpress.org/Function_Reference/add_filter

在WooCommerce中有两个非常相似的命名动作/过滤器:

  • woocommerce_calculate_totals action
  • woocommerce_calculated_total filter
使用购物车对象调用

woocommerce_calculate_totals个动作,因此您应该做的(并且希望应该有效)是:

add_action( 'woocommerce_calculate_totals', 'my_quantity' );

function my_quantity( $wc_cart ){
  $wc_cart->cart_contents['d6f5bb0bbc']['quantity'] = 3;
}