覆盖Woocommerce公共功能

时间:2016-01-28 08:37:47

标签: php wordpress woocommerce

我想将此功能从class-product-addon-cart.php覆盖到我的子主题functions.php

/**
     * add_cart_item function.
     *
     * @access public
     * @param mixed $cart_item
     * @return void
     */
    public function add_cart_item( $cart_item ) {
        // Adjust price if addons are set
        if ( ! empty( $cart_item['addons'] ) && apply_filters( 'woocommerce_product_addons_adjust_price', true, $cart_item ) ) {

            $extra_cost = 0;

            foreach ( $cart_item['addons'] as $addon ) {
                if ( $addon['price'] > 0 ) {
                    $extra_cost += $addon['price'];
                }
            }

            $cart_item['data']->adjust_price( $extra_cost );
        }

        return $cart_item;
    }

我有一种名为来自Woocommerce预订插件的住宿产品的产品以下是预订类的代码:

/**
 * Class for the booking product type
 */
class WC_Product_Booking extends WC_Product {
    private $availability_rules = array();

    /**
     * Constructor
     */
    public function __construct( $product ) {
        if ( empty ( $this->product_type ) ) {
            $this->product_type = 'booking';
        }

        parent::__construct( $product );
    }

因此,如果该产品是住宿产品,则执行该功能。

我写了这段代码,但它不起作用:

$accomodation = new WC_Product_Booking( get_the_ID() );
$product = new WC_Product( get_the_ID() );

if ($accomodation) {
    add_filter('woocommerce_product_addons_adjust_price', '__return_false');
} elseif($product) {
    add_filter('woocommerce_product_addons_adjust_price', true);
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

add_filter('woocommerce_product_addons_adjust_price', 'rei_product_addons_adjust_price', 10, 2);    
function rei_product_addons_adjust_price($is_adjust,$cart_item ){
    $product = $cart_item['data'];

    if ($product->is_type('booking')) {
        $is_adjust = false;
    }

    return $is_adjust;

}