Home Forums WoodMart support forum Variation options don’t work well.

Variation options don’t work well.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #711518

    zngntr
    Participant

    I started two topics about variations, but the methods you suggested didn’t help me achieve what I wanted. So I wrote custom code and added it to the site using WP Code, and it worked as intended, but it only works for a single product. Even though I select the same variation, it doesn’t work for others. How can I fix this? I’m also sharing the custom code with you. This code is very functional. It automatically hides out-of-stock variations and sets the selected variation as the highest-priced variation currently in stock by default. I recommend adding this as a feature to your site.

    The code of “Automatically set the most expensive option among the variations as the “default selection”.:

    /**
    * Selects the variation with the highest price among those in stock
    * and sets it as the default selected variation on the product page.
    *
    * Note: WooCommerce allows filling the “selected” value
    * of variation dropdowns through this filter.
    */
    add_filter(
    ‘woocommerce_dropdown_variation_attribute_options_args’,
    ‘ptk_set_highest_price_instock_default_variation’,
    10,
    1
    );

    function ptk_set_highest_price_instock_default_variation( $args ) {

    // 1) This filter can sometimes be triggered outside the product page.
    // To avoid unnecessary execution, we run it only on the product page.
    if ( ! function_exists(‘is_product’) || ! is_product() ) {
    return $args;
    }

    // 2) Get the current product.
    // global $product holds the related product object on the product page.
    global $product;

    // 3) If the product does not exist or is not a variable product, do nothing.
    if ( ! $product || ! $product->is_type( ‘variable’ ) ) {
    return $args;
    }

    // 4) Get WooCommerce’s list of available variations.
    // get_available_variations() returns only purchasable/valid variations.
    $variations = $product->get_available_variations();

    // 5) If there are no variations, no action is needed.
    if ( empty( $variations ) ) {
    return $args;
    }

    // 6) Initial values to find the “highest price” among in-stock variations.
    $highest_price = -1; // Starting with -1 guarantees comparison even for 0-priced products.
    $target_variation = null; // The best candidate variation will be stored here.

    // 7) Loop through all variations.
    foreach ( $variations as $variation ) {

    // 7.1) Evaluate only variations that are in stock.
    // WooCommerce provides “is_in_stock” in the variation array.
    if ( empty( $variation[‘is_in_stock’] ) ) {
    continue;
    }

    // 7.2) Price of this variation:
    // display_price may be calculated based on tax settings.
    // Cast to float for reliable comparison.
    $price = isset( $variation[‘display_price’] )
    ? (float) $variation[‘display_price’]
    : 0.0;

    // 7.3) If this variation is more expensive, make it the target.
    if ( $price > $highest_price ) {
    $highest_price = $price;
    $target_variation = $variation;
    }
    }

    // 8) If no in-stock variation was found, do not modify anything.
    if ( ! $target_variation || empty( $target_variation[‘attributes’] ) ) {
    return $args;
    }

    // 9) Get the attribute values of the selected target variation.
    // Example: array(‘attribute_pa_gram’ => ‘500g’, ‘attribute_pa_color’ => ‘black’)
    $target_attributes = $target_variation[‘attributes’];

    // 10) This filter is called separately for EACH dropdown.
    // $args[‘attribute’] contains the dropdown’s attribute slug (e.g. “pa_gram”).
    // We find the corresponding attribute in the target variation and set it as selected.
    foreach ( $target_attributes as $attr_key => $attr_value ) {

    // 10.1) WooCommerce attribute keys come with the “attribute_” prefix.
    // Example: “attribute_pa_gram”
    if ( strpos( $attr_key, ‘attribute_’ ) !== 0 ) {
    continue;
    }

    // 10.2) Remove the “attribute_” prefix -> “pa_gram”
    $attribute_name = substr( $attr_key, strlen(‘attribute_’) );

    // 10.3) If this dropdown’s attribute matches the target variation’s attribute,
    // set the selected value.
    if ( isset( $args[‘attribute’] ) && $args[‘attribute’] === $attribute_name ) {
    $args[‘selected’] = $attr_value;
    break; // Done for this dropdown.
    }
    }

    // 11) Return the updated args.
    return $args;
    }

    The code of “Hide out-of-stock variations”:
    add_filter( ‘woocommerce_variation_is_active’, ‘hide_only_out_of_stock_variations’, 10, 2 );
    function hide_only_out_of_stock_variations( $active, $variation ) {
    if ( ! $variation->is_in_stock() ) {
    return false;
    }
    return $active;
    }

    CSS:
    /* Ürün sayfası – stokta olmayan varyasyonları görünmez yap */

    /* Nice-select (Woodmart’ta çok yaygın) */
    .single-product .variations_form .nice-select .list .option.disabled,
    .single-product .variations_form .nice-select .list .option[aria-disabled=”true”],
    .single-product .variations_form .nice-select .list .option.wc-variation-is-unavailable{
    display:none !important;
    }

    /* Select2 kullanılıyorsa */
    .single-product .select2-results__option[aria-disabled=”true”]{
    display:none !important;
    }

    /* Swatch/buton varyasyonlarda (genel) */
    .single-product .variations_form .wd-disabled,
    .single-product .variations_form .is-disabled,
    .single-product .variations_form .disabled,
    .single-product .variations_form .out-of-stock,
    .single-product .variations_form .wd-out-of-stock,
    .single-product .variations_form [aria-disabled=”true”]{
    display:none !important;
    }

    /* Native select: bazı tarayıcılar desteklemez ama kalsın */
    .single-product .variations_form select option:disabled{
    display:none !important;
    }

    CSS -2

    /* Varyasonlar Genel pill form */
    .single-product .wd-swatch.wd-text {
    height: 40px !important;
    padding: 0 22px !important;
    border-radius: 50px !important;
    border: 2px solid #fbd06a !important;
    background: #ffffff !important;
    color: #000 !important;
    font-weight: 500;
    transition: all .25s ease;
    }

    /* İç yazı */
    .single-product .wd-swatch.wd-text .wd-swatch-text {
    font-size: 14px;
    color: #000 !important;
    }

    /* Hover (seçili olmayanlar) */
    .single-product .wd-swatch.wd-text:hover {
    background: #fdf3dc !important;
    }

    /* SEÇİLİ OLAN */
    .single-product .wd-swatch.wd-text.wd-active {
    background: #fbd06a !important;
    border-color: #fbd06a !important;
    color: #000 !important;
    }

    /* Seçili hover değişmesin */
    .single-product .wd-swatch.wd-text.wd-active:hover {
    background: #fbd06a !important;
    }

    CSS – Mobile

    /* =================================================
    MOBİL: Varyasyon kutuları düzeni
    ================================================= */

    .single-product .wd-swatches-product{
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 10px !important;
    align-items: center !important;
    }

    .single-product .wd-swatches-product .wd-swatch.wd-text{
    width: auto !important;
    min-width: 72px !important;
    height: 38px !important;
    padding: 0 16px !important;
    border-radius: 999px !important;
    font-size: 13px !important;
    white-space: nowrap !important;
    }

    /* =================================================
    MOBİL: Sepete eklerken buton yukarı büyümesin
    (xts-theme top:-15px kuralını override eder)
    ================================================= */

    .single_variation_wrap .single_add_to_cart_button:before{
    top: 50% !important;
    transform: translateY(-50%) !important;
    }

    /* =================================================
    MOBİL: Buton yüksekliği sabit kalsın
    ================================================= */

    .single-product form.variations_form .single_add_to_cart_button,
    .single-product form.variations_form .single_add_to_cart_button.loading,
    .single-product form.variations_form .single_add_to_cart_button.added{
    min-height: 44px !important;
    line-height: 44px !important;
    box-sizing: border-box !important;
    padding-top: 0 !important;
    padding-bottom: 0 !important;
    padding-right: 3.2em !important;
    overflow: hidden !important;
    }

    /* =================================================
    MOBİL: Fiyat ve stok yazısı varsayılan boyutunda olsun
    (10px küçültme varsa override eder)
    ================================================= */

    .single_variation_wrap .woocommerce-variation-price,
    .single_variation_wrap .woocommerce-variation-availability,
    .single-product .summary .price,
    .single-product .stock{
    font-size: inherit !important;
    line-height: inherit !important;
    }

    Attachments:
    You must be logged in to view attached files.
    #711625

    Aizaz Imtiaz Awan
    Keymaster
    Xtemos team

    Hello,

    For the customization work, you have to hire a developer. The theme customization, this is beyond our limitations and support policy.

    Regards.
    Xtemos Studios

Viewing 2 posts - 1 through 2 (of 2 total)