• Resolved awilmsmeier

    (@awilmsmeier)


    Hi,
    I have made some enhancements in the 1.1.3 version of the plugin to allow for dependencies to be defined on variation level (and not just on product level).
    See below for details.
    Would you consider including them in the next update (obviously, as this is based upon the previous version, the changes cannot be directly applied to the 1.2 version, but I guess you get the general idea…

    Let me know, if you’d be interested – I could test it out on our sandbox, if that helps.
    Best regards,
    Andreas

    (As of 1.1.3)
    —> Modified plugins_loaded()

    public function plugins_loaded() {

    if ( is_admin() ) {

    // Save admin options.

    add_action( ‘woocommerce_admin_process_product_object’, array( $this, ‘process_product_data’ ) );

    add_action( ‘woocommerce_save_product_variation’, array( $this, ‘process_variant_data’ ), 10, 2 );

    // Add the “Dependencies” tab in Product Data.

    add_action( ‘woocommerce_product_data_tabs’, array( __CLASS__, ‘dependencies_product_data_tab’ ) );

    // Add the “Dependencies” tab content in Product Data.

    add_action( ‘woocommerce_product_data_panels’, array( $this, ‘dependencies_product_data_panel’ ) );

    add_action( ‘woocommerce_product_after_variable_attributes’, array( $this, ‘dependencies_variation_data_panel’ ), 10, 3 );

    }

    —> Modified add_to_cart_validation()

    public function add_to_cart_validation( $add, $item_id, $quantity, $variation_id, $variations ) {

    return $add && $this->evaluate_dependencies( $item_id, $variation_id );

    }

    —> Modified check_cart_items

    public function check_cart_items() {

    $cart_items = WC()->cart->cart_contents;

    foreach ( $cart_items as $cart_item ) {

    $this->evaluate_dependencies( $cart_item[‘product_id’], $cart_item[‘variation_id’] );

    }

    }

    —> Modified evaluate_dependencies (Part 1)

    // Use Variant for checking dependencies, if parameter is set

    if (!is_null($variant)) {

    $item = $variant;

    }

    if ( is_a( $item, ‘WC_Product’ ) || is_a( $item, ‘WC_Product_Variation’ )) {

    if ( $item->is_type( ‘variation’ ) ) {

    $product_id = WC_PD_Core_Compatibility::get_parent_id( $item );

    $product = wc_get_product( $product_id );

    $parent = wc_get_product($product->get_parent_id());

    $att = $product->get_attributes();

    $cat = $parent->get_category_ids();

    } else {

    $product_id = WC_PD_Core_Compatibility::get_id( $item );

    $product = $item;

    $parent = $product;

    $att = $product->get_attributes();

    $cat = $product->get_category_ids();

    }

    } else {

    $product_id = absint( $item );

    $product = wc_get_product( $product_id );

    $parent = wc_get_product($product->get_parent_id());

    $att = $product->get_attributes();

    $cat = $parent->get_category_ids();

    }

    if ( ! $product ) {

    return;

    }

    —> Modified dependencies_product_data_panel()

    This would not be shown for variations

    Added a similar function for showing the fields in variation data

    —> Modified process_variant_data()

    public function process_variant_data( $variant, $i ) {

    if ( ! isset( $_POST[ ‘tied_products’ ] ) || empty( $_POST[ ‘tied_products’ ] ) ) {

    delete_post_meta( $variant, ‘_tied_products’ );

    } elseif ( isset( $_POST[ ‘tied_products’ ] ) && ! empty( $_POST[ ‘tied_products’ ] ) ) {

    $tied_ids = $_POST[ ‘tied_products’ ];

    if ( is_array( $tied_ids ) ) {

    $tied_ids = array_map( ‘intval’, $tied_ids );

    } else {

    $tied_ids = array_filter( array_map( ‘intval’, explode( ‘,’, $tied_ids ) ) );

    }

    update_post_meta( $variant, ‘_tied_products’, $tied_ids );

    }

    if ( isset( $_POST[ ‘dependency_type’ ] ) && ! empty( $_POST[ ‘dependency_type’ ] ) ) {

    update_post_meta( $variant, ‘_dependency_type’, stripslashes( $_POST[ ‘dependency_type’ ] ) );

    }

    }

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Dependencies for product variations?’ is closed to new replies.